Search in sources :

Example 86 with FieldDeclaration

use of org.eclipse.jdt.core.dom.FieldDeclaration in project eclipse-pmd by acanda.

the class SingularFieldQuickFix method updateFieldDeclaration.

/**
 * Updates the field declaration. If the replaced field was the only fragment, the entire field declaration is
 * removed. Otherwise the field declaration stays and only the respective fragment is removed.
 */
private void updateFieldDeclaration(final VariableDeclarationFragment node) {
    final FieldDeclaration fieldDeclaration = (FieldDeclaration) node.getParent();
    @SuppressWarnings("unchecked") final List<VariableDeclarationFragment> fragments = fieldDeclaration.fragments();
    if (fragments.size() > 1) {
        for (final VariableDeclarationFragment fragment : fragments) {
            if (fragment.getName().getIdentifier().equals(node.getName().getIdentifier())) {
                fragment.delete();
            }
        }
    } else {
        fieldDeclaration.delete();
    }
}
Also used : VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration)

Example 87 with FieldDeclaration

use of org.eclipse.jdt.core.dom.FieldDeclaration in project AutoRefactor by JnRouvignac.

the class ASTNodeFactory method newFieldDeclaration.

/**
 * Builds a new {@link FieldDeclaration} instance.
 *
 * @param type     the declared variable type
 * @param fragment the variable declaration fragment
 * @return a new field declaration
 */
public FieldDeclaration newFieldDeclaration(final Type type, final VariableDeclarationFragment fragment) {
    FieldDeclaration fd = ast.newFieldDeclaration(fragment);
    fd.setType(type);
    return fd;
}
Also used : FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration)

Example 88 with FieldDeclaration

use of org.eclipse.jdt.core.dom.FieldDeclaration in project xtext-xtend by eclipse.

the class JavaASTFlattener method visit.

@Override
public boolean visit(final Initializer it) {
    Javadoc _javadoc = it.getJavadoc();
    boolean _tripleNotEquals = (_javadoc != null);
    if (_tripleNotEquals) {
        it.getJavadoc().accept(this);
    }
    this.appendModifiers(it, it.modifiers());
    boolean _isStatic = this._aSTFlattenerUtils.isStatic(it.modifiers());
    if (_isStatic) {
        if (((it.getParent() instanceof TypeDeclaration) && IterableExtensions.<FieldDeclaration>forall(IterableExtensions.<FieldDeclaration>filter(((Iterable<FieldDeclaration>) Conversions.doWrapArray(((TypeDeclaration) it.getParent()).getFields())), ((Function1<FieldDeclaration, Boolean>) (FieldDeclaration it_1) -> {
            return Boolean.valueOf((this._aSTFlattenerUtils.isStatic(it_1.modifiers()) && this._aSTFlattenerUtils.isFinal(it_1.modifiers())));
        })), ((Function1<FieldDeclaration, Boolean>) (FieldDeclaration f) -> {
            final Function1<VariableDeclarationFragment, Boolean> _function = (VariableDeclarationFragment fragment) -> {
                Boolean _isAssignedInBody = this._aSTFlattenerUtils.isAssignedInBody(it.getBody(), fragment);
                return Boolean.valueOf((!(_isAssignedInBody).booleanValue()));
            };
            return Boolean.valueOf(IterableExtensions.<VariableDeclarationFragment>forall(f.fragments(), _function));
        })))) {
            this.appendToBuffer(" final Void static_initializer = {");
            this.appendLineWrapToBuffer();
            it.getBody().accept(this);
            this.appendToBuffer("null }");
            this.appendLineWrapToBuffer();
        } else {
            this.addProblem(it, "Static initializer is not fully supported");
            this.appendToBuffer("{/*FIXME ");
            it.getBody().accept(this);
            this.appendToBuffer("*/}");
        }
    } else {
        ASTNode _parent = it.getParent();
        if ((_parent instanceof AnonymousClassDeclaration)) {
            StringConcatenation _builder = new StringConcatenation();
            _builder.append("Initializer is not supported in ");
            String _simpleName = ASTNode.nodeClassForType(it.getParent().getNodeType()).getSimpleName();
            _builder.append(_simpleName);
            this.addProblem(it, _builder.toString());
        }
        it.getBody().accept(this);
    }
    return false;
}
Also used : VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) Function1(org.eclipse.xtext.xbase.lib.Functions.Function1) ASTNode(org.eclipse.jdt.core.dom.ASTNode) AnonymousClassDeclaration(org.eclipse.jdt.core.dom.AnonymousClassDeclaration) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) Javadoc(org.eclipse.jdt.core.dom.Javadoc) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration) AnnotationTypeDeclaration(org.eclipse.jdt.core.dom.AnnotationTypeDeclaration) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration)

