mirror of
https://github.com/tiennm99/lombok.git
synced 2026-10-14 04:16:19 +00:00
Provide access to the actual annotation values
This commit is contained in:
1 parent
2e88614a84
commit
50524f338e
3 files changed
+31
-17
No files matched your search
@@ -53,29 +53,20 @@ public class AnnotationValues<A extends Annotation> {
|
||||
/** Guesses for each raw expression. If the raw expression is a literal expression, the guess will
|
||||
* likely be right. If not, it'll be wrong. */
|
||||
public final List<Object> valueGuesses;
|
||||
|
||||
/** A list of the actual expressions. List is size 1 unless an array is provided. */
|
||||
public final List<Object> expressions;
|
||||
|
||||
private final LombokNode<?, ?, ?> node;
|
||||
private final boolean isExplicit;
|
||||
|
||||
/**
|
||||
* 'raw' should be the exact expression, for example '5+7', 'AccessLevel.PUBLIC', or 'int.class'.
|
||||
* 'valueGuess' should be a likely guess at the real value intended.
|
||||
*
|
||||
* For classes, supply the class name (qualified or not) as a string.<br />
|
||||
* For enums, supply the simple name part (everything after the last dot) as a string.<br />
|
||||
*/
|
||||
public AnnotationValue(LombokNode<?, ?, ?> node, String raw, Object valueGuess, boolean isExplicit) {
|
||||
this.node = node;
|
||||
this.raws = Collections.singletonList(raw);
|
||||
this.valueGuesses = Collections.singletonList(valueGuess);
|
||||
this.isExplicit = isExplicit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Like the other constructor, but used for when the annotation method is initialized with an array value.
|
||||
*/
|
||||
public AnnotationValue(LombokNode<?, ?, ?> node, List<String> raws, List<Object> valueGuesses, boolean isExplicit) {
|
||||
public AnnotationValue(LombokNode<?, ?, ?> node, List<String> raws, List<Object> expressions, List<Object> valueGuesses, boolean isExplicit) {
|
||||
this.node = node;
|
||||
this.raws = raws;
|
||||
this.expressions = expressions;
|
||||
this.valueGuesses = valueGuesses;
|
||||
this.isExplicit = isExplicit;
|
||||
}
|
||||
@@ -310,6 +301,14 @@ public class AnnotationValues<A extends Annotation> {
|
||||
return v == null ? Collections.<String>emptyList() : v.raws;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the actual expressions used for the provided {@code annotationMethodName}.
|
||||
*/
|
||||
public List<Object> getActualExpressions(String annotationMethodName) {
|
||||
AnnotationValue v = values.get(annotationMethodName);
|
||||
return v == null ? Collections.<Object>emptyList() : v.expressions;
|
||||
}
|
||||
|
||||
public boolean isExplicit(String annotationMethodName) {
|
||||
AnnotationValue annotationValue = values.get(annotationMethodName);
|
||||
return annotationValue != null && annotationValue.isExplicit();
|
||||
@@ -325,6 +324,16 @@ public class AnnotationValues<A extends Annotation> {
|
||||
return l.isEmpty() ? null : l.get(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convenience method to return the first result in a {@link #getActualExpressions(String)} call.
|
||||
*
|
||||
* You should use this method if the annotation method is not an array type.
|
||||
*/
|
||||
public Object getActualExpression(String annotationMethodName) {
|
||||
List<Object> l = getActualExpressions(annotationMethodName);
|
||||
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);
|
||||
|
||||
@@ -377,6 +377,7 @@ public class Eclipse {
|
||||
if (!Modifier.isPublic(m.getModifiers())) continue;
|
||||
String name = m.getName();
|
||||
List<String> raws = new ArrayList<String>();
|
||||
List<Object> expressionValues = new ArrayList<Object>();
|
||||
List<Object> guesses = new ArrayList<Object>();
|
||||
Expression fullExpression = null;
|
||||
Expression[] expressions = null;
|
||||
@@ -397,6 +398,7 @@ public class Eclipse {
|
||||
StringBuffer sb = new StringBuffer();
|
||||
ex.print(0, sb);
|
||||
raws.add(sb.toString());
|
||||
expressionValues.add(ex);
|
||||
guesses.add(calculateValue(ex));
|
||||
}
|
||||
}
|
||||
@@ -404,7 +406,7 @@ public class Eclipse {
|
||||
final Expression fullExpr = fullExpression;
|
||||
final Expression[] exprs = expressions;
|
||||
|
||||
values.put(name, new AnnotationValue(annotationNode, raws, guesses, isExplicit) {
|
||||
values.put(name, new AnnotationValue(annotationNode, raws, expressionValues, guesses, isExplicit) {
|
||||
@Override public void setError(String message, int valueIdx) {
|
||||
Expression ex;
|
||||
if (valueIdx == -1) ex = fullExpr;
|
||||
|
||||
@@ -90,6 +90,7 @@ public class Javac {
|
||||
String name = m.getName();
|
||||
List<String> raws = new ArrayList<String>();
|
||||
List<Object> guesses = new ArrayList<Object>();
|
||||
List<Object> expressions = new ArrayList<Object>();
|
||||
final List<DiagnosticPosition> positions = new ArrayList<DiagnosticPosition>();
|
||||
boolean isExplicit = false;
|
||||
|
||||
@@ -112,17 +113,19 @@ public class Javac {
|
||||
List<JCExpression> elems = ((JCNewArray)rhs).elems;
|
||||
for (JCExpression inner : elems) {
|
||||
raws.add(inner.toString());
|
||||
expressions.add(inner);
|
||||
guesses.add(calculateGuess(inner));
|
||||
positions.add(inner.pos());
|
||||
}
|
||||
} else {
|
||||
raws.add(rhs.toString());
|
||||
expressions.add(rhs);
|
||||
guesses.add(calculateGuess(rhs));
|
||||
positions.add(rhs.pos());
|
||||
}
|
||||
}
|
||||
|
||||
values.put(name, new AnnotationValue(node, raws, guesses, isExplicit) {
|
||||
values.put(name, new AnnotationValue(node, raws, expressions, guesses, isExplicit) {
|
||||
@Override public void setError(String message, int valueIdx) {
|
||||
if (valueIdx < 0) node.addError(message);
|
||||
else node.addError(message, positions.get(valueIdx));
|
||||
|
||||
Reference in new issue
Block a user