Skip to content

Commit

Permalink
working on classification of names roles
Browse files Browse the repository at this point in the history
  • Loading branch information
ftomassetti committed Aug 4, 2018
1 parent 0f83d70 commit 4e79510
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 2 deletions.
Expand Up @@ -5,9 +5,11 @@
import com.github.javaparser.ast.PackageDeclaration;
import com.github.javaparser.ast.body.*;
import com.github.javaparser.ast.expr.*;
import com.github.javaparser.ast.modules.*;
import com.github.javaparser.ast.stmt.ReturnStmt;
import com.github.javaparser.ast.type.ClassOrInterfaceType;
import com.github.javaparser.ast.type.TypeParameter;
import com.github.javaparser.symbolsolver.core.resolution.Context;

/**
* NameLogic contains a set of static methods to implement the abstraction of a "Name" as defined
Expand Down Expand Up @@ -123,6 +125,33 @@ public static NameRole classifyRole(Node name) {
if (whenParentIs(ReturnStmt.class, name, (p, c) -> p.getExpression().isPresent() && p.getExpression().get() == c)) {
return NameRole.REFERENCE;
}
if (whenParentIs(ConstructorDeclaration.class, name, (p, c) -> p.getName() == c)) {
return NameRole.REFERENCE;
}
if (whenParentIs(ModuleDeclaration.class, name, (p, c) -> p.getName() == c)) {
return NameRole.DECLARATION;
}
if (whenParentIs(ModuleRequiresStmt.class, name, (p, c) -> p.getName() == c)) {
return NameRole.REFERENCE;
}
if (whenParentIs(ModuleExportsStmt.class, name, (p, c) -> p.getName() == c)) {
return NameRole.REFERENCE;
}
if (whenParentIs(ModuleExportsStmt.class, name, (p, c) -> p.getModuleNames().contains(c))) {
return NameRole.REFERENCE;
}
if (whenParentIs(ModuleOpensStmt.class, name, (p, c) -> p.getName() == c)) {
return NameRole.REFERENCE;
}
if (whenParentIs(ModuleOpensStmt.class, name, (p, c) -> p.getModuleNames().contains(c))) {
return NameRole.REFERENCE;
}
if (whenParentIs(ModuleUsesStmt.class, name, (p, c) -> p.getName() == c)) {
return NameRole.REFERENCE;
}
if (whenParentIs(ModuleProvidesStmt.class, name, (p, c) -> p.getName() == c)) {
return NameRole.REFERENCE;
}
if (name.getParentNode().isPresent() && NameLogic.isAName(name.getParentNode().get())) {
return classifyRole(name.getParentNode().get());
}
Expand Down
Expand Up @@ -101,7 +101,16 @@ public void identifyNameRolesInSimpleExamples() {
.getScope().asFieldAccessExpr()
.getScope().asFieldAccessExpr()
.getScope())); // a
}
}

@Test
public void classifyRoleModuleName() {
assertNameInCodeHasRole("module com.mydeveloperplanet.jpmshello {\n" +
" requires java.base;\n" +
" requires java.xml;\n" +
" requires com.mydeveloperplanet.jpmshi;\n" +
"}\n", "com.mydeveloperplanet.jpmshello", DECLARATION, ParseStart.MODULE_DECLARATION);
}

@Test
public void classifyRoleRequiresModuleName() {
Expand Down Expand Up @@ -192,6 +201,12 @@ public void classifyRoleAnnotationTypeName() {
REFERENCE, ParseStart.COMPILATION_UNIT);
}

@Test
public void classifyRoleClassName() {
assertNameInCodeHasRole("@Anno class A {} ", "A",
DECLARATION, ParseStart.COMPILATION_UNIT);
}

@Test
public void classifyRoleClassLiteralTypeName() {
assertNameInCodeHasRole("Class<?> c = String.class;", "String",
Expand Down Expand Up @@ -246,6 +261,12 @@ public void classifyRoleQualifiedAnnotationMemberTypeTypeName() {
REFERENCE, ParseStart.COMPILATION_UNIT);
}

@Test
public void classifyRoleAnnotationName() {
assertNameInCodeHasRole("@interface MyAnno { bar.MyClass myMember(); }", "MyAnno",
DECLARATION, ParseStart.COMPILATION_UNIT);
}

@Test
public void classifyRoleUnqualifiedAnnotationMemberTypeTypeName() {
assertNameInCodeHasRole("@interface MyAnno { MyClass myMember(); }", "MyClass",
Expand Down Expand Up @@ -289,6 +310,12 @@ public void classifyRoleUnqualifiedFieldTypeTypeName() {
REFERENCE, ParseStart.COMPILATION_UNIT);
}

@Test
public void classifyRoleFieldName() {
assertNameInCodeHasRole("class Foo { MyClass myField; }", "myField",
DECLARATION, ParseStart.COMPILATION_UNIT);
}

@Test
public void classifyRoleQualifiedFormalParameterOfMethodTypeName() {
assertNameInCodeHasRole("class Foo { void myMethod(bar.MyClass param) {} }", "bar.MyClass",
Expand All @@ -301,6 +328,12 @@ public void classifyRoleUnqualifiedFormalParameterOfMethodTypeName() {
REFERENCE, ParseStart.COMPILATION_UNIT);
}

@Test
public void classifyRoleMethodName() {
assertNameInCodeHasRole("class Foo { void myMethod(MyClass param) {} }", "myMethod",
DECLARATION, ParseStart.COMPILATION_UNIT);
}

@Test
public void classifyRoleReceiverParameterOfMethodTypeName() {
assertNameInCodeHasRole("void myMethod(Foo this) {}", "Foo",
Expand All @@ -319,6 +352,12 @@ public void classifyRoleExceptionParameterTypeTypeName() {
REFERENCE, ParseStart.CLASS_BODY);
}

@Test
public void classifyRoleExceptionParameterName() {
assertNameInCodeHasRole("void myMethod() { try { } catch(Foo e) { } }", "e",
DECLARATION, ParseStart.CLASS_BODY);
}

@Test
public void classifyRoleExplicitParameterTypeInConstructorCallTypeName() {
assertNameInCodeHasRole("void myMethod() { new Call<Foo>(); }", "Foo",
Expand Down Expand Up @@ -404,11 +443,17 @@ public void classifyRoleVariableAccessInTryWithResourceExpressionName() {
}

@Test
public void classifyRoleVariableAccessInTryWithResourceWothTypeExpressionName() {
public void classifyRoleVariableAccessInTryWithResourceWithTypeExpressionName() {
assertNameInCodeHasRole("class Bar { Bar() { try (Object o = anExpression) { }; } } ", "anExpression",
REFERENCE, ParseStart.COMPILATION_UNIT);
}

@Test
public void classifyTryWithResourceName() {
assertNameInCodeHasRole("class Bar { Bar() { try (Object o = anExpression) { }; } } ", "o",
DECLARATION, ParseStart.COMPILATION_UNIT);
}

@Test
public void classifyRoleMethodInvocationMethodName() {
assertNameInCodeHasRole("class Bar { Bar() { myMethod(); } } ", "myMethod",
Expand Down

0 comments on commit 4e79510

Please sign in to comment.