Java Code Examples for org.eclipse.jdt.internal.compiler.lookup.TypeBinding#isEnum()

The following examples show how to use org.eclipse.jdt.internal.compiler.lookup.TypeBinding#isEnum() . 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: PatchExtensionMethod.java    From EasyMPermission with MIT License 6 votes vote down vote up
static List<Extension> getApplicableExtensionMethods(EclipseNode typeNode, Annotation ann, TypeBinding receiverType) {
	List<Extension> extensions = new ArrayList<Extension>();
	if ((typeNode != null) && (ann != null) && (receiverType != null)) {
		BlockScope blockScope = ((TypeDeclaration) typeNode.get()).initializerScope;
		EclipseNode annotationNode = typeNode.getNodeFor(ann);
		AnnotationValues<ExtensionMethod> annotation = createAnnotation(ExtensionMethod.class, annotationNode);
		boolean suppressBaseMethods = false;
		try {
			suppressBaseMethods = annotation.getInstance().suppressBaseMethods();
		} catch (AnnotationValueDecodeFail fail) {
			fail.owner.setError(fail.getMessage(), fail.idx);
		}
		for (Object extensionMethodProvider : annotation.getActualExpressions("value")) {
			if (extensionMethodProvider instanceof ClassLiteralAccess) {
				TypeBinding binding = ((ClassLiteralAccess) extensionMethodProvider).type.resolveType(blockScope);
				if (binding == null) continue;
				if (!binding.isClass() && !binding.isEnum()) continue;
				Extension e = new Extension();
				e.extensionMethods = getApplicableExtensionMethodsDefinedInProvider(typeNode, (ReferenceBinding) binding, receiverType);
				e.suppressBaseMethods = suppressBaseMethods;
				extensions.add(e);
			}
		}
	}
	return extensions;
}
 
Example 2
Source File: CodeSnippetQualifiedNameReference.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Check and/or redirect the field access to the delegate receiver if any
 */
public TypeBinding checkFieldAccess(BlockScope scope) {
	FieldBinding fieldBinding = (FieldBinding) this.binding;
	MethodScope methodScope = scope.methodScope();
	TypeBinding declaringClass = fieldBinding.original().declaringClass;
	// check for forward references
	if ((this.indexOfFirstFieldBinding == 1 || declaringClass.isEnum())
			&& TypeBinding.equalsEquals(methodScope.enclosingSourceType(), declaringClass)
			&& methodScope.lastVisibleFieldID >= 0
			&& fieldBinding.id >= methodScope.lastVisibleFieldID
			&& (!fieldBinding.isStatic() || methodScope.isStatic)) {
		scope.problemReporter().forwardReference(this, this.indexOfFirstFieldBinding-1, fieldBinding);
	}
	this.bits &= ~ASTNode.RestrictiveFlagMASK; // clear bits
	this.bits |= Binding.FIELD;
	return getOtherFieldBindings(scope);
}
 
Example 3
Source File: AnnotationValueImpl.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Convert the JDT representation of a single constant into its javax.lang.model
 * representation.  For instance, convert a StringConstant into a String, or
 * a FieldBinding into a VariableElement.  This does not handle the case where
 * value is an Object[].
 * @param value the JDT object
 * @param type the return type of the annotation member.  If null or not a
 * BaseTypeBinding, this is ignored and the value is inspected to determine type.
 * @param kind an int array whose first element will be set to the type of the
 * converted object, represented with T_* values from TypeIds or from this class.
 * @return
 */
