Java Code Examples for org.eclipse.jdt.core.dom.FieldDeclaration#getModifiers()

The following examples show how to use org.eclipse.jdt.core.dom.FieldDeclaration#getModifiers() . 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: SM_Field.java    From DesigniteJava with Apache License 2.0 5 votes vote down vote up
void setFieldInfo(FieldDeclaration field){
	int modifiers = field.getModifiers();
	if (Modifier.isFinal(modifiers)) 
		finalField =  true;
	if (Modifier.isStatic(modifiers)) 
		staticField =  true;
}
 
Example 2
Source File: JavaASTVisitor.java    From SnowGraph with Apache License 2.0 5 votes vote down vote up
private static String getVisibility(FieldDeclaration decl) {
    int modifiers = decl.getModifiers();
    if (Modifier.isPrivate(modifiers))
        return "private";
    if (Modifier.isProtected(modifiers))
        return "protected";
    if (Modifier.isPublic(modifiers))
        return "public";
    return "package";
}
 
Example 3
Source File: UiBinderUtilities.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 5 votes vote down vote up
public static boolean isUiField(FieldDeclaration fieldDecl) {
  if ((fieldDecl.getModifiers() & Modifier.STATIC) != 0) {
    return false;
  }
  return (JavaASTUtils.findAnnotation(fieldDecl,
      UiBinderConstants.UI_FIELD_ANNOTATION_NAME) != null);
}
 
Example 4
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 5
Source File: ASTReader.java    From JDeodorant with MIT License 5 votes vote down vote up
private void processFieldDeclaration(final ClassObject classObject, FieldDeclaration fieldDeclaration) {
	Type fieldType = fieldDeclaration.getType();
	ITypeBinding binding = fieldType.resolveBinding();
	List<CommentObject> fieldDeclarationComments = new ArrayList<CommentObject>();
	int fieldDeclarationStartPosition = fieldDeclaration.getStartPosition();
	int fieldDeclarationEndPosition = fieldDeclarationStartPosition + fieldDeclaration.getLength();
	for(CommentObject comment : classObject.commentList) {
		int commentStartPosition = comment.getStartPosition();
		int commentEndPosition = commentStartPosition + comment.getLength();
		if(fieldDeclarationStartPosition <= commentStartPosition && fieldDeclarationEndPosition >= commentEndPosition) {
			fieldDeclarationComments.add(comment);
		}
	}
	List<VariableDeclarationFragment> fragments = fieldDeclaration.fragments();
	for(VariableDeclarationFragment fragment : fragments) {
		String qualifiedName = binding.getQualifiedName();
		TypeObject typeObject = TypeObject.extractTypeObject(qualifiedName);
		typeObject.setArrayDimension(typeObject.getArrayDimension() + fragment.getExtraDimensions());
		FieldObject fieldObject = new FieldObject(typeObject, fragment.getName().getIdentifier());
		fieldObject.setClassName(classObject.getName());
		fieldObject.setVariableDeclarationFragment(fragment);
		fieldObject.addComments(fieldDeclarationComments);
		
		int fieldModifiers = fieldDeclaration.getModifiers();
		if((fieldModifiers & Modifier.PUBLIC) != 0)
			fieldObject.setAccess(Access.PUBLIC);
		else if((fieldModifiers & Modifier.PROTECTED) != 0)
			fieldObject.setAccess(Access.PROTECTED);
		else if((fieldModifiers & Modifier.PRIVATE) != 0)
			fieldObject.setAccess(Access.PRIVATE);
		else
			fieldObject.setAccess(Access.NONE);
		
		if((fieldModifiers & Modifier.STATIC) != 0)
			fieldObject.setStatic(true);
		
		classObject.addField(fieldObject);
	}
}
 
Example 6
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 7
Source File: JavaASTVisitor.java    From SnowGraph with Apache License 2.0 4 votes vote down vote up
private static boolean isStatic(FieldDeclaration decl) {
    int modifiers = decl.getModifiers();
    return (Modifier.isStatic(modifiers));
}
 
