use of org.eclipse.jdt.internal.compiler.ast.Annotation in project lombok by rzwitserloot.
the class EclipseHandlerUtil method addAnnotation.
private static Annotation[] addAnnotation(ASTNode source, Annotation[] originalAnnotationArray, char[][] annotationTypeFqn, ASTNode arg) {
char[] simpleName = annotationTypeFqn[annotationTypeFqn.length - 1];
if (originalAnnotationArray != null)
for (Annotation ann : originalAnnotationArray) {
if (ann.type instanceof QualifiedTypeReference) {
char[][] t = ((QualifiedTypeReference) ann.type).tokens;
if (Arrays.deepEquals(t, annotationTypeFqn))
return originalAnnotationArray;
}
if (ann.type instanceof SingleTypeReference) {
char[] lastToken = ((SingleTypeReference) ann.type).token;
if (Arrays.equals(lastToken, simpleName))
return originalAnnotationArray;
}
}
int pS = source.sourceStart, pE = source.sourceEnd;
long p = (long) pS << 32 | pE;
long[] poss = new long[annotationTypeFqn.length];
Arrays.fill(poss, p);
QualifiedTypeReference qualifiedType = new QualifiedTypeReference(annotationTypeFqn, poss);
setGeneratedBy(qualifiedType, source);
Annotation ann;
if (arg instanceof Expression) {
SingleMemberAnnotation sma = new SingleMemberAnnotation(qualifiedType, pS);
sma.declarationSourceEnd = pE;
arg.sourceStart = pS;
arg.sourceEnd = pE;
sma.memberValue = (Expression) arg;
setGeneratedBy(sma.memberValue, source);
ann = sma;
} else if (arg instanceof MemberValuePair) {
NormalAnnotation na = new NormalAnnotation(qualifiedType, pS);
na.declarationSourceEnd = pE;
arg.sourceStart = pS;
arg.sourceEnd = pE;
na.memberValuePairs = new MemberValuePair[] { (MemberValuePair) arg };
setGeneratedBy(na.memberValuePairs[0], source);
setGeneratedBy(na.memberValuePairs[0].value, source);
na.memberValuePairs[0].value.sourceStart = pS;
na.memberValuePairs[0].value.sourceEnd = pE;
ann = na;
} else {
MarkerAnnotation ma = new MarkerAnnotation(qualifiedType, pS);
ma.declarationSourceEnd = pE;
ann = ma;
}
setGeneratedBy(ann, source);
if (originalAnnotationArray == null)
return new Annotation[] { ann };
Annotation[] newAnnotationArray = new Annotation[originalAnnotationArray.length + 1];
System.arraycopy(originalAnnotationArray, 0, newAnnotationArray, 0, originalAnnotationArray.length);
newAnnotationArray[originalAnnotationArray.length] = ann;
return newAnnotationArray;
}
use of org.eclipse.jdt.internal.compiler.ast.Annotation in project lombok by rzwitserloot.
the class EclipseHandlerUtil method unboxAndRemoveAnnotationParameter.
public static List<Annotation> unboxAndRemoveAnnotationParameter(Annotation annotation, String annotationName, String errorName, EclipseNode errorNode) {
if ("value".equals(annotationName)) {
// and onConstructor. Let's exit early and very obviously:
throw new UnsupportedOperationException("Lombok cannot unbox 'value' from SingleMemberAnnotation at this time.");
}
if (!NormalAnnotation.class.equals(annotation.getClass())) {
// CompletionOnAnnotationMemberValuePair from triggering this handler.
return Collections.emptyList();
}
NormalAnnotation normalAnnotation = (NormalAnnotation) annotation;
MemberValuePair[] pairs = normalAnnotation.memberValuePairs;
if (pairs == null)
return Collections.emptyList();
char[] nameAsCharArray = annotationName.toCharArray();
top: for (int i = 0; i < pairs.length; i++) {
boolean allowRaw;
char[] name = pairs[i].name;
if (name == null)
continue;
if (name.length < nameAsCharArray.length)
continue;
for (int j = 0; j < nameAsCharArray.length; j++) {
if (name[j] != nameAsCharArray[j])
continue top;
}
allowRaw = name.length > nameAsCharArray.length;
for (int j = nameAsCharArray.length; j < name.length; j++) {
if (name[j] != '_')
continue top;
}
// If we're still here it's the targeted annotation param.
Expression value = pairs[i].value;
MemberValuePair[] newPairs = new MemberValuePair[pairs.length - 1];
if (i > 0)
System.arraycopy(pairs, 0, newPairs, 0, i);
if (i < pairs.length - 1)
System.arraycopy(pairs, i + 1, newPairs, i, pairs.length - i - 1);
normalAnnotation.memberValuePairs = newPairs;
// We have now removed the annotation parameter and stored the value,
// which we must now unbox. It's either annotations, or @__(annotations).
Expression content = null;
if (value instanceof ArrayInitializer) {
if (!allowRaw) {
addError(errorName, errorNode);
return Collections.emptyList();
}
content = value;
} else if (!(value instanceof Annotation)) {
addError(errorName, errorNode);
return Collections.emptyList();
} else {
Annotation atDummyIdentifier = (Annotation) value;
if (atDummyIdentifier.type instanceof SingleTypeReference && isAllValidOnXCharacters(((SingleTypeReference) atDummyIdentifier.type).token)) {
if (atDummyIdentifier instanceof MarkerAnnotation) {
return Collections.emptyList();
} else if (atDummyIdentifier instanceof NormalAnnotation) {
MemberValuePair[] mvps = ((NormalAnnotation) atDummyIdentifier).memberValuePairs;
if (mvps == null || mvps.length == 0) {
return Collections.emptyList();
}
if (mvps.length == 1 && Arrays.equals("value".toCharArray(), mvps[0].name)) {
content = mvps[0].value;
}
} else if (atDummyIdentifier instanceof SingleMemberAnnotation) {
content = ((SingleMemberAnnotation) atDummyIdentifier).memberValue;
} else {
addError(errorName, errorNode);
return Collections.emptyList();
}
} else {
if (allowRaw) {
content = atDummyIdentifier;
} else {
addError(errorName, errorNode);
return Collections.emptyList();
}
}
}
if (content == null) {
addError(errorName, errorNode);
return Collections.emptyList();
}
if (content instanceof Annotation) {
return Collections.singletonList((Annotation) content);
} else if (content instanceof ArrayInitializer) {
Expression[] expressions = ((ArrayInitializer) content).expressions;
List<Annotation> result = new ArrayList<Annotation>();
if (expressions != null)
for (Expression ex : expressions) {
if (ex instanceof Annotation)
result.add((Annotation) ex);
else {
addError(errorName, errorNode);
return Collections.emptyList();
}
}
return result;
} else {
addError(errorName, errorNode);
return Collections.emptyList();
}
}
return Collections.emptyList();
}
use of org.eclipse.jdt.internal.compiler.ast.Annotation in project che by eclipse.
the class SourceTypeConverter method convertAnnotations.
private Annotation[] convertAnnotations(IAnnotatable element) throws JavaModelException {
IAnnotation[] annotations = element.getAnnotations();
int length = annotations.length;
Annotation[] astAnnotations = new Annotation[length];
if (length > 0) {
char[] cuSource = getSource();
int recordedAnnotations = 0;
for (int i = 0; i < length; i++) {
ISourceRange positions = annotations[i].getSourceRange();
int start = positions.getOffset();
int end = start + positions.getLength();
char[] annotationSource = CharOperation.subarray(cuSource, start, end);
if (annotationSource != null) {
Expression expression = parseMemberValue(annotationSource);
/*
* expression can be null or not an annotation if the source has changed between
* the moment where the annotation source positions have been retrieved and the moment were
* this parsing occurred.
* See https://bugs.eclipse.org/bugs/show_bug.cgi?id=90916
*/
if (expression instanceof Annotation) {
astAnnotations[recordedAnnotations++] = (Annotation) expression;
}
}
}
if (length != recordedAnnotations) {
// resize to remove null annotations
System.arraycopy(astAnnotations, 0, (astAnnotations = new Annotation[recordedAnnotations]), 0, recordedAnnotations);
}
}
return astAnnotations;
}
use of org.eclipse.jdt.internal.compiler.ast.Annotation in project lombok by rzwitserloot.
the class HandleEqualsAndHashCode method handle.
@Override
public void handle(AnnotationValues<EqualsAndHashCode> annotation, Annotation ast, EclipseNode annotationNode) {
handleFlagUsage(annotationNode, ConfigurationKeys.EQUALS_AND_HASH_CODE_FLAG_USAGE, "@EqualsAndHashCode");
EqualsAndHashCode ann = annotation.getInstance();
List<String> excludes = Arrays.asList(ann.exclude());
List<String> includes = Arrays.asList(ann.of());
EclipseNode typeNode = annotationNode.up();
List<Annotation> onParam = unboxAndRemoveAnnotationParameter(ast, "onParam", "@EqualsAndHashCode(onParam", annotationNode);
checkForBogusFieldNames(typeNode, annotation);
Boolean callSuper = ann.callSuper();
if (!annotation.isExplicit("callSuper"))
callSuper = null;
if (!annotation.isExplicit("exclude"))
excludes = null;
if (!annotation.isExplicit("of"))
includes = null;
if (excludes != null && includes != null) {
excludes = null;
annotation.setWarning("exclude", "exclude and of are mutually exclusive; the 'exclude' parameter will be ignored.");
}
Boolean doNotUseGettersConfiguration = annotationNode.getAst().readConfiguration(ConfigurationKeys.EQUALS_AND_HASH_CODE_DO_NOT_USE_GETTERS);
boolean doNotUseGetters = annotation.isExplicit("doNotUseGetters") || doNotUseGettersConfiguration == null ? ann.doNotUseGetters() : doNotUseGettersConfiguration;
FieldAccess fieldAccess = doNotUseGetters ? FieldAccess.PREFER_FIELD : FieldAccess.GETTER;
generateMethods(typeNode, annotationNode, excludes, includes, callSuper, true, fieldAccess, onParam);
}
use of org.eclipse.jdt.internal.compiler.ast.Annotation in project lombok by rzwitserloot.
the class HandleConstructor method createConstructorProperties.
public static Annotation[] createConstructorProperties(ASTNode source, Collection<EclipseNode> fields) {
if (fields.isEmpty())
return null;
int pS = source.sourceStart, pE = source.sourceEnd;
long p = (long) pS << 32 | pE;
long[] poss = new long[3];
Arrays.fill(poss, p);
QualifiedTypeReference constructorPropertiesType = new QualifiedTypeReference(JAVA_BEANS_CONSTRUCTORPROPERTIES, poss);
setGeneratedBy(constructorPropertiesType, source);
SingleMemberAnnotation ann = new SingleMemberAnnotation(constructorPropertiesType, pS);
ann.declarationSourceEnd = pE;
ArrayInitializer fieldNames = new ArrayInitializer();
fieldNames.sourceStart = pS;
fieldNames.sourceEnd = pE;
fieldNames.expressions = new Expression[fields.size()];
int ctr = 0;
for (EclipseNode field : fields) {
char[] fieldName = removePrefixFromField(field);
fieldNames.expressions[ctr] = new StringLiteral(fieldName, pS, pE, 0);
setGeneratedBy(fieldNames.expressions[ctr], source);
ctr++;
}
ann.memberValue = fieldNames;
setGeneratedBy(ann, source);
setGeneratedBy(ann.memberValue, source);
return new Annotation[] { ann };
}
Aggregations