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"));
}
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;
}
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;
}
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;
}
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;
}
Aggregations