Java Code Examples for org.eclipse.jdt.core.dom.IVariableBinding#isEnumConstant()

The following examples show how to use org.eclipse.jdt.core.dom.IVariableBinding#isEnumConstant() . 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: InlineConstantRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private Name findConstantNameNode() {
	ASTNode node= NodeFinder.perform(fSelectionCuRewrite.getRoot(), fSelectionStart, fSelectionLength);
	if (node == null)
		return null;
	if (node instanceof FieldAccess)
		node= ((FieldAccess) node).getName();
	if (!(node instanceof Name))
		return null;
	Name name= (Name) node;
	IBinding binding= name.resolveBinding();
	if (!(binding instanceof IVariableBinding))
		return null;
	IVariableBinding variableBinding= (IVariableBinding) binding;
	if (!variableBinding.isField() || variableBinding.isEnumConstant())
		return null;
	int modifiers= binding.getModifiers();
	if (! (Modifier.isStatic(modifiers) && Modifier.isFinal(modifiers)))
		return null;

	return name;
}
 
Example 2
Source File: ScopeAnalyzer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public boolean visit(SwitchCase node) {
	// switch on enum allows to use enum constants without qualification
	if (hasFlag(VARIABLES, fFlags) && !node.isDefault() && isInside(node.getExpression())) {
		SwitchStatement switchStatement= (SwitchStatement) node.getParent();
		ITypeBinding binding= switchStatement.getExpression().resolveTypeBinding();
		if (binding != null && binding.isEnum()) {
			IVariableBinding[] declaredFields= binding.getDeclaredFields();
			for (int i= 0; i < declaredFields.length; i++) {
				IVariableBinding curr= declaredFields[i];
				if (curr.isEnumConstant()) {
					fBreak= fRequestor.acceptBinding(curr);
					if (fBreak)
						return false;
				}
			}
		}
	}
	return false;
}
 
Example 3
Source File: JdtBasedTypeFactory.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * @since 2.4
 */
protected JvmField createField(StringBuilder typeName, IVariableBinding field) {
	JvmField result;
	if (!field.isEnumConstant()) {
		result = TypesFactory.eINSTANCE.createJvmField();
		Object constantValue = field.getConstantValue();
		if (constantValue != null) {
			result.setConstant(true);
			result.setConstantValue(constantValue);
		} else {
			result.setConstant(false);
		}
	} else
		result = TypesFactory.eINSTANCE.createJvmEnumerationLiteral();
	String name = field.getName();
	result.internalSetIdentifier(typeName.append(name).toString());
	result.setSimpleName(name);
	int modifiers = field.getModifiers();
	result.setFinal(Modifier.isFinal(modifiers));
	result.setStatic(Modifier.isStatic(modifiers));
	result.setTransient(Modifier.isTransient(modifiers));
	result.setVolatile(Modifier.isVolatile(modifiers));
	result.setDeprecated(field.isDeprecated());
	setVisibility(result, modifiers);
	result.setType(createTypeReference(field.getType()));
	createAnnotationValues(field, result);
	return result;
}
 
Example 4
Source File: InJavaImporter.java    From jdt2famix with Eclipse Public License 1.0 5 votes vote down vote up
public StructuralEntity ensureStructuralEntityFromExpression(Expression expression) {
	if (expression instanceof SimpleName) {
		IBinding simpleNameBinding = ((SimpleName) expression).resolveBinding();
		if (simpleNameBinding instanceof IVariableBinding) {
			IVariableBinding binding = ((IVariableBinding) simpleNameBinding).getVariableDeclaration();
			if (binding.isField())
				return ensureAttributeForVariableBinding(binding);
			if (binding.isParameter())
				return ensureParameterWithinCurrentMethodFromVariableBinding(binding);
			if (binding.isEnumConstant())
				return ensureEnumValueFromVariableBinding(binding);
		}
	}
	return null;
}
 
