Search in sources :

Example 11 with TypeDeclaration

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

the class TestSourceDetection method testNamedList.

@Test
public void testNamedList() throws FileNotFoundException {
    final File file = new File(SOURCE, "Test2_Named.java");
    final CompilationUnit cu = JavaParserProvider.parse(file);
    final Map<String, TypeDeclaration<?>> named = TraceReadUtils.getNamedClasses(cu, "");
    Assert.assertEquals(3, named.size());
    MatcherAssert.assertThat(named.get("Test2_Named$MyStuff").toString(), Matchers.containsString("stuff 1"));
    MatcherAssert.assertThat(named.get("Test2_Named$MyStuff2").toString(), Matchers.containsString("stuff 2"));
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) File(java.io.File) TypeDeclaration(com.github.javaparser.ast.body.TypeDeclaration) Test(org.junit.jupiter.api.Test)

Example 12 with TypeDeclaration

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

the class MethodReader method getMethod.

public CallableDeclaration<?> getMethod(final Node node, final TraceElementContent currentTraceElement) {
    if (node != null && node.getParentNode().isPresent()) {
        final Node parent = node.getParentNode().get();
        if (node instanceof MethodDeclaration) {
            final MethodDeclaration method = (MethodDeclaration) node;
            if (method.getNameAsString().equals(currentTraceElement.getMethod())) {
                // TODO LOG.trace
                LOG.trace("Parameter: {} Trace-Parameter: {}", method.getParameters().size(), currentTraceElement.getParameterTypes().length);
                // TODO delete
                LOG.trace(method.getParameters());
                LOG.trace(Arrays.toString(currentTraceElement.getParameterTypes()));
                if (new ParameterComparator(this.clazz).parametersEqual(currentTraceElement, method)) {
                    if (parent instanceof TypeDeclaration<?>) {
                        final TypeDeclaration<?> clazz = (TypeDeclaration<?>) parent;
                        final String clazzName = clazz.getNameAsString();
                        if (clazzName.equals(currentTraceElement.getSimpleClazz())) {
                            return method;
                        }
                    } else {
                        return method;
                    }
                }
            }
        } else if (node instanceof ConstructorDeclaration) {
            if ("<init>".equals(currentTraceElement.getMethod())) {
                if (parent instanceof TypeDeclaration<?>) {
                    final ConstructorDeclaration constructor = (ConstructorDeclaration) node;
                    final TypeDeclaration<?> clazz = (TypeDeclaration<?>) parent;
                    LOG.trace(clazz.getNameAsString() + " " + currentTraceElement.getClazz());
                    if (clazz.getNameAsString().equals(currentTraceElement.getSimpleClazz())) {
                        if (new ParameterComparator(this.clazz).parametersEqual(currentTraceElement, constructor)) {
                            return (CallableDeclaration) node;
                        }
                    }
                }
                LOG.trace(parent);
            }
        }
        for (final Node child : node.getChildNodes()) {
            final CallableDeclaration possibleMethod = getMethod(child, currentTraceElement);
            if (possibleMethod != null) {
                return possibleMethod;
            }
        }
    }
    return null;
}
Also used : MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) ConstructorDeclaration(com.github.javaparser.ast.body.ConstructorDeclaration) Node(com.github.javaparser.ast.Node) CallableDeclaration(com.github.javaparser.ast.body.CallableDeclaration) TypeDeclaration(com.github.javaparser.ast.body.TypeDeclaration)

Example 13 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 14 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 15 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)

Aggregations

TypeDeclaration (com.github.javaparser.ast.body.TypeDeclaration)44 CompilationUnit (com.github.javaparser.ast.CompilationUnit)22 Node (com.github.javaparser.ast.Node)14 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)13 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)12 BodyDeclaration (com.github.javaparser.ast.body.BodyDeclaration)10 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 EnumDeclaration (com.github.javaparser.ast.body.EnumDeclaration)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