Java Code Examples for org.eclipse.jdt.internal.compiler.lookup.AnnotationBinding#getAnnotationType()

The following examples show how to use org.eclipse.jdt.internal.compiler.lookup.AnnotationBinding#getAnnotationType() . 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: PatchDelegate.java    From EasyMPermission with MIT License 6 votes vote down vote up
private static void failIfContainsAnnotation(TypeBinding parent, Binding[] bindings) throws DelegateRecursion {
	if (bindings == null) return;
	
	for (Binding b : bindings) {
		AnnotationBinding[] anns = null;
		if (b instanceof MethodBinding) anns = ((MethodBinding) b).getAnnotations();
		if (b instanceof FieldBinding) anns = ((FieldBinding) b).getAnnotations();
		// anns = b.getAnnotations() would make a heck of a lot more sense, but that is a late addition to ecj, so would cause NoSuchMethodErrors! Don't use that!
		if (anns == null) continue;
		for (AnnotationBinding ann : anns) {
			char[][] name = null;
			try {
				name = ann.getAnnotationType().compoundName;
			} catch (Exception ignore) {}
			
			if (name == null || name.length < 2 || name.length > 3) continue;
			if (!Arrays.equals(STRING_LOMBOK, name[0])) continue;
			if (!Arrays.equals(STRING_DELEGATE, name[name.length - 1])) continue;
			if (name.length == 3 && !Arrays.equals(STRING_EXPERIMENTAL, name[1])) continue;
			
			throw new DelegateRecursion(parent.readableName(), b.readableName());
		}
	}
}
 
Example 2
Source File: Factory.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private AnnotationMirrorImpl createAnnotationMirror(String annoTypeName, AnnotationBinding annoInstance) {
	ReferenceBinding binding = annoInstance.getAnnotationType();
	if (binding != null && binding.isAnnotationType() ) {
		char[] qName;
		if (binding.isMemberType()) {
			annoTypeName = annoTypeName.replace('$', '.');
			qName = CharOperation.concatWith(binding.enclosingType().compoundName, binding.sourceName, '.');
			CharOperation.replace(qName, '$', '.');
		} else {
			qName = CharOperation.concatWith(binding.compoundName, '.');
		}
		if(annoTypeName.equals(new String(qName)) ){
			return (AnnotationMirrorImpl)_env.getFactory().newAnnotationMirror(annoInstance);
		}
	}
	return null;
}
 
Example 3
Source File: RoundEnvImpl.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Check whether an element has a superclass that is annotated with an @Inherited annotation.
 * @param element must be a class (not an interface, enum, etc.).
 * @param anno must be an annotation type, and must be @Inherited
 * @return true if element has a superclass that is annotated with anno
 */
private boolean inheritsAnno(ReferenceBinding element, ReferenceBinding anno) {
	ReferenceBinding searchedElement = element;
	do {
		if (searchedElement instanceof ParameterizedTypeBinding) {
			searchedElement = ((ParameterizedTypeBinding) searchedElement).genericType();
		}
		AnnotationBinding[] annos = Factory.getPackedAnnotationBindings(searchedElement.getAnnotations());
		for (AnnotationBinding annoBinding : annos) {
			if (annoBinding.getAnnotationType() == anno) { //$IDENTITY-COMPARISON$
				// element is annotated with anno
				return true;
			}
		}
	} while (null != (searchedElement = searchedElement.superclass()));
	return false;
}
 
Example 4
Source File: BindingKeyResolver.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public void consumeAnnotation() {
	int size = this.types.size();
	if (size == 0) return;
	Binding annotationType = ((BindingKeyResolver) this.types.get(size-1)).compilerBinding;
	AnnotationBinding[] annotationBindings;
	if (this.compilerBinding == null && this.typeBinding instanceof ReferenceBinding) {
		annotationBindings = ((ReferenceBinding) this.typeBinding).getAnnotations();
	} else if (this.compilerBinding instanceof MethodBinding) {
		annotationBindings = ((MethodBinding) this.compilerBinding).getAnnotations();
	} else if (this.compilerBinding instanceof VariableBinding) {
		annotationBindings = ((VariableBinding) this.compilerBinding).getAnnotations();
	} else {
		return;
	}
	for (int i = 0, length = annotationBindings.length; i < length; i++) {
		AnnotationBinding binding = annotationBindings[i];
		if (binding.getAnnotationType() == annotationType) {
			this.annotationBinding = binding;
			break;
		}
	}
}
 
