Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -239,19 +239,28 @@ public void endVisitInitializer(Initializer node) {

@SuppressWarnings("unchecked")
public boolean visitEnumConstantDeclaration(EnumConstantDeclaration node) {
boolean hasInitBlock = false;
boolean hasInitBlock = (node.getAnonymousClassDeclaration() != null);

for (Expression expr : (List<Expression>)node.arguments()) {
if (expr != null) {
visitClassMemberInitializer(expr);
hasInitBlock = true;
break; // we recovered the INIT_BLOCK, no need to look for other declaration
}
}
return hasInitBlock;

if (hasInitBlock) {
ctxtPushInitializerMethod();
}
return hasInitBlock;
}

@SuppressWarnings("unchecked")
public void endVisitEnumConstantDeclaration(EnumConstantDeclaration node) {
if (node.getAnonymousClassDeclaration() != null) {
context.pop(); // pops the EntityDictionary.INIT_BLOCK_NAME method
return;
}

for (Expression expr : (List<Expression>)node.arguments()) {
if (expr != null) {
context.pop(); // pops the EntityDictionary.INIT_BLOCK_NAME method
Expand Down Expand Up @@ -297,7 +306,7 @@ private Method visitClassMemberInitializer(Expression initializingExpr) {
*
* Used in the case of instance/class initializer and initializing expressions of FieldDeclarations and EnumConstantDeclarations
*/
private Method ctxtPushInitializerMethod() {
protected Method ctxtPushInitializerMethod() {
TType owner = context.topType();
Method fmx = recoverInitializerMethod((TWithMethods)owner);
if (fmx == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,12 +276,8 @@ public void endVisit(FieldDeclaration node) {
endVisitFieldDeclaration(node);
}

@SuppressWarnings("unchecked")
public boolean visit(EnumConstantDeclaration node) {
if (visitEnumConstantDeclaration(node)) {
visitNodeList(node.arguments());
}
return false;
return visitEnumConstantDeclaration(node);
}

public void endVisit(EnumConstantDeclaration node) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,14 @@ public void endVisit(FieldDeclaration node) {
endVisitFieldDeclaration(node);
}

public boolean visit(EnumConstantDeclaration node) {
return visitEnumConstantDeclaration(node);
}

public void endVisit(EnumConstantDeclaration node) {
endVisitEnumConstantDeclaration(node);
}

@SuppressWarnings("unchecked")
public boolean visit(MethodInvocation node) {
//System.err.println("visit(MethodInvocation): " + node.getName().getFullyQualifiedName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,14 @@ public void endVisit(FieldDeclaration node) {
endVisitFieldDeclaration(node);
}

public boolean visit(EnumConstantDeclaration node) {
return visitEnumConstantDeclaration(node);
}

public void endVisit(EnumConstantDeclaration node) {
endVisitEnumConstantDeclaration(node);
}

@Override
/* We are not dealing with the variable of the catch here but in VisitorExceptionRef
* therefore we only visit the body of the catch
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,10 @@ public void testUnresolvedDeclaration() {

@ Test
public void testConstructorInvocations() {
parse(new String[] {"src/test/resources/ad_hoc/DefaultConstructor.java", "src/test/resources/ad_hoc/InvokWithFullPath.java", "src/test/resources/ad_hoc/annotations/Book.java"});
parse(new String[] {
"src/test/resources/ad_hoc/DefaultConstructor.java",
"src/test/resources/ad_hoc/InvokWithFullPath.java",
"src/test/resources/ad_hoc/annotations/Book.java"});

Method meth = detectFamixElement( Method.class, "methodWithClassScope");
assertNotNull(meth);
Expand Down Expand Up @@ -564,7 +567,7 @@ public void testEnumAccess() {
assertEquals("toString", ((TNamedEntity) access.getAccessor()).getName());
}
}
assertTrue("Did not find CUBS EnumValue in Suit Enum", foundClubs);
assertTrue("Did not find 'CLUBS' EnumValue in 'Suit' Enum", foundClubs);

org.moosetechnology.model.famix.famixjavaentities.Enum pl = detectFamixElement(org.moosetechnology.model.famix.famixjavaentities.Enum.class, "Planet");
assertNotNull(pl);
Expand Down Expand Up @@ -662,7 +665,11 @@ public void testReadWriteAccess() {

@Test
public void testStaticInitializationBlock() {
parse(new String[] {"src/test/resources/ad_hoc/Card.java", "src/test/resources/ad_hoc/Planet.java", "src/test/resources/ad_hoc/InvokWithFullPath.java", "src/test/resources/ad_hoc/DefaultConstructor.java"});
parse(new String[] {
"src/test/resources/ad_hoc/Card.java",
"src/test/resources/ad_hoc/Planet.java",
"src/test/resources/ad_hoc/InvokWithFullPath.java",
"src/test/resources/ad_hoc/DefaultConstructor.java"});

Collection<Method> l_meth = entitiesNamed( Method.class, EntityDictionary.INIT_BLOCK_NAME);
assertEquals(3, l_meth.size());
Expand Down Expand Up @@ -696,6 +703,32 @@ public void testStaticInitializationBlockNewString() {
assertEquals("ONE", ((TNamedEntity) firstElt(fmx.getEnumValues())).getName());

assertEquals(2, fmx.getMethods().size()); // constructor + INIT_BLOCK
assertTrue("Enum constructor not found", fmx.getMethods().stream().anyMatch( m -> m.getName().equals("EnumConstWithInitNewString") ) );
assertTrue("Enum initializer method not found", fmx.getMethods().stream().anyMatch( m -> m.getName().equals("<Initializer>") ) );
}

@Test
public void testCastInEnumInitialization() {
parse(new String[]{"src/test/resources/ad_hoc/EnumConstWithInitNewString.java"});

Method fmx = detectFamixElement(Method.class, "<Initializer>");
assertNotNull(fmx);

assertEquals(1, fmx.numberOfOutgoingReferences());
Interface clazz = (Interface) ((Reference)firstElt(fmx.getOutgoingReferences())).getReferredEntity();
assertEquals("CharSequence", clazz.getName());
}

@Test
public void testInvocationInEnumInitialization() {
parse(new String[]{"src/test/resources/ad_hoc/EnumConstWithInitNewString.java"});

Method fmx = detectFamixElement(Method.class, "<Initializer>");
assertNotNull(fmx);

assertEquals(1, fmx.numberOfOutgoingInvocations());
String constructorName = ((Invocation)firstElt(fmx.getOutgoingInvocations())).getSignature();
assertEquals("String(\"whatever\")", constructorName);
}

@Test
Expand Down
15 changes: 4 additions & 11 deletions app/src/test/resources/ad_hoc/EnumConstWithInitNewString.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
package ad_hoc;
/*
* A test copied from jdt2mse (I believe) that happenned to break verveinej
*/

public enum EnumConstWithInitNewString {

ONE(new String("whatever")) {
@Override
void hook() {
string.toString();
}
};
ONE( (CharSequence)new String("whatever") );

String string;
CharSequence string;

EnumConstWithInitNewString(String s) {
EnumConstWithInitNewString(CharSequence s) {
this.string = s;
}

Expand Down