Search in sources :

Example 21 with TypeDeclaration

use of com.github.javaparser.ast.body.TypeDeclaration in project peass by DaGeRe.

the class TraceReadUtils method getNamedClasses.

public static Map<String, TypeDeclaration<?>> getNamedClasses(final Node parent, final String alreadyReadPrefix) {
    final Map<String, TypeDeclaration<?>> foundDeclaredClasses = new HashMap<>();
    for (final Node child : parent.getChildNodes()) {
        LOG.trace(child.getClass());
        if (child instanceof ClassOrInterfaceDeclaration) {
            final String ownName = ((ClassOrInterfaceDeclaration) child).getNameAsString();
            if (alreadyReadPrefix.equals("")) {
                foundDeclaredClasses.put(ownName, (ClassOrInterfaceDeclaration) child);
                foundDeclaredClasses.putAll(getNamedClasses(child, ownName));
            } else {
                foundDeclaredClasses.put(alreadyReadPrefix + "$" + ownName, (ClassOrInterfaceDeclaration) child);
                foundDeclaredClasses.putAll(getNamedClasses(child, alreadyReadPrefix + "$" + ownName));
            }
        } else if (child instanceof EnumDeclaration) {
            final String ownName = ((EnumDeclaration) child).getNameAsString();
            if (alreadyReadPrefix.equals("")) {
                foundDeclaredClasses.put(ownName, (ClassOrInterfaceDeclaration) child);
                foundDeclaredClasses.putAll(getNamedClasses(child, ownName));
            } else {
                foundDeclaredClasses.put(alreadyReadPrefix + "$" + ownName, (EnumDeclaration) child);
                foundDeclaredClasses.putAll(getNamedClasses(child, alreadyReadPrefix + "$" + ownName));
            }
        } else {
            foundDeclaredClasses.putAll(getNamedClasses(child, alreadyReadPrefix));
        }
    }
    return foundDeclaredClasses;
}
Also used : HashMap(java.util.HashMap) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) Node(com.github.javaparser.ast.Node) TypeDeclaration(com.github.javaparser.ast.body.TypeDeclaration) EnumDeclaration(com.github.javaparser.ast.body.EnumDeclaration)

Example 22 with TypeDeclaration

use of com.github.javaparser.ast.body.TypeDeclaration in project peass by DaGeRe.

the class ClazzFinder method getContainingClazz.

public static String getContainingClazz(final Node statement) {
    String clazz = "";
    Node current = statement;
    while (current.getParentNode().isPresent()) {
        if (current instanceof ClassOrInterfaceDeclaration || current instanceof EnumDeclaration || current instanceof AnnotationDeclaration) {
            TypeDeclaration<?> declaration = (TypeDeclaration<?>) current;
            String name = declaration.getNameAsString();
            if (!clazz.isEmpty()) {
                clazz = name + "$" + clazz;
            } else {
                clazz = name;
            }
        }
        current = current.getParentNode().get();
    }
    return clazz;
}
Also used : ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) AnnotationDeclaration(com.github.javaparser.ast.body.AnnotationDeclaration) Node(com.github.javaparser.ast.Node) TypeDeclaration(com.github.javaparser.ast.body.TypeDeclaration) EnumDeclaration(com.github.javaparser.ast.body.EnumDeclaration)

Example 23 with TypeDeclaration

use of com.github.javaparser.ast.body.TypeDeclaration in project peass by DaGeRe.

the class ClazzFinder method findClazz.

public static TypeDeclaration<?> findClazz(final ChangedEntity entity, final List<Node> nodes) {
    TypeDeclaration<?> declaration = null;
    for (final Node node : nodes) {
        if (node instanceof TypeDeclaration<?>) {
            final TypeDeclaration<?> temp = (TypeDeclaration<?>) node;
            final String nameAsString = temp.getNameAsString();
            if (nameAsString.equals(entity.getSimpleClazzName())) {
                declaration = (ClassOrInterfaceDeclaration) node;
                break;
            } else {
                if (entity.getSimpleClazzName().startsWith(nameAsString + ChangedEntity.CLAZZ_SEPARATOR)) {
                    ChangedEntity inner = new ChangedEntity(entity.getSimpleClazzName().substring(nameAsString.length() + 1), entity.getModule());
                    declaration = findClazz(inner, node.getChildNodes());
                }
            }
        }
    }
    return declaration;
}
Also used : Node(com.github.javaparser.ast.Node) ChangedEntity(de.dagere.peass.dependency.analysis.data.ChangedEntity) TypeDeclaration(com.github.javaparser.ast.body.TypeDeclaration)