Example 5
Source File: InJavaImporter.java    From jdt2famix with Eclipse Public License 1.0 5 votes vote down vote up
private Access createAccessFromVariableBinding(IVariableBinding binding, ASTNode node) {
	Access access = new Access();
	StructuralEntity variable = unknownVariable();
	if (binding != null) {
		/*
		 * It sometimes happen that the binding is null. Unfortunately, I was not able
		 * to isolate and reproduce the case, but we still need the guard condition.
		 */
		boolean isField = binding.isField();
		boolean isParameter = binding.isParameter();
		boolean isEnumConstant = binding.isEnumConstant();
		if (!isField && !isParameter && !isEnumConstant)
			// we only consider fields, parameters and enum constants
			return access;
		if (isField)
			variable = ensureAttributeForVariableBinding(binding);
		if (isParameter)
			variable = ensureParameterWithinCurrentMethodFromVariableBinding(binding);
		if (isEnumConstant)
			variable = ensureEnumValueFromVariableBinding(binding);
	}
	access.setVariable(variable);
	access.setIsWrite(false);
	if (topOfContainerStack() instanceof Method)
		access.setAccessor((Method) topOfContainerStack());
	if (topOfContainerStack() instanceof Type)
		/*
		 * This is ugly, but it happens when we have an access from within an annotation
		 * around a type or attribute like:
		 * 
		 * @Annotation(name="something" + AClass.DEFAULT)
		 */
		access.setAccessor(ensureInitializerMethod());
	createLightweightSourceAnchor(access, node);
	repository.add(access);
	return access;
}
 
Example 6
Source File: Checks.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static boolean isEnumCase(ASTNode node) {
	if (node instanceof SwitchCase) {
		final SwitchCase caze= (SwitchCase) node;
		final Expression expression= caze.getExpression();
		if (expression instanceof Name) {
			final Name name= (Name) expression;
			final IBinding binding= name.resolveBinding();
			if (binding instanceof IVariableBinding) {
				IVariableBinding variableBinding= (IVariableBinding) binding;
				return variableBinding.isEnumConstant();
			}
		}
	}
	return false;
}
 
Example 7
Source File: ConvertAnonymousToNestedRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private void addArgumentsForLocalsUsedInInnerClass(ClassInstanceCreation newClassCreation) {
     IVariableBinding[] usedLocals= getUsedLocalVariables();
     for (int i= 0; i < usedLocals.length; i++) {
         final AST ast= fAnonymousInnerClassNode.getAST();
final IVariableBinding binding= usedLocals[i];
Name name= null;
if (binding.isEnumConstant())
	name= ast.newQualifiedName(ast.newSimpleName(binding.getDeclaringClass().getName()), ast.newSimpleName(binding.getName()));
else
	name= ast.newSimpleName(binding.getName());
newClassCreation.arguments().add(name);
     }
 }
 
Example 8
Source File: ScopeAnalyzer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private IVariableBinding[] getEnumContants(ITypeBinding binding) {
	IVariableBinding[] declaredFields= binding.getDeclaredFields();
	ArrayList<IVariableBinding> res= new ArrayList<IVariableBinding>(declaredFields.length);
	for (int i= 0; i < declaredFields.length; i++) {
		IVariableBinding curr= declaredFields[i];
		if (curr.isEnumConstant()) {
			res.add(curr);
		}
	}
	return res.toArray(new IVariableBinding[res.size()]);
}
 
Example 9
Source File: StubUtility2.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static DelegateEntry[] getDelegatableMethods(ITypeBinding binding) {
	final List<DelegateEntry> tuples= new ArrayList<DelegateEntry>();
	final List<IMethodBinding> declared= new ArrayList<IMethodBinding>();
	IMethodBinding[] typeMethods= binding.getDeclaredMethods();
	for (int index= 0; index < typeMethods.length; index++)
		declared.add(typeMethods[index]);
	IVariableBinding[] typeFields= binding.getDeclaredFields();
	for (int index= 0; index < typeFields.length; index++) {
		IVariableBinding fieldBinding= typeFields[index];
		if (fieldBinding.isField() && !fieldBinding.isEnumConstant() && !fieldBinding.isSynthetic())
			getDelegatableMethods(new ArrayList<IMethodBinding>(declared), fieldBinding, fieldBinding.getType(), binding, tuples);
	}
	// list of tuple<IVariableBinding, IMethodBinding>
	return tuples.toArray(new DelegateEntry[tuples.size()]);
}
 
