当前位置: 首页>>代码示例>>Java>>正文


Java TypeBinding.isAnnotationType方法代码示例

本文整理汇总了Java中org.eclipse.jdt.internal.compiler.lookup.TypeBinding.isAnnotationType方法的典型用法代码示例。如果您正苦于以下问题:Java TypeBinding.isAnnotationType方法的具体用法?Java TypeBinding.isAnnotationType怎么用?Java TypeBinding.isAnnotationType使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.eclipse.jdt.internal.compiler.lookup.TypeBinding的用法示例。


在下文中一共展示了TypeBinding.isAnnotationType方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: getUnpackedAnnotationBindings

import org.eclipse.jdt.internal.compiler.lookup.TypeBinding; //导入方法依赖的package包/类
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()]);
}
 
开发者ID:trylimits,项目名称:Eclipse-Postfix-Code-Completion,代码行数:43,代码来源:Factory.java

示例2: convertJDTValueToReflectionType

import org.eclipse.jdt.internal.compiler.lookup.TypeBinding; //导入方法依赖的package包/类
/**
 * Convert a JDT annotation value as obtained from ElementValuePair.getValue()
 * (e.g., IntConstant, FieldBinding, etc.) to the type expected by a reflective
 * method invocation (e.g., int, an enum constant, etc.).
 * @return a value of type {@code expectedType}, or a dummy value of that type if
 * the actual value cannot be converted.
 */
private Object convertJDTValueToReflectionType(Object jdtValue, TypeBinding actualType, Class<?> expectedType) {
	if (expectedType.isPrimitive() || String.class.equals(expectedType)) {
		if (jdtValue instanceof Constant) {
			if (boolean.class.equals(expectedType)) {
				return ((Constant)jdtValue).booleanValue();
			}
			else if (byte.class.equals(expectedType)) {
				return ((Constant)jdtValue).byteValue();
			}
			else if (char.class.equals(expectedType)) {
				return ((Constant)jdtValue).charValue();
			}
			else if (double.class.equals(expectedType)) {
				return ((Constant)jdtValue).doubleValue();
			}
			else if (float.class.equals(expectedType)) {
				return ((Constant)jdtValue).floatValue();
			}
			else if (int.class.equals(expectedType)) {
				return ((Constant)jdtValue).intValue();
			}
			else if (long.class.equals(expectedType)) {
				return ((Constant)jdtValue).longValue();
			}
			else if (short.class.equals(expectedType)) {
				return ((Constant)jdtValue).shortValue();
			}
			else if (String.class.equals(expectedType)) {
				return ((Constant)jdtValue).stringValue();
			}
		}
		// Primitive or string is expected, but our actual value cannot be coerced into one.
		// TODO: if the actual value is an array of primitives, should we unpack the first one?
		return Factory.getMatchingDummyValue(expectedType);
	}
	else if (expectedType.isEnum()) {
		Object returnVal = null;
        if (actualType != null && actualType.isEnum() && jdtValue instanceof FieldBinding) {
        	
        	FieldBinding binding = (FieldBinding)jdtValue;
        	try {
        		Field returnedField = null;
        		returnedField = expectedType.getField( new String(binding.name) );
        		if (null != returnedField) {
        			returnVal = returnedField.get(null);
        		}
        	}
        	catch (NoSuchFieldException nsfe) {
        		// return null
        	}
        	catch (IllegalAccessException iae) {
        		// return null
        	}
        }
        return null == returnVal ? Factory.getMatchingDummyValue(expectedType) : returnVal;
	}
	else if (expectedType.isAnnotation()) {
		// member value is expected to be an annotation type.  Wrap it in an Annotation proxy.
		if (actualType.isAnnotationType() && jdtValue instanceof AnnotationBinding) {
			AnnotationMirrorImpl annoMirror =
				(AnnotationMirrorImpl)_env.getFactory().newAnnotationMirror((AnnotationBinding)jdtValue);
			return Proxy.newProxyInstance(expectedType.getClassLoader(),
					new Class[]{ expectedType }, annoMirror );
		}
		else {
			// No way to cast a non-annotation value to an annotation type; return null to caller
			return null;
		}
	}
	else {
		return Factory.getMatchingDummyValue(expectedType);
	}
}
 
开发者ID:trylimits,项目名称:Eclipse-Postfix-Code-Completion,代码行数:81,代码来源:AnnotationMirrorImpl.java

示例3: resolveStatements

import org.eclipse.jdt.internal.compiler.lookup.TypeBinding; //导入方法依赖的package包/类
public void resolveStatements() {

		super.resolveStatements();
		if (this.arguments != null) {
			this.scope.problemReporter().annotationMembersCannotHaveParameters(this);
		}
		if (this.typeParameters != null) {
			this.scope.problemReporter().annotationMembersCannotHaveTypeParameters(this);
		}
		if (this.extendedDimensions != 0) {
			this.scope.problemReporter().illegalExtendedDimensions(this);
		}
		if (this.binding == null) return;
		TypeBinding returnTypeBinding = this.binding.returnType;
		if (returnTypeBinding != null) {

			// annotation methods can only return base types, String, Class, enum type, annotation types and arrays of these
			checkAnnotationMethodType: {
				TypeBinding leafReturnType = returnTypeBinding.leafComponentType();
				if (returnTypeBinding.dimensions() <= 1) { // only 1-dimensional array permitted
					switch (leafReturnType.erasure().id) {
						case T_byte :
						case T_short :
						case T_char :
						case T_int :
						case T_long :
						case T_float :
						case T_double :
						case T_boolean :
						case T_JavaLangString :
						case T_JavaLangClass :
							break checkAnnotationMethodType;
					}
					if (leafReturnType.isEnum() || leafReturnType.isAnnotationType())
						break checkAnnotationMethodType;
				}
				this.scope.problemReporter().invalidAnnotationMemberType(this);
			}
			if (this.defaultValue != null) {
				MemberValuePair pair = new MemberValuePair(this.selector, this.sourceStart, this.sourceEnd, this.defaultValue);
				pair.binding = this.binding;
				pair.resolveTypeExpecting(this.scope, returnTypeBinding);
				this.binding.setDefaultValue(org.eclipse.jdt.internal.compiler.lookup.ElementValuePair.getValue(this.defaultValue));
			} else { // let it know it does not have a default value so it won't try to find it
				this.binding.setDefaultValue(null);
			}
		}
	}
 
开发者ID:trylimits,项目名称:Eclipse-Postfix-Code-Completion,代码行数:49,代码来源:AnnotationMethodDeclaration.java


注:本文中的org.eclipse.jdt.internal.compiler.lookup.TypeBinding.isAnnotationType方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。