Java Code Examples for org.eclipse.jdt.core.dom.Modifier#isFinal()

The following examples show how to use org.eclipse.jdt.core.dom.Modifier#isFinal() . 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: InJavaImporter.java    From jdt2famix with Eclipse Public License 1.0 6 votes vote down vote up
public Parameter ensureParameterFromSingleVariableDeclaration(SingleVariableDeclaration variableDeclaration,
		Method method) {
	String name = variableDeclaration.getName().toString();
	String qualifiedName = Famix.qualifiedNameOf(method) + NAME_SEPARATOR + name;
	if (parameters.has(qualifiedName))
		return parameters.named(qualifiedName);
	Parameter parameter = new Parameter();
	parameters.add(qualifiedName, parameter);
	parameter.setName(name);
	parameter.setParentBehaviouralEntity(method);
	parameter.setDeclaredType(ensureTypeFromDomType(variableDeclaration.getType()));
	IVariableBinding binding = variableDeclaration.resolveBinding();
	if (binding != null) {
		// We only recover the final modifier
		if (Modifier.isFinal(binding.getModifiers()))
			parameter.addModifiers("final");
	}
	return parameter;
}
 
Example 2
Source File: FinalParametersQuickFix.java    From vscode-checkstyle with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public ASTVisitor getCorrectingASTVisitor(IRegion lineInfo, int markerStartOffset) {
    return new ASTVisitor() {

        @SuppressWarnings("unchecked")
        @Override
        public boolean visit(SingleVariableDeclaration node) {
            if (containsPosition(node, markerStartOffset) && !Modifier.isFinal(node.getModifiers())) {
                if (!Modifier.isFinal(node.getModifiers())) {
                    final Modifier finalModifier = node.getAST().newModifier(ModifierKeyword.FINAL_KEYWORD);
                    node.modifiers().add(finalModifier);
                }
            }
            return true;
        }
    };
}
 
Example 3
Source File: CallInliner.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public boolean visit(SimpleName node) {
	IBinding binding= node.resolveBinding();
	if (binding instanceof IVariableBinding) {
		int accessMode = fFormalArgument.getSimplifiedAccessMode();
		if (fFormalArgument.isFinal() && !Modifier.isFinal(binding.getModifiers())) {
			return setResult(false);
		}
		if (accessMode == FlowInfo.READ || accessMode == FlowInfo.UNUSED)
			return setResult(true);
		// from now on we only have write accesses.
		IVariableBinding vb= (IVariableBinding)binding;
		if (vb.isField())
			return setResult(false);
		return setResult(fFlowInfo.hasAccessMode(fFlowContext, vb, FlowInfo.UNUSED | FlowInfo.WRITE));
	}
	return setResult(false);
}
 
Example 4
Source File: ConstantChecks.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private boolean visitName(Name name) {
	IBinding binding= name.resolveBinding();
	if(binding == null) {
		/* If the binding is null because of compile errors etc.,
		   scenarios which may have been deemed unacceptable in
		   the presence of semantic information will be admitted.
		   Descend deeper.
		 */
		 return true;
	}

	int modifiers= binding.getModifiers();
	if(binding instanceof IVariableBinding) {
		if (!(Modifier.isStatic(modifiers) && Modifier.isFinal(modifiers))) {
			fResult= false;
			return false;
		}
	} else if(binding instanceof IMethodBinding) {
		if (!Modifier.isStatic(modifiers)) {
			fResult= false;
			return false;
		}
	} else if(binding instanceof ITypeBinding) {
		return false; // It's o.k.  Don't descend deeper.

	} else {
		return false; // e.g. a NameQualifiedType's qualifier, which can be a package binding
	}

	//Descend deeper:
	return true;
}
 
Example 5
Source File: VariableDeclarationFix.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static boolean canAddFinal(IBinding binding, ASTNode declNode) {
	if (!(binding instanceof IVariableBinding))
		return false;

	IVariableBinding varbinding= (IVariableBinding)binding;
	int modifiers= varbinding.getModifiers();
	if (Modifier.isFinal(modifiers) || Modifier.isVolatile(modifiers) || Modifier.isTransient(modifiers))
		return false;

	ASTNode parent= ASTNodes.getParent(declNode, VariableDeclarationExpression.class);
	if (parent != null && ((VariableDeclarationExpression)parent).fragments().size() > 1)
		return false;

	if (varbinding.isField() && !Modifier.isPrivate(modifiers))
		return false;

	if (varbinding.isParameter()) {
		ASTNode varDecl= declNode.getParent();
		if (varDecl instanceof MethodDeclaration) {
			MethodDeclaration declaration= (MethodDeclaration)varDecl;
			if (declaration.getBody() == null)
				return false;
		}
	}

	return true;
}
 