Example 8
Source File: ExtractClassRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private FieldDeclaration performFieldRewrite(IType type, ParameterObjectFactory pof, RefactoringStatus status) throws CoreException {
	fBaseCURewrite= new CompilationUnitRewrite(type.getCompilationUnit());
	SimpleName name= (SimpleName) NodeFinder.perform(fBaseCURewrite.getRoot(), type.getNameRange());
	TypeDeclaration typeNode= (TypeDeclaration) ASTNodes.getParent(name, ASTNode.TYPE_DECLARATION);
	ASTRewrite rewrite= fBaseCURewrite.getASTRewrite();
	int modifier= Modifier.PRIVATE;
	TextEditGroup removeFieldGroup= fBaseCURewrite.createGroupDescription(RefactoringCoreMessages.ExtractClassRefactoring_group_remove_field);
	FieldDeclaration lastField= null;
	initializeDeclaration(typeNode);
	for (Iterator<FieldInfo> iter= fVariables.values().iterator(); iter.hasNext();) {
		FieldInfo pi= iter.next();
		if (isCreateField(pi)) {
			VariableDeclarationFragment vdf= pi.declaration;
			FieldDeclaration parent= (FieldDeclaration) vdf.getParent();
			if (lastField == null)
				lastField= parent;
			else if (lastField.getStartPosition() < parent.getStartPosition())
				lastField= parent;

			ListRewrite listRewrite= rewrite.getListRewrite(parent, FieldDeclaration.FRAGMENTS_PROPERTY);
			removeNode(vdf, removeFieldGroup, fBaseCURewrite);
			if (listRewrite.getRewrittenList().size() == 0) {
				removeNode(parent, removeFieldGroup, fBaseCURewrite);
			}

			if (fDescriptor.isCreateTopLevel()) {
				IVariableBinding binding= vdf.resolveBinding();
				ITypeRoot typeRoot= fBaseCURewrite.getCu();
				if (binding == null || binding.getType() == null){
					status.addFatalError(Messages.format(RefactoringCoreMessages.ExtractClassRefactoring_fatal_error_cannot_resolve_binding, BasicElementLabels.getJavaElementName(pi.name)), JavaStatusContext.create(typeRoot, vdf));
				} else {
					ITypeBinding typeBinding= binding.getType();
					if (Modifier.isPrivate(typeBinding.getModifiers())){
						status.addError(Messages.format(RefactoringCoreMessages.ExtractClassRefactoring_error_referencing_private_class, BasicElementLabels.getJavaElementName(typeBinding.getName())), JavaStatusContext.create(typeRoot, vdf));
					} else if (Modifier.isProtected(typeBinding.getModifiers())){
						ITypeBinding declaringClass= typeBinding.getDeclaringClass();
						if (declaringClass != null) {
							IPackageBinding package1= declaringClass.getPackage();
							if (package1 != null && !fDescriptor.getPackage().equals(package1.getName())){
								status.addError(Messages.format(RefactoringCoreMessages.ExtractClassRefactoring_error_referencing_protected_class, new String[] {BasicElementLabels.getJavaElementName(typeBinding.getName()), BasicElementLabels.getJavaElementName(fDescriptor.getPackage())}), JavaStatusContext.create(typeRoot, vdf));
							}
						}
					}
				}
			}
			Expression initializer= vdf.getInitializer();
			if (initializer != null)
				pi.initializer= initializer;
			int modifiers= parent.getModifiers();
			if (!MemberVisibilityAdjustor.hasLowerVisibility(modifiers, modifier)){
				modifier= modifiers;
			}
		}
	}
	FieldDeclaration fieldDeclaration= createParameterObjectField(pof, typeNode, modifier);
	ListRewrite bodyDeclList= rewrite.getListRewrite(typeNode, TypeDeclaration.BODY_DECLARATIONS_PROPERTY);
	if (lastField != null)
		bodyDeclList.insertAfter(fieldDeclaration, lastField, null);
	else
		bodyDeclList.insertFirst(fieldDeclaration, null);
	return fieldDeclaration;
}
 
