Search in sources :

Example 11 with PackageDeclaration

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

the class SourceParser method parse.

public static void parse(String str) throws IOException {
    fields.clear();
    methods.clear();
    imports.clear();
    types.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);
    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();
            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 if (type3.indexOf("<") >= 0 && type3.indexOf(">") >= 0 && type3.indexOf("<") < type3.indexOf(">")) {
                        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 true;
        }

        public boolean visit(MethodDeclaration node) {
            if (node.getName().getIdentifier() != null) {
                MethodInfo2 mf = new MethodInfo2();
                mf.name = node.getName().toString();
                if (node.getReturnType2() != null) {
                    mf.returnType = node.getReturnType2().toString();
                } else {
                    mf.returnType = null;
                }
                List params = node.parameters();
                ArrayList<String> types = new ArrayList<>();
                // System.out.println("params : " + params.toString());
                for (Object s : params) {
                    String type = ((SingleVariableDeclaration) s).getType().toString();
                    if (type.startsWith("class "))
                        type = type.replaceFirst("class ", "");
                    types.add(type);
                }
                // System.out.println("sourceTypes : " + types.toString());
                mf.paramTypes = types;
                methods.put(mf.name, mf);
            }
            return true;
        }

        public boolean visit(ImportDeclaration node) {
            imports.add(node.getName().toString());
            return true;
        }

        public boolean visit(PackageDeclaration node) {
            packageName = node.getName().toString();
            return true;
        }
    });
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) Set(java.util.Set) HashSet(java.util.HashSet) 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) ImportDeclaration(org.eclipse.jdt.core.dom.ImportDeclaration) 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)

Example 12 with PackageDeclaration

use of org.eclipse.jdt.core.dom.PackageDeclaration in project buck by facebook.

the class JavaFileParser method getPackageNameFromSource.

public Optional<String> getPackageNameFromSource(String code) {
    final CompilationUnit compilationUnit = makeCompilationUnitFromSource(code);
    // A Java file might not have a package. Hopefully all of ours do though...
    PackageDeclaration packageDecl = compilationUnit.getPackage();
    if (packageDecl != null) {
        return Optional.of(packageDecl.getName().toString());
    }
    return Optional.empty();
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) PackageDeclaration(org.eclipse.jdt.core.dom.PackageDeclaration)

Example 13 with PackageDeclaration

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

the class JavaASTFlattener method visit.

@Override
public boolean visit(final CompilationUnit it) {
    boolean _isDummyType = this._aSTFlattenerUtils.isDummyType(IterableExtensions.<AbstractTypeDeclaration>head(it.types()));
    boolean _not = (!_isDummyType);
    if (_not) {
        PackageDeclaration _package = it.getPackage();
        if (_package != null) {
            _package.accept(this);
        }
        this.visitAll(it.imports());
    }
    this.visitAll(it.types());
    return false;
}
Also used : PackageDeclaration(org.eclipse.jdt.core.dom.PackageDeclaration)

Example 14 with PackageDeclaration

use of org.eclipse.jdt.core.dom.PackageDeclaration in project flow by vaadin.

the class CodeTest method gwtGenerics.

