Skip to content

Commit

Permalink
Fix for defaulting behaviour for lombok annotations.
Browse files Browse the repository at this point in the history
What's the name of one of those bugs where you look at it and go: Wow. This can never have possibly worked?
And yet it has? An inverse heisenbug: Once observed, it has always existed, but before observing it, no problems.

Anyway, fixed. For what it's worth.
  • Loading branch information
rzwitserloot committed Jan 14, 2022
1 parent 267ef97 commit 2685d37
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 deletions.
17 changes: 9 additions & 8 deletions src/core/lombok/core/AnnotationValues.java
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public List<String> getAsStringList(String methodName) {
AnnotationValue v = values.get(methodName);

if (v == null) {
String[] s = getDefaultIf(methodName, String[].class, new String[0]);
String[] s = getDefaultIf(methodName, new String[0]);
return Collections.unmodifiableList(Arrays.asList(s));
}

Expand All @@ -175,7 +175,7 @@ public List<String> getAsStringList(String methodName) {
Object result = guess == null ? null : guessToType(guess, String.class, v, idx);
if (result == null) {
if (v.valueGuesses.size() == 1) {
String[] s = getDefaultIf(methodName, String[].class, new String[0]);
String[] s = getDefaultIf(methodName, new String[0]);
return Collections.unmodifiableList(Arrays.asList(s));
}
throw new AnnotationValueDecodeFail(v,
Expand All @@ -190,28 +190,29 @@ public List<String> getAsStringList(String methodName) {
public String getAsString(String methodName) {
AnnotationValue v = values.get(methodName);
if (v == null || v.valueGuesses.size() != 1) {
return getDefaultIf(methodName, String.class, "");
return getDefaultIf(methodName, "");
}

Object guess = guessToType(v.valueGuesses.get(0), String.class, v, 0);
if (guess instanceof String) return (String) guess;
return getDefaultIf(methodName, String.class, "");
return getDefaultIf(methodName, "");
}

public boolean getAsBoolean(String methodName) {
AnnotationValue v = values.get(methodName);
if (v == null || v.valueGuesses.size() != 1) {
return getDefaultIf(methodName, boolean.class, false);
return getDefaultIf(methodName, false);
}

Object guess = guessToType(v.valueGuesses.get(0), boolean.class, v, 0);
if (guess instanceof Boolean) return ((Boolean) guess).booleanValue();
return getDefaultIf(methodName, boolean.class, false);
return getDefaultIf(methodName, false);
}

public <T> T getDefaultIf(String methodName, Class<T> type, T defaultValue) {
@SuppressWarnings("unchecked")
public <T> T getDefaultIf(String methodName, T defaultValue) {
try {
return type.cast(Permit.getMethod(type, methodName).getDefaultValue());
return (T) Permit.getMethod(type, methodName).getDefaultValue();
} catch (Exception e) {
return defaultValue;
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/lombok/eclipse/handlers/HandleToString.java
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ public static MethodDeclaration createToString(EclipseNode type, Collection<Incl

if (!prefix.isEmpty()) {
StringLiteral px = new StringLiteral(prefix.toCharArray(), pS, pE, 0);
setGeneratedBy(px, source);
setGeneratedBy(px, source);
current = new BinaryExpression(current, px, PLUS);
current.sourceStart = pS; current.sourceEnd = pE;
setGeneratedBy(current, source);
Expand Down
4 changes: 2 additions & 2 deletions src/core/lombok/javac/HandlerLibrary.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,13 @@ private static class AnnotationHandlerContainer<T extends Annotation> {
this.handler = handler;
this.annotationClass = annotationClass;
HandlerPriority hp = handler.getClass().getAnnotation(HandlerPriority.class);
this.priority = hp == null ? 0L : (((long)hp.value()) << 32) + hp.subValue();
this.priority = hp == null ? 0L : (((long) hp.value()) << 32) + hp.subValue();
this.resolutionResetNeeded = handler.getClass().isAnnotationPresent(ResolutionResetNeeded.class);
this.evenIfAlreadyHandled = handler.getClass().isAnnotationPresent(AlreadyHandledAnnotations.class);
}

public void handle(final JavacNode node) {
handler.handle(JavacHandlerUtil.createAnnotation(annotationClass, node), (JCAnnotation)node.get(), node);
handler.handle(JavacHandlerUtil.createAnnotation(annotationClass, node), (JCAnnotation) node.get(), node);
}

public long getPriority() {
Expand Down

0 comments on commit 2685d37

Please sign in to comment.