Example 89 with FieldDeclaration

use of org.eclipse.jdt.core.dom.FieldDeclaration in project xtext-xtend by eclipse.

the class ASTFlattenerUtils method findDeclaredType.

private Type findDeclaredType(final ASTNode scope, final SimpleName simpleName) {
    final ArrayList<Type> matchesFound = CollectionLiterals.<Type>newArrayList();
    scope.accept(new ASTVisitor() {

        @Override
        public boolean visit(final VariableDeclarationFragment node) {
            boolean _equals = node.getName().getIdentifier().equals(simpleName.getIdentifier());
            if (_equals) {
                final ASTNode parentNode = node.getParent();
                boolean _matched = false;
                if (parentNode instanceof VariableDeclarationStatement) {
                    _matched = true;
                    matchesFound.add(((VariableDeclarationStatement) parentNode).getType());
                }
                if (!_matched) {
                    if (parentNode instanceof FieldDeclaration) {
                        _matched = true;
                        matchesFound.add(((FieldDeclaration) parentNode).getType());
                    }
                }
                if (!_matched) {
                    if (parentNode instanceof VariableDeclarationExpression) {
                        _matched = true;
                        matchesFound.add(((VariableDeclarationExpression) parentNode).getType());
                    }
                }
            }
            return false;
        }

        @Override
        public boolean preVisit2(final ASTNode node) {
            return matchesFound.isEmpty();
        }

        @Override
        public boolean visit(final SingleVariableDeclaration node) {
            boolean _equals = node.getName().getIdentifier().equals(simpleName.getIdentifier());
            if (_equals) {
                matchesFound.add(node.getType());
            }
            return false;
        }
    });
    return IterableExtensions.<Type>head(matchesFound);
}
Also used : Type(org.eclipse.jdt.core.dom.Type) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) ASTNode(org.eclipse.jdt.core.dom.ASTNode) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration) ASTVisitor(org.eclipse.jdt.core.dom.ASTVisitor)

Example 90 with FieldDeclaration

use of org.eclipse.jdt.core.dom.FieldDeclaration in project eap-additional-testsuite by jboss-set.

the class MethodInfo method parse.