private static void gwtGenerics(File file) throws IOException {
    ASTParser parser = ASTParser.newParser(AST.JLS8);
    String value = FileUtils.readFileToString(file, UTF_8);
    parser.setSource(value.toCharArray());
    parser.setKind(ASTParser.K_COMPILATION_UNIT);
    final CompilationUnit cu = (CompilationUnit) parser.createAST(null);
    cu.accept(new ASTVisitor() {

        Set<String> imports = new HashSet<>();

        String packageName;

        @Override
        public boolean visit(PackageDeclaration node) {
            packageName = node.getName().toString();
            return false;
        }

        @Override
        public boolean visit(ImportDeclaration node) {
            imports.add(node.getName().toString());
            return false;
        }

        @Override
        public boolean visit(VariableDeclarationStatement node) {
            for (Object frament : node.fragments()) {
                if (frament instanceof VariableDeclarationFragment) {
                    VariableDeclarationFragment variableDeclaration = (VariableDeclarationFragment) frament;
                    Expression expression = variableDeclaration.getInitializer();
                    if (expression instanceof ClassInstanceCreation) {
                        ClassInstanceCreation classInstanceCreation = (ClassInstanceCreation) expression;
                        Class<?> typeClass = getClass(node.getType());
                        Class<?> instanceClass = getClass(classInstanceCreation.getType());
                        if (typeClass != instanceClass && typeClass.isAssignableFrom(instanceClass)) {
                            fail("Variable type must be the specific implementation in " + node + " in " + file.getName());
                        }
                    }
                }
            }
            return false;
        }

        private Class<?> getClass(Type type) {
            if (type instanceof ArrayType) {
                type = ((ArrayType) type).getElementType();
            }
            if (type instanceof ParameterizedType) {
                type = ((ParameterizedType) type).getType();
            }
            String className = type.toString();
            if (className.indexOf('.') == -1) {
                String dotPrefix = '.' + className;
                for (String i : imports) {
                    if (i.endsWith(dotPrefix)) {
                        className = i;
                        break;
                    }
                }
            }
            Class<?> clas = getClass(className);
            if (clas != null) {
                return clas;
            }
            clas = getClass("java.lang." + className);
            if (clas != null) {
                return clas;
            }
            try {
                String fileName = file.getName();
                fileName = fileName.substring(0, fileName.lastIndexOf('.'));
                if (fileName.equals(className)) {
                    return Class.forName(packageName + '.' + fileName);
                }
                clas = getClass(packageName + '.' + className);
                if (clas != null) {
                    return clas;
                }
                return Class.forName(packageName + '.' + fileName + '$' + className);
            } catch (ClassNotFoundException e) {
                fail("Could not load class " + e);
                return null;
            }
        }

        private Class<?> getClass(String className) {
            try {
                return ClassUtils.getClass(className);
            } catch (ClassNotFoundException e) {
                return null;
            }
        }
    });
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) ASTVisitor(org.eclipse.jdt.core.dom.ASTVisitor) ArrayType(org.eclipse.jdt.core.dom.ArrayType) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) Type(org.eclipse.jdt.core.dom.Type) ArrayType(org.eclipse.jdt.core.dom.ArrayType) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) Expression(org.eclipse.jdt.core.dom.Expression) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ImportDeclaration(org.eclipse.jdt.core.dom.ImportDeclaration) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) ASTParser(org.eclipse.jdt.core.dom.ASTParser) PackageDeclaration(org.eclipse.jdt.core.dom.PackageDeclaration) HashSet(java.util.HashSet)

Example 15 with PackageDeclaration

use of org.eclipse.jdt.core.dom.PackageDeclaration 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

PackageDeclaration (org.eclipse.jdt.core.dom.PackageDeclaration)17 ImportDeclaration (org.eclipse.jdt.core.dom.ImportDeclaration)9 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)8 HashSet (java.util.HashSet)6 ASTVisitor (org.eclipse.jdt.core.dom.ASTVisitor)5 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)5 ASTNode (org.eclipse.jdt.core.dom.ASTNode)4 ASTParser (org.eclipse.jdt.core.dom.ASTParser)4 FieldDeclaration (org.eclipse.jdt.core.dom.FieldDeclaration)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Set (java.util.Set)3 Name (org.eclipse.jdt.core.dom.Name)3 NormalAnnotation (org.eclipse.jdt.core.dom.NormalAnnotation)3 SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)3 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)3 HashMap (java.util.HashMap)2 Nullable (javax.annotation.Nullable)2 AST (org.eclipse.jdt.core.dom.AST)2 AbstractTypeDeclaration (org.eclipse.jdt.core.dom.AbstractTypeDeclaration)2