Skip to content

Commit 38e7d59

Browse files
annot: flag @mc* setters which have a non-void return type as an error #2862 (#2864)
1 parent 57e9411 commit 38e7d59

7 files changed

Lines changed: 51 additions & 19 deletions

File tree

annot/src/main/java/com/predic8/membrane/annot/SpringConfigurationXSDGeneratingAnnotationProcessor.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,7 @@ private void scan(MainInfo main, ElementInfo ii, TypeElement te) {
461461
scan(main, ii, (TypeElement) ((DeclaredType) superclass).asElement());
462462

463463
for (Element e2 : te.getEnclosedElements()) {
464+
validateMcSetterReturnType(e2);
464465
MCAttribute a = e2.getAnnotation(MCAttribute.class);
465466
if (a != null) {
466467
AttributeInfo ai = new AttributeInfo();
@@ -530,6 +531,28 @@ private void scan(MainInfo main, ElementInfo ii, TypeElement te) {
530531
Collections.sort(ii.getChildElementSpecs());
531532
}
532533

534+
private void validateMcSetterReturnType(Element e2) {
535+
if (!(e2 instanceof ExecutableElement executableElement))
536+
return;
537+
String annotationName = getMcSetterAnnotationName(e2);
538+
if (annotationName == null)
539+
return;
540+
if (executableElement.getReturnType().getKind() != TypeKind.VOID)
541+
throw new ProcessingException("Setter annotated with @" + annotationName + " must return void.", e2);
542+
}
543+
544+
private String getMcSetterAnnotationName(Element e2) {
545+
if (e2.getAnnotation(MCAttribute.class) != null)
546+
return "MCAttribute";
547+
if (e2.getAnnotation(MCChildElement.class) != null)
548+
return "MCChildElement";
549+
if (e2.getAnnotation(MCTextContent.class) != null)
550+
return "MCTextContent";
551+
if (e2.getAnnotation(MCOtherAttributes.class) != null)
552+
return "MCOtherAttributes";
553+
return null;
554+
}
555+
533556
private boolean isRequired(Element e2) {
534557
for (AnnotationMirror am : e2.getAnnotationMirrors())
535558
if (((TypeElement) am.getAnnotationType().asElement()).getQualifiedName().toString().equals(REQUIRED))
@@ -581,4 +604,4 @@ private void validateEnumConstantsUppercase(TypeElement enumType) {
581604
}
582605
}
583606
}
584-
}
607+
}

annot/src/test/java/com/predic8/membrane/annot/SpringConfigXSDErrorsTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,4 +355,22 @@ public class Child2 extends AbstractDemoChildElement {
355355
), result);
356356
}
357357

358+
@Test
359+
public void mcSetterMustReturnVoid() {
360+
var sources = splitSources(MC_MAIN_DEMO + """
361+
package com.predic8.membrane.demo;
362+
import com.predic8.membrane.annot.*;
363+
@MCElement(name="demo")
364+
public class DemoElement {
365+
@MCAttribute
366+
public String setName(String name) { return name; }
367+
}
368+
""");
369+
var result = CompilerHelper.compile(sources, false);
370+
371+
assertCompilerResult(false, of(
372+
error("Setter annotated with @MCAttribute must return void.")
373+
), result);
374+
}
375+
358376
}

core/src/main/java/com/predic8/membrane/core/interceptor/jwt/HeaderJwtRetriever.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,16 @@ public String getHeader() {
5151

5252
@Required
5353
@MCAttribute
54-
public HeaderJwtRetriever setHeader(String header) {
54+
public void setHeader(String header) {
5555
this.header = header;
56-
return this;
5756
}
5857

5958
public String getRemoveFromValue() {
6059
return removeFromValue;
6160
}
6261

6362
@MCAttribute
64-
public HeaderJwtRetriever setRemoveFromValue(String removeFromValue) {
63+
public void setRemoveFromValue(String removeFromValue) {
6564
this.removeFromValue = removeFromValue;
66-
return this;
6765
}
6866
}

core/src/main/java/com/predic8/membrane/core/interceptor/jwt/Jwks.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,12 @@ public List<Jwk> getJwks() {
100100
}
101101

102102
@MCChildElement
103-
public Jwks setJwks(List<Jwk> jwks) {
103+
public void setJwks(List<Jwk> jwks) {
104104
if (router != null) { // set in init, so we can't update prior to that call
105105
if (jwks == null) throw new ConfigurationException("JWKs list must not be null.");
106106
this.keysByKid = buildKeyMap(jwks);
107107
}
108108
this.jwks = (jwks == null) ? emptyList() : jwks; // unnecessary, mainly for consistency when debugging
109-
return this;
110109
}
111110

112111
public String getJwksUris() {
@@ -232,9 +231,8 @@ public String getKid() {
232231
}
233232

234233
@MCAttribute
235-
public Jwk setKid(String kid) {
234+
public void setKid(String kid) {
236235
this.kid = kid;
237-
return this;
238236
}
239237

240238
/**

core/src/main/java/com/predic8/membrane/core/interceptor/jwt/JwtAuthInterceptor.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,9 +197,8 @@ public String getExpectedTid() {
197197
* <p>Use "any!!" to allow any audience value. This is strongly discouraged.</p>
198198
*/
199199
@MCAttribute
200-
public JwtAuthInterceptor setExpectedAud(String expectedAud) {
200+
public void setExpectedAud(String expectedAud) {
201201
this.expectedAud = expectedAud;
202-
return this;
203202
}
204203

205204
/**
@@ -209,9 +208,8 @@ public JwtAuthInterceptor setExpectedAud(String expectedAud) {
209208
* @example 67c869d3-0cd4-4a99-86db-088bed1a9601
210209
*/
211210
@MCAttribute
212-
public JwtAuthInterceptor setExpectedTid(String expectedTid) {
211+
public void setExpectedTid(String expectedTid) {
213212
this.expectedTid = expectedTid;
214-
return this;
215213
}
216214

217215
@Override

core/src/main/java/com/predic8/membrane/core/interceptor/ntlm/NtlmInterceptor.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,8 @@ private void prepareStreamByEmptyingIt(Exchange exc) {
156156
}
157157

158158
@MCChildElement(order = 1)
159-
public NtlmInterceptor setNTLMRetriever(NTLMRetriever NTLMRetriever) {
159+
public void setNTLMRetriever(NTLMRetriever NTLMRetriever) {
160160
this.NTLMRetriever = NTLMRetriever;
161-
return this;
162161
}
163162

164163
public NTLMRetriever getNTLMRetriever() {

core/src/main/java/com/predic8/membrane/core/interceptor/session/SessionManager.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -437,9 +437,8 @@ public boolean isSecure() {
437437
* @default false
438438
*/
439439
@MCAttribute
440-
public SessionManager setSecure(boolean secure) {
440+
public void setSecure(boolean secure) {
441441
this.secure = secure;
442-
return this;
443442
}
444443

445444
public boolean isSessionCookie() {
@@ -451,9 +450,8 @@ public boolean isSessionCookie() {
451450
* @default false
452451
*/
453452
@MCAttribute
454-
public SessionManager setSessionCookie(boolean sessionCookie) {
453+
public void setSessionCookie(boolean sessionCookie) {
455454
this.sessionCookie = sessionCookie;
456-
return this;
457455
}
458456

459457
protected String getKeyOfCookie(String validCookie) {

0 commit comments

Comments
 (0)