public static void parse(String str) throws IOException {
    types.clear();
    fields.clear();
    methods.clear();
    classInstanceCreations.clear();
    typesNotResolved.clear();
    methodsNotResolved.clear();
    methodInvocations.clear();
    imports.clear();
    ASTParser parser = ASTParser.newParser(AST.JLS3);
    parser.setSource(readFileToString(str).toCharArray());
    parser.setKind(ASTParser.K_COMPILATION_UNIT);
    final CompilationUnit cu = (CompilationUnit) parser.createAST(null);
    HashMap<String, String> declarations = new HashMap();
    cu.accept(new ASTVisitor() {

        Set names = new HashSet();

        int blockCount = 0;

        public boolean visit(FieldDeclaration node) {
            String name = node.fragments().get(0).toString().split("=")[0].trim();
            String type = node.getType().toString();
            declarations.put(name, type);
            if (!node.modifiers().toString().contains("private")) {
                fields.put(name, type);
            }
            ArrayList<String> types0 = new ArrayList<>();
            String type2 = null;
            do {
                if (type.contains("[")) {
                    type = type.replaceAll("\\[\\]", "");
                    if (type.contains("[")) {
                        type = type.substring(0, type.indexOf("["));
                    }
                }
                if (type.contains("<")) {
                    String type3 = type;
                    type = type.substring(0, type.indexOf("<"));
                    if (type3.substring(type3.indexOf("<") + 1).startsWith("<>") || type3.substring(type.indexOf("<") + 1).startsWith("<T>")) {
                        type2 = null;
                    } else {
                        type2 = type3.substring(type3.indexOf("<") + 1, type3.lastIndexOf(">"));
                        if (type2.contains(",")) {
                            if (type2.substring(0, type2.indexOf(",")).contains("<")) {
                                types0.add(type2);
                            } else {
                                types0.add(type2.substring(0, type2.indexOf(",")));
                                types0.add(type2.substring(type2.indexOf(",") + 1));
                            }
                        } else {
                            types0.add(type2);
                        }
                    }
                }
                types.addAll(Arrays.asList(type.split(" extends ")));
                if (types0.size() != 0) {
                    type = types0.remove(0);
                } else {
                    type = null;
                }
            } while (type != null);
            return false;
        }

        public boolean visit(EnumConstantDeclaration node) {
            String type = node.getName().toString();
            // System.out.println("ccccc : " + type);
            List<String> constants = node.arguments();
            for (String f : constants) {
                // System.out.println("ccccc : " + type + "." + f);
                declarations.put(type + "." + f, type);
                if (!node.modifiers().toString().contains("private")) {
                    fields.put(type + "." + f, type);
                }
            }
            return false;
        }

        public boolean visit(MethodDeclaration node) {
            if (node.getName().getIdentifier() != null) {
                String returnType = null;
                if (node.getReturnType2() == null) {
                    returnType = "";
                } else {
                    returnType = node.getReturnType2().toString();
                }
                HashMap<String, String> bDeclarations = new HashMap();
                bDeclarations.putAll(importedClassFields);
                bDeclarations.putAll(declarations);
                methods.put(node.getName().toString() + "_Return_Type", new String[] { returnType });
                String[] methodParams = new String[node.parameters().size()];
                List params = node.parameters();
                int i = 0;
                for (Object s : params) {
                    String type = ((SingleVariableDeclaration) s).getType().toString();
                    bDeclarations.put(((SingleVariableDeclaration) s).getName().toString(), type);
                    ArrayList<String> types0 = new ArrayList<>();
                    String type2 = null;
                    String typeF = null;
                    do {
                        if (type.contains("[")) {
                            type = type.replaceAll("\\[\\]", "");
                            if (type.contains("[")) {
                                type = type.substring(0, type.indexOf("["));
                            }
                        } else if (type.contains("<")) {
                            String type3 = type;
                            type = type.substring(0, type.indexOf("<"));
                            if (type3.substring(type3.indexOf("<") + 1).startsWith("<>") || type3.substring(type.indexOf("<") + 1).startsWith("<T>")) {
                                type2 = null;
                            } else {
                                type2 = type3.substring(type3.indexOf("<") + 1, type3.lastIndexOf(">"));
                                if (type2.contains(",")) {
                                    if (type2.substring(0, type2.indexOf(",")).contains("<")) {
                                        types0.add(type2);
                                    } else {
                                        types0.add(type2.substring(0, type2.indexOf(",")));
                                        types0.add(type2.substring(type2.indexOf(",") + 1));
                                    }
                                } else {
                                    types0.add(type2);
                                }
                            }
                        }
                        types.addAll(Arrays.asList(type.split(" extends ")));
                        if (types0.size() != 0) {
                            type = types0.remove(0);
                            typeF = type;
                        } else {
                            type = null;
                        }
                    } while (type != null);
                    methodParams[i++] = typeF;
                }
                methods.put(node.getName().toString() + "_Return_Type", methodParams);
                Block block = node.getBody();
                blockIterate(block, cu, bDeclarations);
            }
            return false;
        }

        public boolean visit(ImportDeclaration node) {
            imports.add(node.getName().toString());
            if (DependencyTreeMethods.jarClassPaths.containsKey(node.getName().toString())) {
                importedClassFields = DependencyTreeMethods.listFieldsOfJarClass(DependencyTreeMethods.jarClassPaths.get(node.getName().toString()), node.getName().toString());
                fields.putAll(importedClassFields);
            }
            return false;
        }

        public boolean visit(PackageDeclaration node) {
            packageName = node.getName().toString();
            return true;
        }
    });
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) ArrayList(java.util.ArrayList) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration) ASTVisitor(org.eclipse.jdt.core.dom.ASTVisitor) EnumConstantDeclaration(org.eclipse.jdt.core.dom.EnumConstantDeclaration) ImportDeclaration(org.eclipse.jdt.core.dom.ImportDeclaration) Block(org.eclipse.jdt.core.dom.Block) ArrayList(java.util.ArrayList) List(java.util.List) ASTParser(org.eclipse.jdt.core.dom.ASTParser) PackageDeclaration(org.eclipse.jdt.core.dom.PackageDeclaration) HashSet(java.util.HashSet)

Aggregations

FieldDeclaration (org.eclipse.jdt.core.dom.FieldDeclaration)103 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)53 ASTNode (org.eclipse.jdt.core.dom.ASTNode)51 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)46 AST (org.eclipse.jdt.core.dom.AST)26 AbstractTypeDeclaration (org.eclipse.jdt.core.dom.AbstractTypeDeclaration)26 BodyDeclaration (org.eclipse.jdt.core.dom.BodyDeclaration)26 Type (org.eclipse.jdt.core.dom.Type)24 TypeDeclaration (org.eclipse.jdt.core.dom.TypeDeclaration)22 Block (org.eclipse.jdt.core.dom.Block)17 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)16 SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)16 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)16 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)16 ArrayList (java.util.ArrayList)15 SimpleName (org.eclipse.jdt.core.dom.SimpleName)15 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)14 Expression (org.eclipse.jdt.core.dom.Expression)14 ListRewrite (org.eclipse.jdt.core.dom.rewrite.ListRewrite)14 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)13