Example 9
Source File: PolymorphismRefactoring.java    From JDeodorant with MIT License 4 votes vote down vote up
protected void generateGettersForAccessedFields() {
	AST contextAST = sourceTypeDeclaration.getAST();
	Set<VariableDeclarationFragment> accessedFields = new LinkedHashSet<VariableDeclarationFragment>();
	accessedFields.addAll(typeCheckElimination.getAccessedFields());
	accessedFields.addAll(typeCheckElimination.getSuperAccessedFields());
	for(VariableDeclarationFragment fragment : accessedFields) {
		if((fragment.resolveBinding().getModifiers() & Modifier.STATIC) == 0) {
			IMethodBinding getterMethodBinding = null;
			if(typeCheckElimination.getSuperAccessedFields().contains(fragment)) {
				for(IVariableBinding fieldBinding : typeCheckElimination.getSuperAccessedFieldBindings()) {
					if(fieldBinding.isEqualTo(fragment.resolveBinding())) {
						getterMethodBinding = typeCheckElimination.getGetterMethodBindingOfSuperAccessedField(fieldBinding);
						break;
					}
				}
			}
			else {
				getterMethodBinding = findGetterMethodInContext(fragment.resolveBinding());
			}
			if(getterMethodBinding == null) {
				FieldDeclaration fieldDeclaration = (FieldDeclaration)fragment.getParent();
				int modifiers = fieldDeclaration.getModifiers();
				if(!fragment.equals(typeCheckElimination.getTypeField()) &&
						!((modifiers & Modifier.PUBLIC) != 0 && (modifiers & Modifier.STATIC) != 0)) {
					ASTRewrite sourceRewriter = ASTRewrite.create(sourceTypeDeclaration.getAST());
					MethodDeclaration newMethodDeclaration = contextAST.newMethodDeclaration();
					sourceRewriter.set(newMethodDeclaration, MethodDeclaration.RETURN_TYPE2_PROPERTY, fieldDeclaration.getType(), null);
					ListRewrite methodDeclarationModifiersRewrite = sourceRewriter.getListRewrite(newMethodDeclaration, MethodDeclaration.MODIFIERS2_PROPERTY);
					methodDeclarationModifiersRewrite.insertLast(contextAST.newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD), null);
					String methodName = fragment.getName().getIdentifier();
					methodName = "get" + methodName.substring(0,1).toUpperCase() + methodName.substring(1,methodName.length());
					sourceRewriter.set(newMethodDeclaration, MethodDeclaration.NAME_PROPERTY, contextAST.newSimpleName(methodName), null);
					Block methodDeclarationBody = contextAST.newBlock();
					ListRewrite methodDeclarationBodyStatementsRewrite = sourceRewriter.getListRewrite(methodDeclarationBody, Block.STATEMENTS_PROPERTY);
					ReturnStatement returnStatement = contextAST.newReturnStatement();
					sourceRewriter.set(returnStatement, ReturnStatement.EXPRESSION_PROPERTY, fragment.getName(), null);
					methodDeclarationBodyStatementsRewrite.insertLast(returnStatement, null);
					sourceRewriter.set(newMethodDeclaration, MethodDeclaration.BODY_PROPERTY, methodDeclarationBody, null);
					ListRewrite contextBodyRewrite = sourceRewriter.getListRewrite(sourceTypeDeclaration, TypeDeclaration.BODY_DECLARATIONS_PROPERTY);
					contextBodyRewrite.insertLast(newMethodDeclaration, null);
					try {
						TextEdit sourceEdit = sourceRewriter.rewriteAST();
						ICompilationUnit sourceICompilationUnit = (ICompilationUnit)sourceCompilationUnit.getJavaElement();
						CompilationUnitChange change = compilationUnitChanges.get(sourceICompilationUnit);
						change.getEdit().addChild(sourceEdit);
						change.addTextEditGroup(new TextEditGroup("Create getter method for accessed field", new TextEdit[] {sourceEdit}));
					} catch (JavaModelException e) {
						e.printStackTrace();
					}
				}
			}
		}
	}
}