Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

review refactor: all non-leaf interfaces of the metamodel should be visited by CtInheritanceScanner #1703

Merged
merged 18 commits into from
Nov 13, 2017
Merged
Show file tree
Hide file tree
Changes from 6 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
4 changes: 2 additions & 2 deletions src/test/java/spoon/generating/RoleHandlersGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,10 @@ public void process() {
}
params.put("$Role$", getFactory().Type().createReference(CtRole.class));
params.put("ROLE", rim.getRole().name());
params.put("$TargetType$", rim.getOwnerType().getModelInteface().getReference());
params.put("$TargetType$", rim.getOwnerType().getModelInterface().getReference());
// params.put("AbstractHandler", getFactory().Type().createReference("spoon.reflect.meta.impl.AbstractRoleHandler"));
params.put("AbstractHandler", getRoleHandlerSuperTypeQName(rim));
params.put("Node", rim.getOwnerType().getModelInteface().getReference());
params.put("Node", rim.getOwnerType().getModelInterface().getReference());
params.put("ValueType", fixMainValueType(getRoleHandlerSuperTypeQName(rim).endsWith("SingleHandler") ? rim.getValueType() : rim.getItemValueType()));
CtClass<?> modelRoleHandlerClass = Substitution.createTypeFromTemplate(
getHandlerName(rim),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import spoon.reflect.visitor.CtScanner;
import spoon.reflect.visitor.filter.AbstractFilter;
import spoon.reflect.visitor.filter.TypeFilter;
import spoon.test.metamodel.SpoonMetaModel;

import java.util.ArrayList;
import java.util.Collection;
Expand All @@ -30,6 +31,7 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static spoon.test.metamodel.MMTypeKind.ABSTRACT;

public class SpoonArchitectureEnforcerTest {

Expand Down Expand Up @@ -217,26 +219,26 @@ public boolean matches(CtClass element) {
public void testInterfacesAreCtScannable() {
// contract: all interface inherited from CtElement should be visited in CtScanner
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the contract of this bug should be:
all non-leaf interfaces of the metamodel should be visited by CtInheritance scanner

the interfaces do not have to inherit from ctelement

Launcher interfaces = new Launcher();
interfaces.addInputResource("src/main/java/spoon/support");
interfaces.addInputResource("src/main/java/spoon/reflect/declaration");
interfaces.addInputResource("src/main/java/spoon/reflect/code");
interfaces.addInputResource("src/main/java/spoon/reflect/reference");
interfaces.addInputResource("src/main/java/spoon/reflect/visitor/CtScanner.java");
interfaces.buildModel();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SpoonMetaModel needs these packages too
"spoon/support/reflect/declaration",
"spoon/support/reflect/code",
"spoon/support/reflect/reference"
Otherwise meta model is not complete.


Collection<CtType<?>> allTypes = interfaces.getModel().getAllTypes();

CtClass<?> ctScanner = interfaces.getFactory().Class().get(CtInheritanceScanner.class);
CtType<?> ctElement = interfaces.getFactory().Type().get(CtElement.class);

List<String> missingMethods = new ArrayList<>();
for (CtType type : allTypes) {
if (type.isSubtypeOf(ctElement.getReference())) {
String methodName = "scan"+type.getSimpleName();

new SpoonMetaModel(interfaces.getFactory()).getMMTypes().forEach(mmType->{
if(mmType.getKind()==ABSTRACT && mmType.getModelInterface() != null) {
CtInterface abstractIface = mmType.getModelInterface();
String methodName = "scan"+abstractIface.getSimpleName();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thes 2 lines above may be replaced by shorter

String methodName = "scan"+mmType.getName();

if (ctScanner.getMethodsByName(methodName).isEmpty()) {
missingMethods.add(methodName);
}
}
}
});

assertTrue("The following methods are missing in CtScanner: \n"+ StringUtils.join(missingMethods, "\n"),missingMethods.isEmpty());
}
Expand Down
17 changes: 14 additions & 3 deletions src/test/java/spoon/test/metamodel/MMType.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@
import spoon.reflect.declaration.CtClass;
import spoon.reflect.declaration.CtInterface;
import spoon.reflect.declaration.CtMethod;
import spoon.reflect.declaration.CtType;
import spoon.reflect.declaration.ModifierKind;
import spoon.reflect.path.CtRole;
import spoon.support.reflect.CtExtendedModifier;
import spoon.support.visitor.ClassTypingContext;

/**
Expand Down Expand Up @@ -99,10 +101,19 @@ MMField getOrCreateMMField(CtRole role) {
*/
public MMTypeKind getKind() {
if (kind == null) {
if (modelClass != null && modelClass.hasModifier(ModifierKind.ABSTRACT) == false) {
kind = MMTypeKind.LEAF;
if (modelClass == null && modelInterface == null) {
return null;
} else {
kind = MMTypeKind.ABSTRACT;
// we first consider interface
if (modelClass == null) {
this.kind = MMTypeKind.ABSTRACT;
} else {
if (modelClass.hasModifier(ModifierKind.ABSTRACT)) {
this.kind = MMTypeKind.ABSTRACT;
} else {
this.kind = MMTypeKind.LEAF;
}
}
}
}
return kind;
Expand Down