Example 6
Source File: InJavaImporter.java    From jdt2famix with Eclipse Public License 1.0 5 votes vote down vote up
private void extractBasicModifiersFromBinding(int modifiers, NamedEntity entity) {
	Boolean publicModifier = Modifier.isPublic(modifiers);
	Boolean protectedModifier = Modifier.isProtected(modifiers);
	Boolean privateModifier = Modifier.isPrivate(modifiers);
	if (publicModifier)
		entity.addModifiers("public");
	if (protectedModifier)
		entity.addModifiers("protected");
	if (privateModifier)
		entity.addModifiers("private");
	if (!(publicModifier || protectedModifier || privateModifier))
		entity.addModifiers("package");
	if (Modifier.isFinal(modifiers))
		entity.addModifiers("final");
	if (Modifier.isAbstract(modifiers))
		entity.addModifiers("abstract");
	if (Modifier.isNative(modifiers))
		entity.addModifiers("native");
	if (Modifier.isSynchronized(modifiers))
		entity.addModifiers("synchronized");
	if (Modifier.isTransient(modifiers))
		entity.addModifiers("transient");
	if (Modifier.isVolatile(modifiers))
		entity.addModifiers("volatile");
	/*
	 * We do not extract the static modifier here because we want to set the
	 * hasClassScope property and we do that specifically only for attributes and
	 * methods
	 */
}
 
Example 7
Source File: GenerateToStringAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public ToStringInfo(ITypeBinding typeBinding) {
	IMethodBinding[] declaredMethods= typeBinding.getDeclaredMethods();

	for (int i= 0; i < declaredMethods.length; i++) {
		if (declaredMethods[i].getName().equals(METHODNAME_TO_STRING) && declaredMethods[i].getParameterTypes().length == 0) {
			this.foundToString= true;
			if (Modifier.isFinal(declaredMethods[i].getModifiers()))
				this.foundFinalToString= true;
		}
	}
}
 
Example 8
Source File: GenerateHashCodeEqualsAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private HashCodeEqualsInfo getTypeInfo(ITypeBinding someType, boolean checkSuperclasses) {
	HashCodeEqualsInfo info= new HashCodeEqualsInfo();
	if (someType.isTypeVariable()) {
		someType= someType.getErasure();
	}
	
	while (true) {
		IMethodBinding[] declaredMethods= someType.getDeclaredMethods();

		for (int i= 0; i < declaredMethods.length; i++) {
			if (declaredMethods[i].getName().equals(METHODNAME_EQUALS)) {
				ITypeBinding[] b= declaredMethods[i].getParameterTypes();
				if ((b.length == 1) && (b[0].getQualifiedName().equals("java.lang.Object"))) { //$NON-NLS-1$
					info.foundEquals= true;
					if (Modifier.isFinal(declaredMethods[i].getModifiers()))
						info.foundFinalEquals= true;
				}
			}
			if (declaredMethods[i].getName().equals(METHODNAME_HASH_CODE) && declaredMethods[i].getParameterTypes().length == 0) {
				info.foundHashCode= true;
				if (Modifier.isFinal(declaredMethods[i].getModifiers()))
					info.foundFinalHashCode= true;
			}
			if (info.foundEquals && info.foundHashCode)
				break;
		}
		if (checkSuperclasses) {
			someType= someType.getSuperclass();
			if (someType == null || TypeRules.isJavaLangObject(someType)) {
				break;
			}
		} else {
			break;
		}
	}
	
	return info;
}
 
Example 9
Source File: NumberOfFields.java    From ck with Apache License 2.0 5 votes vote down vote up
@Override
public void visit(FieldDeclaration node) {
	fields++;
	fieldNames.addAll(getVariableName(node.fragments()));

	boolean isPublic = Modifier.isPublic(node.getModifiers());
	boolean isPrivate = Modifier.isPrivate(node.getModifiers());
	boolean isProtected = Modifier.isProtected(node.getModifiers());

	if(isPublic)
		publicFields++;
	else if(isPrivate)
		privateFields++;
	else if(isProtected)
		protectedFields++;
	else
		defaultFields++;

	// other characteristics rather than visibility
	boolean isStatic = Modifier.isStatic(node.getModifiers());
	boolean isFinal = Modifier.isFinal(node.getModifiers());
	boolean isSynchronized = Modifier.isSynchronized(node.getModifiers());
	
	if(isStatic)
		staticFields++;

	if(isFinal)
		finalFields++;

	if(isSynchronized)
		synchronizedFields++;

}
 
