From 3e03dcfac19901342a07f74b0dfd9d62dbb380f3 Mon Sep 17 00:00:00 2001 From: Reinier Zwitserloot Date: Sun, 26 Jul 2009 17:02:40 +0200 Subject: Fixed bugs with annotation handling: An array initializer with more than 1 entry now no longer causes ArrayIndexOutOfBoundsException, the setWarning method on a single item in an array initializer on eclipse now generates the warning on just that node (like with errors), and the API of AnnotationValues has been updated to support setting errors/warning on any node. --- src/lombok/core/AnnotationValues.java | 40 ++++++++++++++++++++++++++++++++++- src/lombok/eclipse/Eclipse.java | 13 ++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/lombok/core/AnnotationValues.java b/src/lombok/core/AnnotationValues.java index 7613212a..9d098b59 100644 --- a/src/lombok/core/AnnotationValues.java +++ b/src/lombok/core/AnnotationValues.java @@ -85,6 +85,18 @@ public class AnnotationValues { node.addError(message); } + /** + * Override this if you want more specific behaviour (to get the source position just right). + * + * @param message English message with the problem. + * @param valueIdx The index into the values for this annotation key that caused the problem. + * -1 for a problem that applies to all values, otherwise the 0-based index into an array of values. + * If there is no array for this value (e.g. value=1 instead of value={1,2}), then always -1 or 0. + */ + public void setWarning(String message, int valueIdx) { + node.addError(message); + } + /** {@inheritDoc} */ @Override public String toString() { return "raws: " + raws + " valueGuesses: " + valueGuesses; @@ -159,7 +171,7 @@ public class AnnotationValues { if ( expected.isArray() ) { isArray = true; expected = expected.getComponentType(); - array = Array.newInstance(expected, 1); + array = Array.newInstance(expected, v.valueGuesses.size()); } if ( !isArray && v.valueGuesses.size() > 1 ) { @@ -296,6 +308,32 @@ public class AnnotationValues { return l.isEmpty() ? null : l.get(0); } + /** Generates an error message on the stated annotation value (you should only call this method if you know it's there!) */ + public void setError(String annotationMethodName, String message) { + setError(annotationMethodName, message, -1); + } + + /** Generates a warning message on the stated annotation value (you should only call this method if you know it's there!) */ + public void setWarning(String annotationMethodName, String message) { + setWarning(annotationMethodName, message, -1); + } + + /** Generates an error message on the stated annotation value, which must have an array initializer. + * The index-th item in the initializer will carry the error (you should only call this method if you know it's there!) */ + public void setError(String annotationMethodName, String message, int index) { + AnnotationValue v = values.get(annotationMethodName); + if ( v == null ) return; + v.setError(message, index); + } + + /** Generates a warning message on the stated annotation value, which must have an array initializer. + * The index-th item in the initializer will carry the error (you should only call this method if you know it's there!) */ + public void setWarning(String annotationMethodName, String message, int index) { + AnnotationValue v = values.get(annotationMethodName); + if ( v == null ) return; + v.setWarning(message, index); + } + /** * Attempts to translate class literals to their fully qualified names, such as 'Throwable.class' to 'java.lang.Throwable'. * diff --git a/src/lombok/eclipse/Eclipse.java b/src/lombok/eclipse/Eclipse.java index a52b8d86..cba2bd05 100644 --- a/src/lombok/eclipse/Eclipse.java +++ b/src/lombok/eclipse/Eclipse.java @@ -315,6 +315,19 @@ public class Eclipse { annotationNode.addError(message, sourceStart, sourceEnd); } + + @Override public void setWarning(String message, int valueIdx) { + Expression ex; + if ( valueIdx == -1 ) ex = fullExpr; + else ex = exprs != null ? exprs[valueIdx] : null; + + if ( ex == null ) ex = annotation; + + int sourceStart = ex.sourceStart; + int sourceEnd = ex.sourceEnd; + + annotationNode.addWarning(message, sourceStart, sourceEnd); + } }); } -- cgit