Example 5
Source File: AnnotationMirrorImpl.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static boolean equals(AnnotationBinding annotationBinding, AnnotationBinding annotationBinding2) {
	if (annotationBinding.getAnnotationType() != annotationBinding2.getAnnotationType()) return false; //$IDENTITY-COMPARISON$
	final ElementValuePair[] elementValuePairs = annotationBinding.getElementValuePairs();
	final ElementValuePair[] elementValuePairs2 = annotationBinding2.getElementValuePairs();
	final int length = elementValuePairs.length;
	if (length != elementValuePairs2.length) return false;
	loop: for (int i = 0; i < length; i++) {
		ElementValuePair pair = elementValuePairs[i];
		// loop on the given pair to make sure one will match
		for (int j = 0; j < length; j++) {
			ElementValuePair pair2 = elementValuePairs2[j];
			if (pair.binding == pair2.binding) {
				if (pair.value == null) {
					if (pair2.value == null) {
						continue loop;
					}
					return false;
				} else {
					if (pair2.value == null) return false;
					if (pair2.value instanceof Object[] && pair.value instanceof Object[]) {
						if (!Arrays.equals((Object[]) pair.value, (Object[]) pair2.value)) {
							return false;
						}
					} else if (!pair2.value.equals(pair.value)){
						return false;
					}
				}
				continue loop;
			}
		}
		return false;
	}
	return true;
}
 
Example 6
Source File: LambdaExpression.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private void mergeParameterNullAnnotations(BlockScope currentScope) {
	LookupEnvironment env = currentScope.environment();
	TypeBinding[] ourParameters = this.binding.parameters;
	TypeBinding[] descParameters = this.descriptor.parameters;
	int len = Math.min(ourParameters.length, descParameters.length);
	for (int i = 0; i < len; i++) {
		long ourTagBits = ourParameters[i].tagBits & TagBits.AnnotationNullMASK;
		long descTagBits = descParameters[i].tagBits & TagBits.AnnotationNullMASK;
		if (ourTagBits == 0L) {
			if (descTagBits != 0L && !ourParameters[i].isBaseType()) {
				AnnotationBinding [] annotations = descParameters[i].getTypeAnnotations();
				for (int j = 0, length = annotations.length; j < length; j++) {
					AnnotationBinding annotation = annotations[j];
					if (annotation != null) {
						switch (annotation.getAnnotationType().id) {
							case TypeIds.T_ConfiguredAnnotationNullable :
							case TypeIds.T_ConfiguredAnnotationNonNull :
								ourParameters[i] = env.createAnnotatedType(ourParameters[i], new AnnotationBinding [] { annotation });
								break;
						}
					}
				}
			}
		} else if (ourTagBits != descTagBits) {
			if (ourTagBits == TagBits.AnnotationNonNull) { // requested @NonNull not provided
				char[][] inheritedAnnotationName = null;
				if (descTagBits == TagBits.AnnotationNullable)
					inheritedAnnotationName = env.getNullableAnnotationName();
				currentScope.problemReporter().illegalRedefinitionToNonNullParameter(this.arguments[i], this.descriptor.declaringClass, inheritedAnnotationName);
			}
		}			
	}
}
 