Example 10
Source File: CodeStyleFix.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private void handleVariable(SimpleName node, IVariableBinding varbinding) {
	if (!varbinding.isField())
		return;

	if (varbinding.isEnumConstant())
		return;

	ITypeBinding declaringClass= varbinding.getDeclaringClass();
	if (Modifier.isStatic(varbinding.getModifiers())) {
		if (fFindUnqualifiedStaticAccesses) {
			Initializer initializer= (Initializer) ASTNodes.getParent(node, Initializer.class);
			//Do not qualify assignments to static final fields in static initializers (would result in compile error)
			StructuralPropertyDescriptor parentDescription= node.getLocationInParent();
			if (initializer != null && Modifier.isStatic(initializer.getModifiers())
					&& Modifier.isFinal(varbinding.getModifiers()) && parentDescription == Assignment.LEFT_HAND_SIDE_PROPERTY)
				return;

			//Do not qualify static fields if defined inside an anonymous class
			if (declaringClass.isAnonymous())
				return;

			fResult.add(new AddStaticQualifierOperation(declaringClass, node));
		}
	} else if (fFindUnqualifiedAccesses){
		String qualifier= getThisExpressionQualifier(declaringClass, fImportRewrite, node);
		if (qualifier == null)
			return;

		if (qualifier.length() == 0)
			qualifier= null;

		fResult.add(new AddThisQualifierOperation(qualifier, node));
	}
}
 
Example 11
Source File: BindingLabelProvider.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static ImageDescriptor getFieldImageDescriptor(IVariableBinding binding) {
	final int modifiers= binding.getModifiers();
	if (Modifier.isPublic(modifiers) || binding.isEnumConstant())
		return JavaPluginImages.DESC_FIELD_PUBLIC;
	if (Modifier.isProtected(modifiers))
		return JavaPluginImages.DESC_FIELD_PROTECTED;
	if (Modifier.isPrivate(modifiers))
		return JavaPluginImages.DESC_FIELD_PRIVATE;

	return JavaPluginImages.DESC_FIELD_DEFAULT;
}
 
Example 12
Source File: JdtUtils.java    From j2cl with Apache License 2.0 4 votes vote down vote up
public static FieldDescriptor createFieldDescriptor(IVariableBinding variableBinding) {
  checkArgument(!isArrayLengthBinding(variableBinding));

  boolean isStatic = isStatic(variableBinding);
  Visibility visibility = getVisibility(variableBinding);
  DeclaredTypeDescriptor enclosingTypeDescriptor =
      createDeclaredTypeDescriptor(variableBinding.getDeclaringClass());
  String fieldName = variableBinding.getName();

  TypeDescriptor thisTypeDescriptor =
      createTypeDescriptorWithNullability(
          variableBinding.getType(), variableBinding.getAnnotations());

  if (variableBinding.isEnumConstant()) {
    // Enum fields are always non-nullable.
    thisTypeDescriptor = thisTypeDescriptor.toNonNullable();
  }

  FieldDescriptor declarationFieldDescriptor = null;
  if (variableBinding.getVariableDeclaration() != variableBinding) {
    declarationFieldDescriptor = createFieldDescriptor(variableBinding.getVariableDeclaration());
  }

  JsInfo jsInfo = JsInteropUtils.getJsInfo(variableBinding);
  boolean isCompileTimeConstant = variableBinding.getConstantValue() != null;
  boolean isFinal = JdtUtils.isFinal(variableBinding);
  return FieldDescriptor.newBuilder()
      .setEnclosingTypeDescriptor(enclosingTypeDescriptor)
      .setName(fieldName)
      .setTypeDescriptor(thisTypeDescriptor)
      .setStatic(isStatic)
      .setVisibility(visibility)
      .setJsInfo(jsInfo)
      .setFinal(isFinal)
      .setCompileTimeConstant(isCompileTimeConstant)
      .setDeclarationDescriptor(declarationFieldDescriptor)
      .setEnumConstant(variableBinding.isEnumConstant())
      .setUnusableByJsSuppressed(
          JsInteropAnnotationUtils.isUnusableByJsSuppressed(variableBinding))
      .setDeprecated(isDeprecated(variableBinding))
      .build();
}