Java Code Examples for org.eclipse.jdt.core.dom.FieldDeclaration#getType()
The following examples show how to use
org.eclipse.jdt.core.dom.FieldDeclaration#getType() .
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: JavaASTVisitor.java From SnowGraph with Apache License 2.0 | 6 votes |
private List<FieldInfo> createFieldInfos(FieldDeclaration node, String belongTo) { List<FieldInfo> fieldInfos = new ArrayList<>(); Type type = node.getType(); Set<String> types = getTypes(type); String typeString = type.toString(); String visibility = getVisibility(node); boolean isStatic = isStatic(node); boolean isFinal = isFinal(node); String comment = ""; if (node.getJavadoc() != null) comment = sourceContent.substring(node.getJavadoc().getStartPosition(), node.getJavadoc().getStartPosition() + node.getJavadoc().getLength()); List<VariableDeclarationFragment> fragments = node.fragments(); for (VariableDeclarationFragment fragment : fragments) { FieldInfo fieldInfo = new FieldInfo(); fieldInfo.belongTo = belongTo; fieldInfo.name = fragment.getName().getFullyQualifiedName(); fieldInfo.typeString = typeString; fieldInfo.types = types; fieldInfo.visibility = visibility; fieldInfo.isFinal = isFinal; fieldInfo.isStatic = isStatic; fieldInfo.comment = comment; fieldInfos.add(fieldInfo); } return fieldInfos; }
Example 2
Source File: VariableDeclaration.java From RefactoringMiner with MIT License | 6 votes |
private static Type extractType(org.eclipse.jdt.core.dom.VariableDeclaration variableDeclaration) { Type returnedVariableType = null; if(variableDeclaration instanceof SingleVariableDeclaration) { SingleVariableDeclaration singleVariableDeclaration = (SingleVariableDeclaration)variableDeclaration; returnedVariableType = singleVariableDeclaration.getType(); } else if(variableDeclaration instanceof VariableDeclarationFragment) { VariableDeclarationFragment fragment = (VariableDeclarationFragment)variableDeclaration; if(fragment.getParent() instanceof VariableDeclarationStatement) { VariableDeclarationStatement variableDeclarationStatement = (VariableDeclarationStatement)fragment.getParent(); returnedVariableType = variableDeclarationStatement.getType(); } else if(fragment.getParent() instanceof VariableDeclarationExpression) { VariableDeclarationExpression variableDeclarationExpression = (VariableDeclarationExpression)fragment.getParent(); returnedVariableType = variableDeclarationExpression.getType(); } else if(fragment.getParent() instanceof FieldDeclaration) { FieldDeclaration fieldDeclaration = (FieldDeclaration)fragment.getParent(); returnedVariableType = fieldDeclaration.getType(); } } return returnedVariableType; }
Example 3
Source File: HierarchyProcessor.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
protected static FieldDeclaration createNewFieldDeclarationNode(final ASTRewrite rewrite, final CompilationUnit unit, final IField field, final VariableDeclarationFragment oldFieldFragment, final TypeVariableMaplet[] mapping, final IProgressMonitor monitor, final RefactoringStatus status, final int modifiers) throws JavaModelException { final VariableDeclarationFragment newFragment= rewrite.getAST().newVariableDeclarationFragment(); copyExtraDimensions(oldFieldFragment, newFragment); if (oldFieldFragment.getInitializer() != null) { Expression newInitializer= null; if (mapping.length > 0) newInitializer= createPlaceholderForExpression(oldFieldFragment.getInitializer(), field.getCompilationUnit(), mapping, rewrite); else newInitializer= createPlaceholderForExpression(oldFieldFragment.getInitializer(), field.getCompilationUnit(), rewrite); newFragment.setInitializer(newInitializer); } newFragment.setName(((SimpleName) ASTNode.copySubtree(rewrite.getAST(), oldFieldFragment.getName()))); final FieldDeclaration newField= rewrite.getAST().newFieldDeclaration(newFragment); final FieldDeclaration oldField= ASTNodeSearchUtil.getFieldDeclarationNode(field, unit); copyJavadocNode(rewrite, oldField, newField); copyAnnotations(oldField, newField); newField.modifiers().addAll(ASTNodeFactory.newModifiers(rewrite.getAST(), modifiers)); final Type oldType= oldField.getType(); Type newType= null; if (mapping.length > 0) { newType= createPlaceholderForType(oldType, field.getCompilationUnit(), mapping, rewrite); } else newType= createPlaceholderForType(oldType, field.getCompilationUnit(), rewrite); newField.setType(newType); return newField; }
Example 4
Source File: MoveMethodRefactoring.java From JDeodorant with MIT License | 6 votes |
private void addParameterToMovedMethod(MethodDeclaration newMethodDeclaration, SimpleName fieldName, ASTRewrite targetRewriter) { AST ast = newMethodDeclaration.getAST(); SingleVariableDeclaration parameter = ast.newSingleVariableDeclaration(); Type fieldType = null; FieldDeclaration[] fields = sourceTypeDeclaration.getFields(); for(FieldDeclaration field : fields) { List<VariableDeclarationFragment> fragments = field.fragments(); for(VariableDeclarationFragment fragment : fragments) { if(fragment.getName().getIdentifier().equals(fieldName.getIdentifier())) { fieldType = field.getType(); break; } } } targetRewriter.set(parameter, SingleVariableDeclaration.TYPE_PROPERTY, fieldType, null); targetRewriter.set(parameter, SingleVariableDeclaration.NAME_PROPERTY, ast.newSimpleName(fieldName.getIdentifier()), null); ListRewrite parametersRewrite = targetRewriter.getListRewrite(newMethodDeclaration, MethodDeclaration.PARAMETERS_PROPERTY); parametersRewrite.insertLast(parameter, null); this.additionalArgumentsAddedToMovedMethod.add(fieldName.getIdentifier()); this.additionalTypeBindingsToBeImportedInTargetClass.add(fieldType.resolveBinding()); addParamTagElementToJavadoc(newMethodDeclaration, targetRewriter, fieldName.getIdentifier()); }
Example 5
Source File: RefactoringUtility.java From JDeodorant with MIT License | 6 votes |
private static Type extractType(VariableDeclaration variableDeclaration) { Type returnedVariableType = null; if(variableDeclaration instanceof SingleVariableDeclaration) { SingleVariableDeclaration singleVariableDeclaration = (SingleVariableDeclaration)variableDeclaration; returnedVariableType = singleVariableDeclaration.getType(); } else if(variableDeclaration instanceof VariableDeclarationFragment) { VariableDeclarationFragment fragment = (VariableDeclarationFragment)variableDeclaration; if(fragment.getParent() instanceof VariableDeclarationStatement) { VariableDeclarationStatement variableDeclarationStatement = (VariableDeclarationStatement)fragment.getParent(); returnedVariableType = variableDeclarationStatement.getType(); } else if(fragment.getParent() instanceof VariableDeclarationExpression) { VariableDeclarationExpression variableDeclarationExpression = (VariableDeclarationExpression)fragment.getParent(); returnedVariableType = variableDeclarationExpression.getType(); } else if(fragment.getParent() instanceof FieldDeclaration) { FieldDeclaration fieldDeclaration = (FieldDeclaration)fragment.getParent(); returnedVariableType = fieldDeclaration.getType(); } } return returnedVariableType; }
Example 6
Source File: NodeUtils.java From SimFix with GNU General Public License v2.0 | 5 votes |
public boolean visit(FieldDeclaration node) { Type type = node.getType(); for(Object object: node.fragments()){ VariableDeclarationFragment vdf = (VariableDeclarationFragment) object; _vars.put(vdf.getName().toString(), type); } return true; }
Example 7
Source File: TypeParseVisitor.java From SimFix with GNU General Public License v2.0 | 5 votes |
public boolean visit(TypeDeclaration node) { Pair<String, String> clazzAndMethodName = NodeUtils.getTypeDecAndMethodDec(node.getName()); String clazz = clazzAndMethodName.getFirst(); AST ast = AST.newAST(AST.JLS8); Type type = ast.newSimpleType(ast.newSimpleName(clazz)); ProjectInfo.addFieldType(clazz, "THIS", type); Type suType = node.getSuperclassType(); if(suType != null){ ProjectInfo.addFieldType(clazz, "SUPER", suType); ProjectInfo.addSuperClass(clazz, suType.toString()); } List<Object> sInterfaces = node.superInterfaceTypes(); if(sInterfaces != null){ for(Object object : sInterfaces){ if(object instanceof Type){ Type interfaceType = (Type) object; ProjectInfo.addSuperInterface(clazz, interfaceType.toString()); } } } FieldDeclaration fields[] = node.getFields(); for (FieldDeclaration f : fields) { for (Object o : f.fragments()) { VariableDeclarationFragment vdf = (VariableDeclarationFragment) o; Type tmpType = f.getType(); if(vdf.getExtraDimensions() > 0){ tmpType = ast.newArrayType((Type) ASTNode.copySubtree(ast, tmpType), vdf.getExtraDimensions()); } ProjectInfo.addFieldType(clazz, vdf.getName().toString(), tmpType); } } return true; }
Example 8
Source File: SelfEncapsulateFieldRefactoring.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 5 votes |
private MethodDeclaration createGetterMethod(AST ast, ASTRewrite rewriter, String lineDelimiter) throws CoreException { FieldDeclaration field = ASTNodes.getParent(fFieldDeclaration, FieldDeclaration.class); Type type = field.getType(); MethodDeclaration result = ast.newMethodDeclaration(); result.setName(ast.newSimpleName(fGetterName)); result.modifiers().addAll(ASTNodeFactory.newModifiers(ast, createModifiers())); Type returnType = DimensionRewrite.copyTypeAndAddDimensions(type, fFieldDeclaration.extraDimensions(), rewriter); result.setReturnType2(returnType); Block block = ast.newBlock(); result.setBody(block); String body = CodeGeneration.getGetterMethodBodyContent(fField.getCompilationUnit(), getTypeName(field.getParent()), fGetterName, fField.getElementName(), lineDelimiter); if (body != null) { body = body.substring(0, body.lastIndexOf(lineDelimiter)); ASTNode getterNode = rewriter.createStringPlaceholder(body, ASTNode.BLOCK); block.statements().add(getterNode); } else { ReturnStatement rs = ast.newReturnStatement(); rs.setExpression(ast.newSimpleName(fField.getElementName())); block.statements().add(rs); } if (fGenerateJavadoc) { String string = CodeGeneration.getGetterComment(fField.getCompilationUnit(), getTypeName(field.getParent()), fGetterName, fField.getElementName(), ASTNodes.asString(type), StubUtility.getBaseName(fField), lineDelimiter); if (string != null) { Javadoc javadoc = (Javadoc) fRewriter.createStringPlaceholder(string, ASTNode.JAVADOC); result.setJavadoc(javadoc); } } return result; }
Example 9
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 10
Source File: TestQ22.java From compiler with Apache License 2.0 | 5 votes |
@Override public boolean visit(FieldDeclaration node) { field ++; org.eclipse.jdt.core.dom.Type type = node.getType(); if (type instanceof SimpleType && ((SimpleType)type).getName().getFullyQualifiedName().equals("String")) { stringField += node.fragments().size(); stringField2 += node.fragments().size(); } return true; }
Example 11
Source File: PushDownRefactoringProcessor.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
private FieldDeclaration createNewFieldDeclarationNode(MemberActionInfo info, CompilationUnit declaringCuNode, TypeVariableMaplet[] mapping, ASTRewrite rewrite, VariableDeclarationFragment oldFieldFragment) throws JavaModelException { Assert.isTrue(info.isFieldInfo()); IField field= (IField) info.getMember(); AST ast= rewrite.getAST(); VariableDeclarationFragment newFragment= ast.newVariableDeclarationFragment(); copyExtraDimensions(oldFieldFragment, newFragment); Expression initializer= oldFieldFragment.getInitializer(); if (initializer != null) { Expression newInitializer= null; if (mapping.length > 0) newInitializer= createPlaceholderForExpression(initializer, field.getCompilationUnit(), mapping, rewrite); else newInitializer= createPlaceholderForExpression(initializer, field.getCompilationUnit(), rewrite); newFragment.setInitializer(newInitializer); } newFragment.setName(ast.newSimpleName(oldFieldFragment.getName().getIdentifier())); FieldDeclaration newField= ast.newFieldDeclaration(newFragment); FieldDeclaration oldField= ASTNodeSearchUtil.getFieldDeclarationNode(field, declaringCuNode); if (info.copyJavadocToCopiesInSubclasses()) copyJavadocNode(rewrite, oldField, newField); copyAnnotations(oldField, newField); newField.modifiers().addAll(ASTNodeFactory.newModifiers(ast, info.getNewModifiersForCopyInSubclass(oldField.getModifiers()))); Type oldType= oldField.getType(); ICompilationUnit cu= field.getCompilationUnit(); Type newType= null; if (mapping.length > 0) { newType= createPlaceholderForType(oldType, cu, mapping, rewrite); } else newType= createPlaceholderForType(oldType, cu, rewrite); newField.setType(newType); return newField; }
Example 12
Source File: SelfEncapsulateFieldRefactoring.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
private MethodDeclaration createGetterMethod(AST ast, ASTRewrite rewriter, String lineDelimiter) throws CoreException { FieldDeclaration field= (FieldDeclaration)ASTNodes.getParent(fFieldDeclaration, FieldDeclaration.class); Type type= field.getType(); MethodDeclaration result= ast.newMethodDeclaration(); result.setName(ast.newSimpleName(fGetterName)); result.modifiers().addAll(ASTNodeFactory.newModifiers(ast, createModifiers())); Type returnType= DimensionRewrite.copyTypeAndAddDimensions(type, fFieldDeclaration.extraDimensions(), rewriter); result.setReturnType2(returnType); Block block= ast.newBlock(); result.setBody(block); String body= CodeGeneration.getGetterMethodBodyContent(fField.getCompilationUnit(), getTypeName(field.getParent()), fGetterName, fField.getElementName(), lineDelimiter); if (body != null) { ASTNode getterNode= rewriter.createStringPlaceholder(body, ASTNode.BLOCK); block.statements().add(getterNode); } else { ReturnStatement rs= ast.newReturnStatement(); rs.setExpression(ast.newSimpleName(fField.getElementName())); block.statements().add(rs); } if (fGenerateJavadoc) { String string= CodeGeneration.getGetterComment( fField.getCompilationUnit() , getTypeName(field.getParent()), fGetterName, fField.getElementName(), ASTNodes.asString(type), StubUtility.getBaseName(fField), lineDelimiter); if (string != null) { Javadoc javadoc= (Javadoc)fRewriter.createStringPlaceholder(string, ASTNode.JAVADOC); result.setJavadoc(javadoc); } } return result; }
Example 13
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 14
Source File: SelfEncapsulateFieldRefactoring.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 4 votes |
private MethodDeclaration createSetterMethod(AST ast, ASTRewrite rewriter, String lineDelimiter) throws CoreException { FieldDeclaration field = ASTNodes.getParent(fFieldDeclaration, FieldDeclaration.class); Type type = field.getType(); MethodDeclaration result = ast.newMethodDeclaration(); result.setName(ast.newSimpleName(fSetterName)); result.modifiers().addAll(ASTNodeFactory.newModifiers(ast, createModifiers())); if (fSetterMustReturnValue) { result.setReturnType2((Type) rewriter.createCopyTarget(type)); } SingleVariableDeclaration param = ast.newSingleVariableDeclaration(); result.parameters().add(param); param.setName(ast.newSimpleName(fArgName)); param.setType((Type) rewriter.createCopyTarget(type)); List<Dimension> extraDimensions = DimensionRewrite.copyDimensions(fFieldDeclaration.extraDimensions(), rewriter); param.extraDimensions().addAll(extraDimensions); Block block = ast.newBlock(); result.setBody(block); String fieldAccess = createFieldAccess(); String body = CodeGeneration.getSetterMethodBodyContent(fField.getCompilationUnit(), getTypeName(field.getParent()), fSetterName, fieldAccess, fArgName, lineDelimiter); if (body != null) { body = body.substring(0, body.lastIndexOf(lineDelimiter)); ASTNode setterNode = rewriter.createStringPlaceholder(body, ASTNode.BLOCK); block.statements().add(setterNode); } else { Assignment ass = ast.newAssignment(); ass.setLeftHandSide((Expression) rewriter.createStringPlaceholder(fieldAccess, ASTNode.QUALIFIED_NAME)); ass.setRightHandSide(ast.newSimpleName(fArgName)); block.statements().add(ass); } if (fSetterMustReturnValue) { ReturnStatement rs = ast.newReturnStatement(); rs.setExpression(ast.newSimpleName(fArgName)); block.statements().add(rs); } if (fGenerateJavadoc) { String string = CodeGeneration.getSetterComment(fField.getCompilationUnit(), getTypeName(field.getParent()), fSetterName, fField.getElementName(), ASTNodes.asString(type), fArgName, StubUtility.getBaseName(fField), lineDelimiter); if (string != null) { Javadoc javadoc = (Javadoc) fRewriter.createStringPlaceholder(string, ASTNode.JAVADOC); result.setJavadoc(javadoc); } } return result; }
Example 15
Source File: SelfEncapsulateFieldRefactoring.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
private MethodDeclaration createSetterMethod(AST ast, ASTRewrite rewriter, String lineDelimiter) throws CoreException { FieldDeclaration field= (FieldDeclaration)ASTNodes.getParent(fFieldDeclaration, FieldDeclaration.class); Type type= field.getType(); MethodDeclaration result= ast.newMethodDeclaration(); result.setName(ast.newSimpleName(fSetterName)); result.modifiers().addAll(ASTNodeFactory.newModifiers(ast, createModifiers())); if (fSetterMustReturnValue) { result.setReturnType2((Type)rewriter.createCopyTarget(type)); } SingleVariableDeclaration param= ast.newSingleVariableDeclaration(); result.parameters().add(param); param.setName(ast.newSimpleName(fArgName)); param.setType((Type)rewriter.createCopyTarget(type)); List<Dimension> extraDimensions= DimensionRewrite.copyDimensions(fFieldDeclaration.extraDimensions(), rewriter); param.extraDimensions().addAll(extraDimensions); Block block= ast.newBlock(); result.setBody(block); String fieldAccess= createFieldAccess(); String body= CodeGeneration.getSetterMethodBodyContent(fField.getCompilationUnit(), getTypeName(field.getParent()), fSetterName, fieldAccess, fArgName, lineDelimiter); if (body != null) { ASTNode setterNode= rewriter.createStringPlaceholder(body, ASTNode.BLOCK); block.statements().add(setterNode); } else { Assignment ass= ast.newAssignment(); ass.setLeftHandSide((Expression) rewriter.createStringPlaceholder(fieldAccess, ASTNode.QUALIFIED_NAME)); ass.setRightHandSide(ast.newSimpleName(fArgName)); block.statements().add(ass); } if (fSetterMustReturnValue) { ReturnStatement rs= ast.newReturnStatement(); rs.setExpression(ast.newSimpleName(fArgName)); block.statements().add(rs); } if (fGenerateJavadoc) { String string= CodeGeneration.getSetterComment( fField.getCompilationUnit() , getTypeName(field.getParent()), fSetterName, fField.getElementName(), ASTNodes.asString(type), fArgName, StubUtility.getBaseName(fField), lineDelimiter); if (string != null) { Javadoc javadoc= (Javadoc)fRewriter.createStringPlaceholder(string, ASTNode.JAVADOC); result.setJavadoc(javadoc); } } return result; }