Example 24 with TypeDeclaration

use of com.github.javaparser.ast.body.TypeDeclaration in project butterknife by JakeWharton.

the class FinalRClassBuilder method brewJava.

public static void brewJava(File rFile, File outputDir, String packageName, String className) throws Exception {
    CompilationUnit compilationUnit = JavaParser.parse(rFile);
    TypeDeclaration resourceClass = compilationUnit.getTypes().get(0);
    TypeSpec.Builder result = TypeSpec.classBuilder(className).addModifiers(PUBLIC).addModifiers(FINAL);
    for (Node node : resourceClass.getChildNodes()) {
        if (node instanceof ClassOrInterfaceDeclaration) {
            addResourceType(Arrays.asList(SUPPORTED_TYPES), result, (ClassOrInterfaceDeclaration) node);
        }
    }
    JavaFile finalR = JavaFile.builder(packageName, result.build()).addFileComment("Generated code from Butter Knife gradle plugin. Do not modify!").build();
    finalR.writeTo(outputDir);
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) Node(com.github.javaparser.ast.Node) JavaFile(com.squareup.javapoet.JavaFile) TypeDeclaration(com.github.javaparser.ast.body.TypeDeclaration) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 25 with TypeDeclaration

use of com.github.javaparser.ast.body.TypeDeclaration in project checker-framework by typetools.

the class ToIndexFileConverter method extractScene.

/**
 * Entry point of recursive-descent IndexUnit to AScene transformer. It operates by visiting the
 * stub and scene in parallel, descending into them in the same way. It augments the existing
 * scene (it does not create a new scene).
 *
 * @param iu {@link StubUnit} representing stubfile
 */
private static void extractScene(StubUnit iu, AScene scene) {
    for (CompilationUnit cu : iu.getCompilationUnits()) {
        NodeList<TypeDeclaration<?>> typeDecls = cu.getTypes();
        if (typeDecls != null && cu.getPackageDeclaration().isPresent()) {
            List<ImportDeclaration> impDecls = cu.getImports();
            PackageDeclaration pkgDecl = cu.getPackageDeclaration().get();
            for (TypeDeclaration<?> typeDecl : typeDecls) {
                ToIndexFileConverter converter = new ToIndexFileConverter(pkgDecl, impDecls, scene);
                String pkgName = converter.pkgName;
                String name = typeDecl.getNameAsString();
                if (pkgName != null) {
                    name = pkgName + "." + name;
                }
                typeDecl.accept(converter, scene.classes.getVivify(name));
            }
        }
    }
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ImportDeclaration(com.github.javaparser.ast.ImportDeclaration) TypeDeclaration(com.github.javaparser.ast.body.TypeDeclaration) PackageDeclaration(com.github.javaparser.ast.PackageDeclaration)

Aggregations

TypeDeclaration (com.github.javaparser.ast.body.TypeDeclaration)39 CompilationUnit (com.github.javaparser.ast.CompilationUnit)21 Node (com.github.javaparser.ast.Node)14 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)11 BodyDeclaration (com.github.javaparser.ast.body.BodyDeclaration)10 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)9 ResolvedReferenceTypeDeclaration (com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration)8 List (java.util.List)8 Optional (java.util.Optional)8 ResolvedTypeDeclaration (com.github.javaparser.resolution.declarations.ResolvedTypeDeclaration)6 SymbolReference (com.github.javaparser.symbolsolver.model.resolution.SymbolReference)6 Collectors (java.util.stream.Collectors)6 ImportDeclaration (com.github.javaparser.ast.ImportDeclaration)5 NodeList (com.github.javaparser.ast.NodeList)5 ConstructorDeclaration (com.github.javaparser.ast.body.ConstructorDeclaration)5 FieldDeclaration (com.github.javaparser.ast.body.FieldDeclaration)5 ResolvedMethodDeclaration (com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration)5 ResolvedValueDeclaration (com.github.javaparser.resolution.declarations.ResolvedValueDeclaration)5 JavaParserFacade (com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade)5 File (java.io.File)5