Java Code Examples for org.eclipse.jdt.core.dom.Modifier#FINAL

The following examples show how to use org.eclipse.jdt.core.dom.Modifier#FINAL . 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: JavaStatementPostfixContext.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Adds a new field to the {@link AST} using the given type and variable name. The method
 * returns a {@link TextEdit} which can then be applied using the {@link #applyTextEdit(TextEdit)} method.
 * 
 * @param type
 * @param varName
 * @return a {@link TextEdit} which represents the changes which would be made, or <code>null</code> if the field
 * can not be created.
 */
public TextEdit addField(String type, String varName, boolean publicField, boolean staticField, boolean finalField, String value) {
	
	if (isReadOnly())
		return null;

	if (!domInitialized)
		initDomAST();
	
	boolean isStatic = isBodyStatic();
	int modifiers = (!publicField) ? Modifier.PRIVATE : Modifier.PUBLIC;
	if (isStatic || staticField) {
		modifiers |= Modifier.STATIC;
	}
	if (finalField) {
		modifiers |= Modifier.FINAL;
	}
	
	ASTRewrite rewrite= ASTRewrite.create(parentDeclaration.getAST());
	
	VariableDeclarationFragment newDeclFrag = addFieldDeclaration(rewrite, parentDeclaration, modifiers, varName, type, value);

	TextEdit te = rewrite.rewriteAST(getDocument(), null);
	return te;
}
 
Example 2
Source File: VariableDeclarationFix.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private static ModifierChangeOperation createAddFinalOperation(SimpleName name, ASTNode decl) {
	if (decl == null)
		return null;

	IBinding binding= name.resolveBinding();
	if (!canAddFinal(binding, decl))
		return null;

	if (decl instanceof SingleVariableDeclaration) {
		return new ModifierChangeOperation(decl, new ArrayList<VariableDeclarationFragment>(), Modifier.FINAL, Modifier.NONE);
	} else if (decl instanceof VariableDeclarationExpression) {
		return new ModifierChangeOperation(decl, new ArrayList<VariableDeclarationFragment>(), Modifier.FINAL, Modifier.NONE);
	} else if (decl instanceof VariableDeclarationFragment){
		VariableDeclarationFragment frag= (VariableDeclarationFragment)decl;
		decl= decl.getParent();
		if (decl instanceof FieldDeclaration || decl instanceof VariableDeclarationStatement) {
			List<VariableDeclarationFragment> list= new ArrayList<VariableDeclarationFragment>();
			list.add(frag);
			return new ModifierChangeOperation(decl, list, Modifier.FINAL, Modifier.NONE);
		} else if (decl instanceof VariableDeclarationExpression) {
			return new ModifierChangeOperation(decl, new ArrayList<VariableDeclarationFragment>(), Modifier.FINAL, Modifier.NONE);
		}
	}

	return null;
}
 
Example 3
Source File: VariableDeclarationFix.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private boolean handleFragments(List<VariableDeclarationFragment> list, ASTNode declaration) {
	List<VariableDeclarationFragment> toChange= new ArrayList<VariableDeclarationFragment>();

	for (Iterator<VariableDeclarationFragment> iter= list.iterator(); iter.hasNext();) {
		VariableDeclarationFragment fragment= iter.next();
		SimpleName name= fragment.getName();
		IBinding resolveBinding= name.resolveBinding();
		if (canAddFinal(resolveBinding, declaration)) {
			IVariableBinding varbinding= (IVariableBinding)resolveBinding;
			if (varbinding.isField()) {
				if (fieldCanBeFinal(fragment, varbinding))
					toChange.add(fragment);
			} else {
				if (!fWrittenVariables.containsKey(resolveBinding))
					toChange.add(fragment);
			}
		}
	}

	if (toChange.size() == 0)
		return false;

	ModifierChangeOperation op= new ModifierChangeOperation(declaration, toChange, Modifier.FINAL, Modifier.NONE);
	fResult.add(op);
	return false;
}
 
Example 4
Source File: PromoteTempToFieldRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private int getModifiers() {
	int flags= fVisibility;
	if (fDeclareFinal)
		flags |= Modifier.FINAL;
	if (fDeclareStatic)
		flags |= Modifier.STATIC;
    return flags;
}
 
Example 5
Source File: SourceActionDialog.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
protected Composite addVisibilityAndModifiersChoices(Composite buttonComposite) {
	// Add visibility and modifiers buttons: http://bugs.eclipse.org/bugs/show_bug.cgi?id=35870
	// Add persistence of options: http://bugs.eclipse.org/bugs/show_bug.cgi?id=38400
	IVisibilityChangeListener visibilityChangeListener= new IVisibilityChangeListener(){
		public void visibilityChanged(int newVisibility) {
			setVisibility(newVisibility);
		}
		public void modifierChanged(int modifier, boolean isChecked) {
			switch (modifier) {
				case Modifier.FINAL:  {
					setFinal(isChecked);
					return;
				}
				case Modifier.SYNCHRONIZED:  {
					setSynchronized(isChecked);
					return;
				}
				default: return;
			}
		}
	};

	int initialVisibility= getVisibilityModifier();
	int[] availableVisibilities= new int[]{Modifier.PUBLIC, Modifier.PROTECTED, Modifier.PRIVATE, Modifier.NONE};

	Composite visibilityComposite= createVisibilityControlAndModifiers(buttonComposite, visibilityChangeListener, availableVisibilities, initialVisibility);
	return visibilityComposite;
}
 
Example 6
Source File: NewVariableCorrectionProposal.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private int evaluateFieldModifiers(ASTNode newTypeDecl) {
	if (fSenderBinding.isAnnotation()) {
		return 0;
	}
	if (fSenderBinding.isInterface()) {
		// for interface members copy the modifiers from an existing field
		FieldDeclaration[] fieldDecls= ((TypeDeclaration) newTypeDecl).getFields();
		if (fieldDecls.length > 0) {
			return fieldDecls[0].getModifiers();
		}
		return 0;
	}
	int modifiers= 0;

	if (fVariableKind == CONST_FIELD) {
		modifiers |= Modifier.FINAL | Modifier.STATIC;
	} else {
		ASTNode parent= fOriginalNode.getParent();
		if (parent instanceof QualifiedName) {
			IBinding qualifierBinding= ((QualifiedName)parent).getQualifier().resolveBinding();
			if (qualifierBinding instanceof ITypeBinding) {
				modifiers |= Modifier.STATIC;
			}
		} else if (ASTResolving.isInStaticContext(fOriginalNode)) {
			modifiers |= Modifier.STATIC;
		}
	}
	ASTNode node= ASTResolving.findParentType(fOriginalNode, true);
	if (newTypeDecl.equals(node)) {
		modifiers |= Modifier.PRIVATE;
	} else if (node instanceof AnonymousClassDeclaration) {
		modifiers |= Modifier.PROTECTED;
	} else {
		modifiers |= Modifier.PUBLIC;
	}

	return modifiers;
}
 
Example 7
Source File: MissingReturnTypeCorrectionProposal.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
protected boolean testModifier(IVariableBinding curr) {
	int modifiers= curr.getModifiers();
	int staticFinal= Modifier.STATIC | Modifier.FINAL;
	if ((modifiers & staticFinal) == staticFinal) {
		return false;
	}
	if (Modifier.isStatic(modifiers) && !Modifier.isStatic(getModifiers())) {
		return false;
	}
	return true;
}
 
Example 8
Source File: TokenModifiers.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public boolean applies(IBinding binding) {
    int flags = binding.getModifiers();
    if ((flags & Modifier.FINAL) != 0) {
        return true;
    }
    return false;
}
 
Example 9
Source File: ConvertAnonymousToNestedRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private int createModifiersForNestedClass() {
    int flags= fVisibility;
    if (fDeclareFinal)
        flags|= Modifier.FINAL;
    if (mustInnerClassBeStatic() || fDeclareStatic)
        flags|= Modifier.STATIC;
    return flags;
}
 
Example 10
Source File: UMLModelASTReader.java    From RefactoringMiner with MIT License 5 votes vote down vote up
private List<UMLAttribute> processFieldDeclaration(CompilationUnit cu, FieldDeclaration fieldDeclaration, boolean isInterfaceField, String sourceFile) {
	UMLJavadoc javadoc = generateJavadoc(fieldDeclaration);
	List<UMLAttribute> attributes = new ArrayList<UMLAttribute>();
	Type fieldType = fieldDeclaration.getType();
	List<VariableDeclarationFragment> fragments = fieldDeclaration.fragments();
	for(VariableDeclarationFragment fragment : fragments) {
		UMLType type = UMLType.extractTypeObject(cu, sourceFile, fieldType, fragment.getExtraDimensions());
		String fieldName = fragment.getName().getFullyQualifiedName();
		LocationInfo locationInfo = generateLocationInfo(cu, sourceFile, fragment, CodeElementType.FIELD_DECLARATION);
		UMLAttribute umlAttribute = new UMLAttribute(fieldName, type, locationInfo);
		VariableDeclaration variableDeclaration = new VariableDeclaration(cu, sourceFile, fragment);
		variableDeclaration.setAttribute(true);
		umlAttribute.setVariableDeclaration(variableDeclaration);
		umlAttribute.setJavadoc(javadoc);
		
		int fieldModifiers = fieldDeclaration.getModifiers();
		if((fieldModifiers & Modifier.PUBLIC) != 0)
			umlAttribute.setVisibility("public");
		else if((fieldModifiers & Modifier.PROTECTED) != 0)
			umlAttribute.setVisibility("protected");
		else if((fieldModifiers & Modifier.PRIVATE) != 0)
			umlAttribute.setVisibility("private");
		else if(isInterfaceField)
			umlAttribute.setVisibility("public");
		else
			umlAttribute.setVisibility("package");
		
		if((fieldModifiers & Modifier.FINAL) != 0)
			umlAttribute.setFinal(true);
		
		if((fieldModifiers & Modifier.STATIC) != 0)
			umlAttribute.setStatic(true);
		
		attributes.add(umlAttribute);
	}
	return attributes;
}
 
Example 11
Source File: JavaASTFlattener.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public boolean visit(final Modifier it) {
  boolean append = true;
  int _flagValue = it.getKeyword().toFlagValue();
  switch (_flagValue) {
    case Modifier.PUBLIC:
      if (((it.getParent() instanceof TypeDeclaration) || (it.getParent() instanceof MethodDeclaration))) {
        append = false;
      }
      break;
    case Modifier.PRIVATE:
      ASTNode _parent = it.getParent();
      if ((_parent instanceof FieldDeclaration)) {
        append = false;
      }
      break;
    case Modifier.FINAL:
      if (((it.getParent() instanceof VariableDeclarationExpression) || (it.getParent() instanceof VariableDeclarationStatement))) {
        append = false;
      }
      break;
    default:
      append = true;
      break;
  }
  if (append) {
    String valueToAppend = it.getKeyword().toString();
    int _flagValue_1 = it.getKeyword().toFlagValue();
    boolean _equals = (_flagValue_1 == 0);
    if (_equals) {
      valueToAppend = "package";
    }
    this.appendToBuffer(valueToAppend);
    this.appendSpaceToBuffer();
  }
  return false;
}
 
Example 12
Source File: NewVariableCorrectionProposal.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private int evaluateFieldModifiers(ASTNode newTypeDecl) {
	if (fSenderBinding.isAnnotation()) {
		return 0;
	}
	if (fSenderBinding.isInterface()) {
		// for interface members copy the modifiers from an existing field
		FieldDeclaration[] fieldDecls= ((TypeDeclaration) newTypeDecl).getFields();
		if (fieldDecls.length > 0) {
			return fieldDecls[0].getModifiers();
		}
		return 0;
	}
	int modifiers= 0;

	if (fVariableKind == CONST_FIELD) {
		modifiers |= Modifier.FINAL | Modifier.STATIC;
	} else {
		ASTNode parent= fOriginalNode.getParent();
		if (parent instanceof QualifiedName) {
			IBinding qualifierBinding= ((QualifiedName)parent).getQualifier().resolveBinding();
			if (qualifierBinding instanceof ITypeBinding) {
				modifiers |= Modifier.STATIC;
			}
		} else if (ASTResolving.isInStaticContext(fOriginalNode)) {
			modifiers |= Modifier.STATIC;
		}
	}
	ASTNode node= ASTResolving.findParentType(fOriginalNode, true);
	if (newTypeDecl.equals(node)) {
		modifiers |= Modifier.PRIVATE;
	} else if (node instanceof AnonymousClassDeclaration) {
		modifiers |= Modifier.PROTECTED;
	} else {
		modifiers |= Modifier.PUBLIC;
	}

	return modifiers;
}
 
Example 13
Source File: MissingReturnTypeCorrectionProposal.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
protected boolean testModifier(IVariableBinding curr) {
	int modifiers= curr.getModifiers();
	int staticFinal= Modifier.STATIC | Modifier.FINAL;
	if ((modifiers & staticFinal) == staticFinal) {
		return false;
	}
	if (Modifier.isStatic(modifiers) && !Modifier.isStatic(getModifiers())) {
		return false;
	}
	return true;
}
 
Example 14
Source File: TypeRules.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Tests if a two types are cast compatible
 * @param castType The binding of the type to cast to
 * @param bindingToCast The binding ef the expression to cast.
 * @return boolean Returns true if (castType) bindingToCast is a valid cast expression (can be unnecessary, but not invalid).
 */
public static boolean canCast(ITypeBinding castType, ITypeBinding bindingToCast) {
	//see bug 80715

	String voidName= PrimitiveType.VOID.toString();

	if (castType.isAnonymous() || castType.isNullType() || voidName.equals(castType.getName())) {
		throw new IllegalArgumentException();
	}

	if (castType == bindingToCast) {
		return true;
	}

	if (voidName.equals(bindingToCast.getName())) {
		return false;
	}

	if (bindingToCast.isArray()) {
		if (!castType.isArray()) {
			return isArrayCompatible(castType); // can not cast an arraytype to a non array type (except to Object, Serializable...)
		}

		int toCastDim= bindingToCast.getDimensions();
		int castTypeDim= castType.getDimensions();
		if (toCastDim == castTypeDim) {
			bindingToCast= bindingToCast.getElementType();
			castType= castType.getElementType();
			if (castType.isPrimitive() && castType != bindingToCast) {
				return false; // can't assign arrays of different primitive types to each other
			}
			// fall through
		} else if (toCastDim < castTypeDim) {
			return isArrayCompatible(bindingToCast.getElementType());
		} else {
			return isArrayCompatible(castType.getElementType());
		}
	}
	if (castType.isPrimitive()) {
		if (!bindingToCast.isPrimitive()) {
			return false;
		}
		String boolName= PrimitiveType.BOOLEAN.toString();
		return (!boolName.equals(castType.getName()) && !boolName.equals(bindingToCast.getName()));
	} else {
		if (bindingToCast.isPrimitive()) {
			return false;
		}
		if (castType.isArray()) {
			return isArrayCompatible(bindingToCast);
		}
		if (castType.isInterface()) {
			if ((bindingToCast.getModifiers() & Modifier.FINAL) != 0) {
				return Bindings.isSuperType(castType, bindingToCast);
			} else {
				return true;
			}
		}
		if (bindingToCast.isInterface()) {
			if ((castType.getModifiers() & Modifier.FINAL) != 0) {
				return Bindings.isSuperType(bindingToCast, castType);
			} else {
				return true;
			}
		}
		if (isJavaLangObject(castType)) {
			return true;
		}

		return Bindings.isSuperType(bindingToCast, castType) || Bindings.isSuperType(castType, bindingToCast);
	}
}
 
Example 15
Source File: MoveInnerToTopRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private int getEnclosingInstanceAccessModifiers() {
	if (fMarkInstanceFieldAsFinal)
		return Modifier.PRIVATE | Modifier.FINAL;
	else
		return Modifier.PRIVATE;
}
 
Example 16
Source File: SemanticHighlightings.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public boolean consumes(SemanticToken token) {
	IBinding binding= token.getBinding();
	return binding != null && binding.getKind() == IBinding.VARIABLE && ((IVariableBinding)binding).isField() && (binding.getModifiers() & (Modifier.FINAL | Modifier.STATIC)) == (Modifier.FINAL | Modifier.STATIC);
}
 
Example 17
Source File: SemanticHighlightings.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public boolean consumes(SemanticToken token) {
	IBinding binding = token.getBinding();
	return binding != null && binding.getKind() == IBinding.VARIABLE && ((IVariableBinding) binding).isField() && (binding.getModifiers() & (Modifier.FINAL | Modifier.STATIC)) == (Modifier.FINAL | Modifier.STATIC);
}