Java Code Examples for org.eclipse.jdt.core.dom.Modifier#STATIC
The following examples show how to use
org.eclipse.jdt.core.dom.Modifier#STATIC .
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 |
/** * 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: MoveMethodRefactoring.java From JDeodorant with MIT License | 6 votes |
private void modifySourceStaticMethodInvocationsInTargetClass(MethodDeclaration newMethodDeclaration, ASTRewrite targetRewriter) { ExpressionExtractor extractor = new ExpressionExtractor(); List<Expression> sourceMethodInvocations = extractor.getMethodInvocations(sourceMethod.getBody()); List<Expression> newMethodInvocations = extractor.getMethodInvocations(newMethodDeclaration.getBody()); int i = 0; for(Expression expression : sourceMethodInvocations) { if(expression instanceof MethodInvocation) { MethodInvocation methodInvocation = (MethodInvocation)expression; IMethodBinding methodBinding = methodInvocation.resolveMethodBinding(); if((methodBinding.getModifiers() & Modifier.STATIC) != 0 && methodBinding.getDeclaringClass().isEqualTo(sourceTypeDeclaration.resolveBinding()) && !additionalMethodsToBeMoved.containsKey(methodInvocation)) { AST ast = newMethodDeclaration.getAST(); SimpleName qualifier = ast.newSimpleName(sourceTypeDeclaration.getName().getIdentifier()); targetRewriter.set(newMethodInvocations.get(i), MethodInvocation.EXPRESSION_PROPERTY, qualifier, null); this.additionalTypeBindingsToBeImportedInTargetClass.add(sourceTypeDeclaration.resolveBinding()); } } i++; } }
Example 3
Source File: StyledStringVisitor.java From JDeodorant with MIT License | 5 votes |
private boolean isNonStaticField(ASTNode node){ if (node instanceof SimpleName){ SimpleName simpleName = (SimpleName) node; IBinding binding = simpleName.resolveBinding(); if(binding != null && binding.getKind() == IBinding.VARIABLE) { IVariableBinding variableBinding = (IVariableBinding)binding; if(variableBinding.isField() && (variableBinding.getModifiers() & Modifier.STATIC) == 0) return true; } } return false; }
Example 4
Source File: JavaSourceFileParser.java From BUILD_file_generator with Apache License 2.0 | 5 votes |
/** * Returns true iff 'methodDeclaration' represents a void static method named 'main' that takes a * single String[] parameter. */ private static boolean isMainMethod(MethodDeclaration methodDeclaration) { // Is it static? if ((methodDeclaration.getModifiers() & Modifier.STATIC) == 0) { return false; } // Does it return void? Type returnType = methodDeclaration.getReturnType2(); if (!returnType.isPrimitiveType()) { return false; } if (((PrimitiveType) returnType).getPrimitiveTypeCode() != PrimitiveType.VOID) { return false; } // Is it called 'main'? if (!"main".equals(methodDeclaration.getName().getIdentifier())) { return false; } // Does it have a single parameter? if (methodDeclaration.parameters().size() != 1) { return false; } // Is the parameter's type String[]? SingleVariableDeclaration pt = getOnlyElement((List<SingleVariableDeclaration>) methodDeclaration.parameters()); IVariableBinding vb = pt.resolveBinding(); if (vb == null) { return false; } ITypeBinding tb = vb.getType(); return tb != null && "java.lang.String[]".equals(tb.getQualifiedName()); }
Example 5
Source File: SemanticHighlightings.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 5 votes |
@Override public boolean consumes(SemanticToken token) { SimpleName node = token.getNode(); if (node.isDeclaration()) { return false; } IBinding binding = token.getBinding(); return binding != null && binding.getKind() == IBinding.METHOD && (binding.getModifiers() & Modifier.STATIC) == Modifier.STATIC; }
Example 6
Source File: ExtractFieldRefactoring.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 5 votes |
private int getModifiers() { int flags = fVisibility; if (isDeclaredInStaticMethod()) { flags |= Modifier.STATIC; } return flags; }
Example 7
Source File: AbstractMethodFragment.java From JDeodorant with MIT License | 5 votes |
protected void processConstructorInvocation(ConstructorInvocation constructorInvocation) { IMethodBinding methodBinding = constructorInvocation.resolveConstructorBinding(); String originClassName = methodBinding.getDeclaringClass().getQualifiedName(); TypeObject originClassTypeObject = TypeObject.extractTypeObject(originClassName); String methodInvocationName = methodBinding.getName(); String qualifiedName = methodBinding.getReturnType().getQualifiedName(); TypeObject returnType = TypeObject.extractTypeObject(qualifiedName); ConstructorInvocationObject constructorInvocationObject = new ConstructorInvocationObject(originClassTypeObject, methodInvocationName, returnType); constructorInvocationObject.setConstructorInvocation(constructorInvocation); ITypeBinding[] parameterTypes = methodBinding.getParameterTypes(); for(ITypeBinding parameterType : parameterTypes) { String qualifiedParameterName = parameterType.getQualifiedName(); TypeObject typeObject = TypeObject.extractTypeObject(qualifiedParameterName); constructorInvocationObject.addParameter(typeObject); } ITypeBinding[] thrownExceptionTypes = methodBinding.getExceptionTypes(); for(ITypeBinding thrownExceptionType : thrownExceptionTypes) { constructorInvocationObject.addThrownException(thrownExceptionType.getQualifiedName()); } if((methodBinding.getModifiers() & Modifier.STATIC) != 0) constructorInvocationObject.setStatic(true); addConstructorInvocation(constructorInvocationObject); List<Expression> arguments = constructorInvocation.arguments(); for(Expression argument : arguments) { if(argument instanceof SimpleName) { SimpleName argumentName = (SimpleName)argument; IBinding binding = argumentName.resolveBinding(); if(binding != null && binding.getKind() == IBinding.VARIABLE) { IVariableBinding variableBinding = (IVariableBinding)binding; if(variableBinding.isParameter()) { PlainVariable variable = new PlainVariable(variableBinding); addParameterPassedAsArgumentInConstructorInvocation(variable, constructorInvocationObject); } } } } }
Example 8
Source File: PromoteTempToFieldRefactoring.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
private int getModifiers() { int flags= fVisibility; if (fDeclareFinal) flags |= Modifier.FINAL; if (fDeclareStatic) flags |= Modifier.STATIC; return flags; }
Example 9
Source File: MissingReturnTypeCorrectionProposal.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 5 votes |
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 10
Source File: StyledStringVisitor.java From JDeodorant with MIT License | 5 votes |
private boolean isStaticMethodCall(ASTNode node) { if (node instanceof SimpleName){ SimpleName simpleName = (SimpleName) node; IBinding binding = simpleName.resolveBinding(); if(binding != null && binding.getKind() == IBinding.METHOD) { IMethodBinding methodBinding = (IMethodBinding)binding; if((methodBinding.getModifiers() & Modifier.STATIC) != 0) return true; } } return false; }
Example 11
Source File: UiBinderUtilities.java From gwt-eclipse-plugin with Eclipse Public License 1.0 | 5 votes |
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 12
Source File: StyledStringVisitor.java From JDeodorant with MIT License | 5 votes |
private boolean isNamedConstant(ASTNode node){ if (node instanceof SimpleName){ SimpleName simpleName = (SimpleName) node; IBinding binding = simpleName.resolveBinding(); if(binding != null && binding.getKind() == IBinding.VARIABLE) { IVariableBinding variableBinding = (IVariableBinding)binding; if(variableBinding.isField() && (variableBinding.getModifiers() & Modifier.STATIC) != 0) return true; } } return false; }
Example 13
Source File: UMLModelASTReader.java From RefactoringMiner with MIT License | 5 votes |
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 14
Source File: ASTReader.java From JDeodorant with MIT License | 5 votes |
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 15
Source File: SemanticHighlightings.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
@Override public boolean consumes(SemanticToken token) { IBinding binding= token.getBinding(); return binding != null && binding.getKind() == IBinding.VARIABLE && ((IVariableBinding)binding).isField() && (binding.getModifiers() & Modifier.STATIC) == Modifier.STATIC; }
Example 16
Source File: SemanticHighlightings.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
@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: AssignToVariableAssistProposal.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 4 votes |
private ASTRewrite doAddField(ASTRewrite rewrite, ASTNode nodeToAssign, ITypeBinding typeBinding, int index) { boolean isParamToField= nodeToAssign.getNodeType() == ASTNode.SINGLE_VARIABLE_DECLARATION; ASTNode newTypeDecl= ASTResolving.findParentType(nodeToAssign); if (newTypeDecl == null) { return null; } Expression expression= isParamToField ? ((SingleVariableDeclaration) nodeToAssign).getName() : ((ExpressionStatement) nodeToAssign).getExpression(); AST ast= newTypeDecl.getAST(); createImportRewrite((CompilationUnit) nodeToAssign.getRoot()); BodyDeclaration bodyDecl= ASTResolving.findParentBodyDeclaration(nodeToAssign); Block body; if (bodyDecl instanceof MethodDeclaration) { body= ((MethodDeclaration) bodyDecl).getBody(); } else if (bodyDecl instanceof Initializer) { body= ((Initializer) bodyDecl).getBody(); } else { return null; } IJavaProject project= getCompilationUnit().getJavaProject(); boolean isAnonymous= newTypeDecl.getNodeType() == ASTNode.ANONYMOUS_CLASS_DECLARATION; boolean isStatic= Modifier.isStatic(bodyDecl.getModifiers()) && !isAnonymous; int modifiers= Modifier.PRIVATE; if (isStatic) { modifiers |= Modifier.STATIC; } VariableDeclarationFragment newDeclFrag= addFieldDeclaration(rewrite, newTypeDecl, modifiers, expression, nodeToAssign, typeBinding, index); String varName= newDeclFrag.getName().getIdentifier(); Assignment assignment= ast.newAssignment(); assignment.setRightHandSide((Expression) rewrite.createCopyTarget(expression)); boolean needsThis= StubUtility.useThisForFieldAccess(project); if (isParamToField) { needsThis |= varName.equals(((SimpleName) expression).getIdentifier()); } SimpleName accessName= ast.newSimpleName(varName); if (needsThis) { FieldAccess fieldAccess= ast.newFieldAccess(); fieldAccess.setName(accessName); if (isStatic) { String typeName= ((AbstractTypeDeclaration) newTypeDecl).getName().getIdentifier(); fieldAccess.setExpression(ast.newSimpleName(typeName)); } else { fieldAccess.setExpression(ast.newThisExpression()); } assignment.setLeftHandSide(fieldAccess); } else { assignment.setLeftHandSide(accessName); } ASTNode selectionNode; if (isParamToField) { // assign parameter to field ExpressionStatement statement= ast.newExpressionStatement(assignment); int insertIdx= findAssignmentInsertIndex(body.statements(), nodeToAssign) + index; rewrite.getListRewrite(body, Block.STATEMENTS_PROPERTY).insertAt(statement, insertIdx, null); selectionNode= statement; } else { if (needsSemicolon(expression)) { rewrite.replace(expression, ast.newExpressionStatement(assignment), null); } else { rewrite.replace(expression, assignment, null); } selectionNode= nodeToAssign; } addLinkedPosition(rewrite.track(newDeclFrag.getName()), false, KEY_NAME + index); if (!isParamToField) { FieldDeclaration fieldDeclaration= (FieldDeclaration) newDeclFrag.getParent(); addLinkedPosition(rewrite.track(fieldDeclaration.getType()), false, KEY_TYPE); } addLinkedPosition(rewrite.track(accessName), true, KEY_NAME + index); IVariableBinding variableBinding= newDeclFrag.resolveBinding(); if (variableBinding != null) { SimpleName[] linkedNodes= LinkedNodeFinder.findByBinding(nodeToAssign.getRoot(), variableBinding); for (int i= 0; i < linkedNodes.length; i++) { addLinkedPosition(rewrite.track(linkedNodes[i]), false, KEY_NAME + index); } } setEndPosition(rewrite.track(selectionNode)); return rewrite; }
Example 18
Source File: PolymorphismRefactoring.java From JDeodorant with MIT License | 4 votes |
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(); } } } } } }
Example 19
Source File: SemanticHighlightings.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 4 votes |
@Override public boolean consumes(SemanticToken token) { IBinding binding = token.getBinding(); return binding != null && binding.getKind() == IBinding.VARIABLE && ((IVariableBinding) binding).isField() && (binding.getModifiers() & Modifier.STATIC) == Modifier.STATIC; }
Example 20
Source File: SemanticHighlightings.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 4 votes |
@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); }