Example 10
Source File: ConstantChecks.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private boolean visitName(Name name) {
	IBinding binding = name.resolveBinding();
	if (binding == null) {
		/* If the binding is null because of compile errors etc.,
		   scenarios which may have been deemed unacceptable in
		   the presence of semantic information will be admitted.
		   Descend deeper.
		 */
		return true;
	}

	int modifiers = binding.getModifiers();
	if (binding instanceof IVariableBinding) {
		if (!(Modifier.isStatic(modifiers) && Modifier.isFinal(modifiers))) {
			fResult = false;
			return false;
		}
	} else if (binding instanceof IMethodBinding) {
		if (!Modifier.isStatic(modifiers)) {
			fResult = false;
			return false;
		}
	} else if (binding instanceof ITypeBinding) {
		return false; // It's o.k.  Don't descend deeper.

	} else {
		return false; // e.g. a NameQualifiedType's qualifier, which can be a package binding
	}

	//Descend deeper:
	return true;
}
 
Example 11
Source File: ASTNodes.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static int getOrderPreference(BodyDeclaration member, MembersOrderPreferenceCache store) {
	int memberType= member.getNodeType();
	int modifiers= member.getModifiers();

	switch (memberType) {
		case ASTNode.TYPE_DECLARATION:
		case ASTNode.ENUM_DECLARATION :
		case ASTNode.ANNOTATION_TYPE_DECLARATION :
			return store.getCategoryIndex(MembersOrderPreferenceCache.TYPE_INDEX) * 2;
		case ASTNode.FIELD_DECLARATION:
			if (Modifier.isStatic(modifiers)) {
				int index= store.getCategoryIndex(MembersOrderPreferenceCache.STATIC_FIELDS_INDEX) * 2;
				if (Modifier.isFinal(modifiers)) {
					return index; // first final static, then static
				}
				return index + 1;
			}
			return store.getCategoryIndex(MembersOrderPreferenceCache.FIELDS_INDEX) * 2;
		case ASTNode.INITIALIZER:
			if (Modifier.isStatic(modifiers)) {
				return store.getCategoryIndex(MembersOrderPreferenceCache.STATIC_INIT_INDEX) * 2;
			}
			return store.getCategoryIndex(MembersOrderPreferenceCache.INIT_INDEX) * 2;
		case ASTNode.ANNOTATION_TYPE_MEMBER_DECLARATION:
			return store.getCategoryIndex(MembersOrderPreferenceCache.METHOD_INDEX) * 2;
		case ASTNode.METHOD_DECLARATION:
			if (Modifier.isStatic(modifiers)) {
				return store.getCategoryIndex(MembersOrderPreferenceCache.STATIC_METHODS_INDEX) * 2;
			}
			if (((MethodDeclaration) member).isConstructor()) {
				return store.getCategoryIndex(MembersOrderPreferenceCache.CONSTRUCTORS_INDEX) * 2;
			}
			return store.getCategoryIndex(MembersOrderPreferenceCache.METHOD_INDEX) * 2;
		default:
			return 100;
	}
}
 
Example 12
Source File: DesignForExtensionQuickFix.java    From vscode-checkstyle with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public ASTVisitor getCorrectingASTVisitor(IRegion lineInfo, int markerStartOffset) {
    return new ASTVisitor() {

        @SuppressWarnings("unchecked")
        @Override
        public boolean visit(MethodDeclaration node) {
            // recalculate start position because optional javadoc is mixed
            // into the original start position
            final int pos = node.getStartPosition() +
                    (node.getJavadoc() != null ? node.getJavadoc().getLength() + JAVADOC_COMMENT_LENGTH : 0);
            if (containsPosition(lineInfo, pos)) {

                if (!Modifier.isFinal(node.getModifiers())) {

                    final Modifier finalModifier = node.getAST().newModifier(ModifierKeyword.FINAL_KEYWORD);
                    node.modifiers().add(finalModifier);

                    // reorder modifiers into their correct order
                    final List<ASTNode> reorderedModifiers = ModifierOrderQuickFix
                            .reorderModifiers(node.modifiers());
                    node.modifiers().clear();
                    node.modifiers().addAll(reorderedModifiers);
                }
            }
            return true;
        }
    };
}
 