private Object convertToMirrorType(Object value, TypeBinding type, int kind[]) {
	if (type == null) {
		kind[0] = TypeIds.T_JavaLangString;
		return "<error>"; //$NON-NLS-1$
	} else if (type instanceof BaseTypeBinding || type.id == TypeIds.T_JavaLangString) {
		if (value == null) {
			if (type instanceof BaseTypeBinding
					|| type.id == TypeIds.T_JavaLangString) {
				// return a string with error in it to reflect a value that could not be resolved
				kind[0] = TypeIds.T_JavaLangString;
				return "<error>"; //$NON-NLS-1$
			} else if (type.isAnnotationType()) {
				kind[0] = T_AnnotationMirror;
				return _env.getFactory().newAnnotationMirror(null);
			}
		} else if (value instanceof Constant) {
			if (type instanceof BaseTypeBinding) {
				kind[0] = ((BaseTypeBinding)type).id;
			}
			else if (type.id == TypeIds.T_JavaLangString) {
				kind[0] = ((Constant)value).typeID();
			} else {
				// error case
				kind[0] = TypeIds.T_JavaLangString;
				return "<error>"; //$NON-NLS-1$
			}
			switch (kind[0]) {
			case T_boolean:
				return ((Constant)value).booleanValue();
			case T_byte:
				return ((Constant)value).byteValue();
			case T_char:
				return ((Constant)value).charValue();
			case T_double:
				return ((Constant)value).doubleValue();
			case T_float:
				return ((Constant)value).floatValue();
			case T_int:
				try {
					if (value instanceof LongConstant
							|| value instanceof DoubleConstant
							|| value instanceof FloatConstant) {
						// error case
						kind[0] = TypeIds.T_JavaLangString;
						return "<error>"; //$NON-NLS-1$
					}
					return ((Constant)value).intValue();
				} catch (ShouldNotImplement e) {
					kind[0] = TypeIds.T_JavaLangString;
					return "<error>"; //$NON-NLS-1$
				}
			case T_JavaLangString:
				return ((Constant)value).stringValue();
			case T_long:
				return ((Constant)value).longValue();
			case T_short:
				return ((Constant)value).shortValue();
			}
		}
	} else if (type.isEnum()) {
		if (value instanceof FieldBinding) {
			kind[0] = T_EnumConstant;
			return (VariableElement) _env.getFactory().newElement((FieldBinding) value);
		} else {
			kind[0] = TypeIds.T_JavaLangString;
			return "<error>"; //$NON-NLS-1$
		}
	} else if (type.isAnnotationType()) {
		if (value instanceof AnnotationBinding) {
			kind[0] = T_AnnotationMirror;
			return _env.getFactory().newAnnotationMirror((AnnotationBinding) value);
		}
	} else if (value instanceof TypeBinding) {
		kind[0] = T_ClassObject;
		return _env.getFactory().newTypeMirror((TypeBinding) value);
	}
	// error case
	kind[0] = TypeIds.T_JavaLangString;
	return "<error>"; //$NON-NLS-1$
}
 
Example 4
Source File: AnnotationMirrorImpl.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * 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);
	}
}
 
Example 5
Source File: AnnotationMethodDeclaration.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
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);
			}
		}
	}
 
Example 6
Source File: CaseStatement.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Returns the constant intValue or ordinal for enum constants. If constant is NotAConstant, then answers Float.MIN_VALUE
 * @see org.eclipse.jdt.internal.compiler.ast.Statement#resolveCase(org.eclipse.jdt.internal.compiler.lookup.BlockScope, org.eclipse.jdt.internal.compiler.lookup.TypeBinding, org.eclipse.jdt.internal.compiler.ast.SwitchStatement)
 */
public Constant resolveCase(BlockScope scope, TypeBinding switchExpressionType, SwitchStatement switchStatement) {
	// switchExpressionType maybe null in error case
	scope.enclosingCase = this; // record entering in a switch case block

	if (this.constantExpression == null) {
		// remember the default case into the associated switch statement
		if (switchStatement.defaultCase != null)
			scope.problemReporter().duplicateDefaultCase(this);

		// on error the last default will be the selected one ...
		switchStatement.defaultCase = this;
		return Constant.NotAConstant;
	}
	// add into the collection of cases of the associated switch statement
	switchStatement.cases[switchStatement.caseCount++] = this;
	// tag constant name with enum type for privileged access to its members
	if (switchExpressionType != null && switchExpressionType.isEnum() && (this.constantExpression instanceof SingleNameReference)) {
		((SingleNameReference) this.constantExpression).setActualReceiverType((ReferenceBinding)switchExpressionType);
	}
	TypeBinding caseType = this.constantExpression.resolveType(scope);
	if (caseType == null || switchExpressionType == null) return Constant.NotAConstant;
	if (this.constantExpression.isConstantValueOfTypeAssignableToType(caseType, switchExpressionType)
			|| caseType.isCompatibleWith(switchExpressionType)) {
		if (caseType.isEnum()) {
			if (((this.constantExpression.bits & ASTNode.ParenthesizedMASK) >> ASTNode.ParenthesizedSHIFT) != 0) {
				scope.problemReporter().enumConstantsCannotBeSurroundedByParenthesis(this.constantExpression);
			}

			if (this.constantExpression instanceof NameReference
					&& (this.constantExpression.bits & ASTNode.RestrictiveFlagMASK) == Binding.FIELD) {
				NameReference reference = (NameReference) this.constantExpression;
				FieldBinding field = reference.fieldBinding();
				if ((field.modifiers & ClassFileConstants.AccEnum) == 0) {
					 scope.problemReporter().enumSwitchCannotTargetField(reference, field);
				} else 	if (reference instanceof QualifiedNameReference) {
					 scope.problemReporter().cannotUseQualifiedEnumConstantInCaseLabel(reference, field);
				}
				return IntConstant.fromValue(field.original().id + 1); // (ordinal value + 1) zero should not be returned see bug 141810
			}
		} else {
			return this.constantExpression.constant;
		}
	} else if (isBoxingCompatible(caseType, switchExpressionType, this.constantExpression, scope)) {
		// constantExpression.computeConversion(scope, caseType, switchExpressionType); - do not report boxing/unboxing conversion
		return this.constantExpression.constant;
	}
	scope.problemReporter().typeMismatchError(caseType, switchExpressionType, this.constantExpression, switchStatement.expression);
	return Constant.NotAConstant;
}