From d6fe0a281b23f44afd241fc60f5401bbca6fa0a8 Mon Sep 17 00:00:00 2001 From: Reinier Zwitserloot Date: Mon, 19 Jan 2015 23:31:47 +0100 Subject: * Refactor AnnotationValues generator to also pick up on annotation ‘methods’ that don’t actually exist in the targeted annotation type; this is useful for old (experimental) variants that had more methods, for example. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../eclipse/handlers/EclipseHandlerUtil.java | 66 ++++++++++++---------- 1 file changed, 37 insertions(+), 29 deletions(-) (limited to 'src/core/lombok') diff --git a/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java b/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java index def9ee47..426171c2 100644 --- a/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java +++ b/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009-2014 The Project Lombok Authors. + * Copyright (C) 2009-2015 The Project Lombok Authors. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -201,6 +201,7 @@ public class EclipseHandlerUtil { public static void sanityCheckForMethodGeneratingAnnotationsOnBuilderClass(EclipseNode typeNode, EclipseNode errorNode) { List disallowed = null; for (EclipseNode child : typeNode.down()) { + if (child.getKind() != Kind.ANNOTATION) continue; for (Class annType : INVALID_ON_BUILDERS) { if (annotationTypeMatches(annType, child)) { if (disallowed == null) disallowed = new ArrayList(); @@ -681,47 +682,39 @@ public class EclipseHandlerUtil { */ public static AnnotationValues createAnnotation(Class type, final EclipseNode annotationNode) { + final Annotation annotation = (Annotation) annotationNode.get(); Map values = new HashMap(); - final MemberValuePair[] pairs = annotation.memberValuePairs(); - for (Method m : type.getDeclaredMethods()) { - if (!Modifier.isPublic(m.getModifiers())) continue; - String name = m.getName(); + MemberValuePair[] memberValuePairs = annotation.memberValuePairs(); + + if (memberValuePairs != null) for (final MemberValuePair pair : memberValuePairs) { List raws = new ArrayList(); List expressionValues = new ArrayList(); List guesses = new ArrayList(); - Expression fullExpression = null; Expression[] expressions = null; - if (pairs != null) for (MemberValuePair pair : pairs) { - char[] n = pair.name; - String mName = n == null ? "value" : new String(pair.name); - if (mName.equals(name)) fullExpression = pair.value; + char[] n = pair.name; + String mName = (n == null || n.length == 0) ? "value" : new String(pair.name); + final Expression rhs = pair.value; + if (rhs instanceof ArrayInitializer) { + expressions = ((ArrayInitializer)rhs).expressions; + } else if (rhs != null) { + expressions = new Expression[] { rhs }; } - - boolean isExplicit = fullExpression != null; - - if (isExplicit) { - if (fullExpression instanceof ArrayInitializer) { - expressions = ((ArrayInitializer)fullExpression).expressions; - } else expressions = new Expression[] { fullExpression }; - if (expressions != null) for (Expression ex : expressions) { - StringBuffer sb = new StringBuffer(); - ex.print(0, sb); - raws.add(sb.toString()); - expressionValues.add(ex); - guesses.add(calculateValue(ex)); - } + if (expressions != null) for (Expression ex : expressions) { + StringBuffer sb = new StringBuffer(); + ex.print(0, sb); + raws.add(sb.toString()); + expressionValues.add(ex); + guesses.add(calculateValue(ex)); } - final Expression fullExpr = fullExpression; final Expression[] exprs = expressions; - - values.put(name, new AnnotationValue(annotationNode, raws, expressionValues, guesses, isExplicit) { + values.put(mName, new AnnotationValue(annotationNode, raws, expressionValues, guesses, true) { @Override public void setError(String message, int valueIdx) { Expression ex; - if (valueIdx == -1) ex = fullExpr; + if (valueIdx == -1) ex = rhs; else ex = exprs != null ? exprs[valueIdx] : null; if (ex == null) ex = annotation; @@ -734,7 +727,7 @@ public class EclipseHandlerUtil { @Override public void setWarning(String message, int valueIdx) { Expression ex; - if (valueIdx == -1) ex = fullExpr; + if (valueIdx == -1) ex = rhs; else ex = exprs != null ? exprs[valueIdx] : null; if (ex == null) ex = annotation; @@ -747,6 +740,21 @@ public class EclipseHandlerUtil { }); } + for (Method m : type.getDeclaredMethods()) { + if (!Modifier.isPublic(m.getModifiers())) continue; + String name = m.getName(); + if (!values.containsKey(name)) { + values.put(name, new AnnotationValue(annotationNode, new ArrayList(), new ArrayList(), new ArrayList(), false) { + @Override public void setError(String message, int valueIdx) { + annotationNode.addError(message); + } + @Override public void setWarning(String message, int valueIdx) { + annotationNode.addWarning(message); + } + }); + } + } + return new AnnotationValues(type, values, annotationNode); } -- cgit