Example 13
Source File: BindingLabelProvider.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static int getAdornmentFlags(IBinding binding) {
	int adornments= 0;
	final int modifiers= binding.getModifiers();
	if (Modifier.isAbstract(modifiers))
		adornments|= JavaElementImageDescriptor.ABSTRACT;
	if (Modifier.isFinal(modifiers))
		adornments|= JavaElementImageDescriptor.FINAL;
	if (Modifier.isStatic(modifiers))
		adornments|= JavaElementImageDescriptor.STATIC;
	
	if (binding.isDeprecated())
		adornments|= JavaElementImageDescriptor.DEPRECATED;
	
	if (binding instanceof IMethodBinding) {
		if (((IMethodBinding) binding).isConstructor())
			adornments|= JavaElementImageDescriptor.CONSTRUCTOR;
		if (Modifier.isSynchronized(modifiers))
			adornments|= JavaElementImageDescriptor.SYNCHRONIZED;
		if (Modifier.isNative(modifiers))
			adornments|= JavaElementImageDescriptor.NATIVE;
		ITypeBinding type= ((IMethodBinding) binding).getDeclaringClass();
		if (type.isInterface() && !Modifier.isAbstract(modifiers) && !Modifier.isStatic(modifiers))
			adornments|= JavaElementImageDescriptor.DEFAULT_METHOD;
		if (((IMethodBinding) binding).getDefaultValue() != null)
			adornments|= JavaElementImageDescriptor.ANNOTATION_DEFAULT;
	}
	if (binding instanceof IVariableBinding && ((IVariableBinding) binding).isField()) {
		if (Modifier.isTransient(modifiers))
			adornments|= JavaElementImageDescriptor.TRANSIENT;
		if (Modifier.isVolatile(modifiers))
			adornments|= JavaElementImageDescriptor.VOLATILE;
	}
	return adornments;
}
 
Example 14
Source File: FinalClassQuickfix.java    From eclipse-cs with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
protected ASTVisitor handleGetCorrectingASTVisitor(final IRegion lineInfo,
        final int markerStartOffset) {
  return new ASTVisitor() {

    @SuppressWarnings("unchecked")
    @Override
    public boolean visit(TypeDeclaration node) {
      // recalculate start position because optional javadoc is mixed
      // into the original start position
      int pos = node.getStartPosition() + (node.getJavadoc() != null
              ? node.getJavadoc().getLength() + JAVADOC_COMMENT_LENGTH
              : 0);
      if (containsPosition(lineInfo, pos)) {

        if (!Modifier.isFinal(node.getModifiers())) {

          Modifier finalModifier = node.getAST().newModifier(ModifierKeyword.FINAL_KEYWORD);
          node.modifiers().add(finalModifier);

          // reorder modifiers into their correct order
          List<?> reorderedModifiers = ModifierOrderQuickfix.reOrderModifiers(node.modifiers());
          node.modifiers().clear();
          node.modifiers().addAll(reorderedModifiers);
        }
      }
      return true;
    }
  };
}
 
Example 15
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 16
Source File: JavaASTVisitor.java    From SnowGraph with Apache License 2.0 4 votes vote down vote up
private static boolean isFinal(FieldDeclaration decl) {
    int modifiers = decl.getModifiers();
    return (Modifier.isFinal(modifiers));
}
 
Example 17
Source File: JavaASTVisitor.java    From SnowGraph with Apache License 2.0 4 votes vote down vote up
private static boolean isFinal(SingleVariableDeclaration decl) {
    int modifiers = decl.getModifiers();
    return (Modifier.isFinal(modifiers));
}
 
Example 18
Source File: JdtUtils.java    From j2cl with Apache License 2.0 4 votes vote down vote up
private static boolean isFinal(IBinding binding) {
  return Modifier.isFinal(binding.getModifiers());
}
 
Example 19
Source File: ParameterData.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public boolean isFinal() {
	return Modifier.isFinal(fDeclaration.getModifiers());
}
 
Example 20
Source File: ParameterObjectFactory.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 2 votes vote down vote up
/**
 * Return whether the setter should be created for this field. This method is only called when
 * the global createSetters is set and the parameterInfo is marked for field creation.
 * @param pi the parameter info
 * @return <code>true</code> if a setter should be created
 */
public boolean isCreateSetter(ParameterInfo pi) {
	return !Modifier.isFinal(pi.getOldBinding().getModifiers());
}