Example 7
Source File: Factory.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public static AnnotationBinding [] getPackedAnnotationBindings(AnnotationBinding [] annotations) {
	
	int length = annotations == null ? 0 : annotations.length;
	if (length == 0)
		return annotations;
	
	AnnotationBinding[] repackagedBindings = annotations; // only replicate if repackaging.
	for (int i = 0; i < length; i++) {
		AnnotationBinding annotation = repackagedBindings[i];
		if (annotation == null) continue;
		ReferenceBinding annotationType = annotation.getAnnotationType();
		if (!annotationType.isRepeatableAnnotationType())
			continue;
		ReferenceBinding containerType = annotationType.containerAnnotationType();
		if (containerType == null)
			continue; // FUBAR.
		MethodBinding [] values = containerType.getMethods(TypeConstants.VALUE);
		if (values == null || values.length != 1)
			continue; // FUBAR.
		MethodBinding value = values[0];
		if (value.returnType == null || value.returnType.dimensions() != 1 || TypeBinding.notEquals(value.returnType.leafComponentType(), annotationType))
			continue; // FUBAR
		
		// We have a kosher repeatable annotation with a kosher containing type. See if actually repeats.
		List<AnnotationBinding> containees = null;
		for (int j = i + 1; j < length; j++) {
			AnnotationBinding otherAnnotation = repackagedBindings[j];
			if (otherAnnotation == null) continue;
			if (otherAnnotation.getAnnotationType() == annotationType) { //$IDENTITY-COMPARISON$
				if (repackagedBindings == annotations)
					System.arraycopy(repackagedBindings, 0, repackagedBindings = new AnnotationBinding[length], 0, length);
				repackagedBindings[j] = null; // so it is not double packed.
				if (containees == null) {
					containees = new ArrayList<AnnotationBinding>();
					containees.add(annotation);
				}
				containees.add(otherAnnotation);
			}
		}
		if (containees != null) {
			ElementValuePair [] elementValuePairs = new ElementValuePair [] { new ElementValuePair(TypeConstants.VALUE, containees.toArray(), value) };
			repackagedBindings[i] = new AnnotationBinding(containerType, elementValuePairs);
		}
	}
	if (repackagedBindings == annotations)
		return annotations;
	
	int finalTally = 0;
	for (int i = 0; i < length; i++) {
		if (repackagedBindings[i] != null)
			finalTally++;
	}
	annotations = new AnnotationBinding [finalTally];
	for (int i = 0, j = 0; i < length; i++) {
		if (repackagedBindings[i] != null)
			annotations[j++] = repackagedBindings[i];
	}
	return annotations;
}
 
Example 8
Source File: Factory.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public static AnnotationBinding [] getUnpackedAnnotationBindings(AnnotationBinding [] annotations) {
	
	int length = annotations == null ? 0 : annotations.length;
	if (length == 0)
		return annotations;
	
	List<AnnotationBinding> unpackedAnnotations = new ArrayList<AnnotationBinding>();
	for (int i = 0; i < length; i++) {
		AnnotationBinding annotation = annotations[i];
		if (annotation == null) continue;
		unpackedAnnotations.add(annotation);
		ReferenceBinding annotationType = annotation.getAnnotationType();
		
		MethodBinding [] values = annotationType.getMethods(TypeConstants.VALUE);
		if (values == null || values.length != 1)
			continue;
		MethodBinding value = values[0];
		
		if (value.returnType.dimensions() != 1)
			continue;
		
		TypeBinding containeeType = value.returnType.leafComponentType();
		if (containeeType == null || !containeeType.isAnnotationType() || !containeeType.isRepeatableAnnotationType())
			continue;
		
		if (containeeType.containerAnnotationType() != annotationType) //$IDENTITY-COMPARISON$
			continue;
		
		// We have a kosher container: unwrap the contained annotations.
		ElementValuePair [] elementValuePairs = annotation.getElementValuePairs();
		for (ElementValuePair elementValuePair : elementValuePairs) {
			if (CharOperation.equals(elementValuePair.getName(), TypeConstants.VALUE)) {
				Object [] containees = (Object []) elementValuePair.getValue();
				for (Object object : containees) {
					unpackedAnnotations.add((AnnotationBinding) object);
				}
				break;
			}
		}
	}
	return (AnnotationBinding[]) unpackedAnnotations.toArray(new AnnotationBinding [unpackedAnnotations.size()]);
}