Skip to content

Commit

Permalink
Add annotation type to extractor, avoiding class cast exception when …
Browse files Browse the repository at this point in the history
…parsing annotation declarations
  • Loading branch information
Taina Caetano committed Apr 11, 2017
1 parent 970475b commit 90613dc
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.dnfeitosa.codegraph.index.java.internal.javaparser.MethodsCollector;
import com.github.javaparser.JavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.AnnotationDeclaration;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.ast.body.ConstructorDeclaration;
import com.github.javaparser.ast.body.EnumDeclaration;
Expand Down Expand Up @@ -140,8 +141,14 @@ private String getTypeType(TypeDeclaration typeDeclaration) {
if (typeDeclaration instanceof EnumDeclaration) {
return "enum";
}
ClassOrInterfaceDeclaration coid = (ClassOrInterfaceDeclaration) typeDeclaration;
return coid.isInterface() ? "interface" : "class";
if (typeDeclaration instanceof ClassOrInterfaceDeclaration) {
ClassOrInterfaceDeclaration coid = (ClassOrInterfaceDeclaration) typeDeclaration;
return coid.isInterface() ? "interface" : "class";
}
if (typeDeclaration instanceof AnnotationDeclaration) {
return "annotation";
}
throw new IllegalArgumentException("Invalid declaration. How did Java allow this?");
}

private CompilationUnit getCompilationUnitFrom(File file) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,22 @@ public void shouldExtractAnEnum() throws Exception {

assertThat(type.getName(), is("SomeEnum"));
assertThat(type.getPackageName(), is("com.dnfeitosa.codegraph"));
assertThat(type.getType(), is("enum"));
assertNull(type.getSuperclass());
}

@Test
public void shouldExtractAnAnnotation() throws Exception {
File sourceFile = new File(getClass().getResource("/sources/SomeAnnotation.java").getFile());

PackageResolver packageResolver = new CompositePackageResolver(new DefaultPackageResolver(), new JavaLangPackageResolver());
List<Type> types = typesExtractor.parseTypes(sourceFile, packageResolver);

Type type = types.get(0);

// assertThat(type.getFields().size(), is(2));
// assertThat(type.getFields().get(0).getName(), is("VALUE1"));
// assertThat(type.getFields().get(0).getType().getName(), is("com.dnfeitosa.codegraph"));
// assertThat(type.getFields().get(0).getType().getPackageName(), is("VALUE1"));
// assertThat(type.getFields().get(1).getType().getPackageName(), is("com.dnfeitosa.codegraph"));
assertThat(type.getName(), is("SomeAnnotation"));
assertThat(type.getPackageName(), is("com.dnfeitosa.codegraph"));
assertThat(type.getType(), is("annotation"));
assertNull(type.getSuperclass());
}
}
5 changes: 5 additions & 0 deletions codegraph-java/src/test/resources/sources/SomeAnnotation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.dnfeitosa.codegraph;

@interface SomeAnnotation {
String ahPoxa();
}

0 comments on commit 90613dc

Please sign in to comment.