org.eclipse.jdt.internal.compiler.ast.SingleMemberAnnotation Java Examples

The following examples show how to use org.eclipse.jdt.internal.compiler.ast.SingleMemberAnnotation. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HandleConstructor.java    From EasyMPermission with MIT License 5 votes vote down vote up
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 };
}
 
Example #2
Source File: EclipseHandlerUtil.java    From EasyMPermission with MIT License 4 votes vote down vote up
public static List<Annotation> unboxAndRemoveAnnotationParameter(Annotation annotation, String annotationName, String errorName, EclipseNode errorNode) {
	if ("value".equals(annotationName)) {
		// We can't unbox this, because SingleMemberAnnotation REQUIRES a value, and this method
		// is supposed to remove the value. That means we need to replace the SMA with either
		// MarkerAnnotation or NormalAnnotation and that is beyond the scope of this method as we
		// don't need that at the time of writing this method; we only unbox onMethod, onParameter
		// 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())) {
		// Prevent MarkerAnnotation, SingleMemberAnnotation, and
		// 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();
	
	for (int i = 0; i < pairs.length; i++) {
		if (pairs[i].name == null || !Arrays.equals(nameAsCharArray, pairs[i].name)) continue;
		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 '@__({... annotations ...})',
		// which we must now unbox.
		if (!(value instanceof Annotation)) {
			errorNode.addError("The correct format is " + errorName + "@__({@SomeAnnotation, @SomeOtherAnnotation}))");
			return Collections.emptyList();
		}
		
		Annotation atDummyIdentifier = (Annotation) value;
		if (!(atDummyIdentifier.type instanceof SingleTypeReference) ||
				!isAllValidOnXCharacters(((SingleTypeReference) atDummyIdentifier.type).token)) {
			errorNode.addError("The correct format is " + errorName + "@__({@SomeAnnotation, @SomeOtherAnnotation}))");
			return Collections.emptyList();
		}
		
		if (atDummyIdentifier instanceof MarkerAnnotation) {
			// It's @Getter(onMethod=@__). This is weird, but fine.
			return Collections.emptyList();
		}
		
		Expression content = null;
		
		if (atDummyIdentifier instanceof NormalAnnotation) {
			MemberValuePair[] mvps = ((NormalAnnotation) atDummyIdentifier).memberValuePairs;
			if (mvps == null || mvps.length == 0) {
				// It's @Getter(onMethod=@__()). This is weird, but fine.
				return Collections.emptyList();
			}
			if (mvps.length == 1 && Arrays.equals("value".toCharArray(), mvps[0].name)) {
				content = mvps[0].value;
			}
		}
		
		if (atDummyIdentifier instanceof SingleMemberAnnotation) {
			content = ((SingleMemberAnnotation) atDummyIdentifier).memberValue;
		}
		
		if (content == null) {
			errorNode.addError("The correct format is " + errorName + "@__({@SomeAnnotation, @SomeOtherAnnotation}))");
			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 {
					errorNode.addError("The correct format is " + errorName + "@__({@SomeAnnotation, @SomeOtherAnnotation}))");
					return Collections.emptyList();
				}
			}
			return result;
		} else {
			errorNode.addError("The correct format is " + errorName + "@__({@SomeAnnotation, @SomeOtherAnnotation}))");
			return Collections.emptyList();
		}
	}
	
	return Collections.emptyList();
}
 
Example #3
Source File: SetGeneratedByVisitor.java    From EasyMPermission with MIT License 4 votes vote down vote up
@Override public boolean visit(SingleMemberAnnotation node, BlockScope scope) {
	fixPositions(setGeneratedBy(node, source));
	return super.visit(node, scope);
}