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

refactor: improve meta-metamodel classes about Spoon types and roles #1894

Merged
merged 3 commits into from
Mar 7, 2018
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
24 changes: 12 additions & 12 deletions src/test/java/spoon/generating/RoleHandlersGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
import spoon.reflect.reference.CtWildcardReference;
import spoon.reflect.visitor.PrinterHelper;
import spoon.template.Substitution;
import spoon.test.metamodel.MMField;
import spoon.test.metamodel.MetamodelProperty;
import spoon.test.metamodel.MMMethod;
import spoon.test.metamodel.MMMethodKind;
import spoon.test.metamodel.SpoonMetaModel;
Expand All @@ -49,17 +49,17 @@ public class RoleHandlersGenerator extends AbstractManualProcessor {

// Class[] helperIfaces = new Class[]{CtBodyHolder.class,
// CtTypeInformation.class, CtNamedElement.class};
Map<String, MMField> methodsByTypeRoleHandler = new HashMap<>();
Map<String, MetamodelProperty> methodsByTypeRoleHandler = new HashMap<>();

@Override
public void process() {
SpoonMetaModel metaModel = new SpoonMetaModel(getFactory());

//all root super MMFields
List<MMField> superFields = new ArrayList<>();
List<MetamodelProperty> superFields = new ArrayList<>();

metaModel.getMMTypes().forEach(mmType -> {
mmType.getRole2field().forEach((role, rim) -> {
metaModel.getConcepts().forEach(mmConcept -> {
mmConcept.getRoleToProperty().forEach((role, rim) -> {
addUniqueObject(superFields, rim.getRootSuperField());
});
});
Expand All @@ -69,11 +69,11 @@ public void process() {
if (d != 0) {
return d;
}
return a.getOwnerType().getName().compareTo(b.getOwnerType().getName());
return a.getOwnerConcept().getName().compareTo(b.getOwnerConcept().getName());
});
PrinterHelper concept = new PrinterHelper(getFactory().getEnvironment());
superFields.forEach(mmField -> {
concept.write(mmField.getOwnerType().getName() + " CtRole." + mmField.getRole().name()).writeln().incTab()
concept.write(mmField.getOwnerConcept().getName() + " CtRole." + mmField.getRole().name()).writeln().incTab()
.write("ItemType: ").write(mmField.getValueType().toString()).writeln();
for (MMMethodKind mk : MMMethodKind.values()) {
MMMethod mmMethod = mmField.getMethod(mk);
Expand Down 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().getModelInterface().getReference());
params.put("$TargetType$", rim.getOwnerConcept().getModelInterface().getReference());
// params.put("AbstractHandler", getFactory().Type().createReference("spoon.reflect.meta.impl.AbstractRoleHandler"));
params.put("AbstractHandler", getRoleHandlerSuperTypeQName(rim));
params.put("Node", rim.getOwnerType().getModelInterface().getReference());
params.put("Node", rim.getOwnerConcept().getModelInterface().getReference());
params.put("ValueType", fixMainValueType(getRoleHandlerSuperTypeQName(rim).endsWith("SingleHandler") ? rim.getValueType() : rim.getItemValueType()));
CtClass<?> modelRoleHandlerClass = Substitution.createTypeFromTemplate(
getHandlerName(rim),
Expand Down Expand Up @@ -184,12 +184,12 @@ private <T> boolean addUniqueObject(Collection<T> col, T o) {
return true;
}

String getHandlerName(MMField field) {
String typeName = field.getOwnerType().getName();
String getHandlerName(MetamodelProperty field) {
String typeName = field.getOwnerConcept().getName();
return typeName + "_" + field.getRole().name() + "_RoleHandler";
}

public String getRoleHandlerSuperTypeQName(MMField field) {
public String getRoleHandlerSuperTypeQName(MetamodelProperty field) {
switch (field.getValueContainerType()) {
case LIST:
return "spoon.reflect.meta.impl.ListHandler";
Expand Down
14 changes: 7 additions & 7 deletions src/test/java/spoon/reflect/visitor/CtScannerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
import spoon.reflect.visitor.processors.CheckScannerTestProcessor;
import spoon.test.metamodel.MMMethod;
import spoon.test.metamodel.MMMethodKind;
import spoon.test.metamodel.MMType;
import spoon.test.metamodel.MetamodelConcept;
import spoon.test.metamodel.MMTypeKind;
import spoon.test.metamodel.SpoonMetaModel;

Expand Down Expand Up @@ -140,20 +140,20 @@ public void testScannerCallsAllProperties() throws Exception {

class Counter { int nbChecks = 0; }
Counter c = new Counter();
for (MMType leafMmType : metaModel.getMMTypes()) {
for (MetamodelConcept leafConcept : metaModel.getConcepts()) {

// we only consider leaf, actual classes of the metamodel (eg CtInvocation) and not abstract ones (eg CtModifiable)
if (leafMmType.getKind() != MMTypeKind.LEAF) {
if (leafConcept.getKind() != MMTypeKind.LEAF) {
continue;
}

CtMethod<?> visitMethod = scannerVisitMethodsByName.remove("visit"+leafMmType.getName());
assertNotNull("CtScanner#" + "visit"+leafMmType.getName() + "(...) not found", visitMethod);
CtMethod<?> visitMethod = scannerVisitMethodsByName.remove("visit"+leafConcept.getName());
assertNotNull("CtScanner#" + "visit"+leafConcept.getName() + "(...) not found", visitMethod);
Set<String> calledMethods = new HashSet<>();
Set<String> checkedMethods = new HashSet<>();

// go over the roles and the corresponding fields of this type
leafMmType.getRole2field().forEach((role, mmField) -> {
leafConcept.getRoleToProperty().forEach((role, mmField) -> {

if (mmField.isDerived()) {
//ignore derived fields
Expand Down Expand Up @@ -181,7 +181,7 @@ public boolean matches(CtInvocation element) {
}
}).first();

if(getter.getName().equals("getComments") && leafMmType.getModelInterface().isSubtypeOf(ctRefRef)) {
if(getter.getName().equals("getComments") && leafConcept.getModelInterface().isSubtypeOf(ctRefRef)) {
//one cannot set comments on references see the @UnsettableProperty of CtReference#setComments
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,9 +284,9 @@ public void testInterfacesAreCtScannable() {

List<String> missingMethods = new ArrayList<>();

new SpoonMetaModel(interfaces.getFactory()).getMMTypes().forEach(mmType -> {
if (mmType.getKind() == ABSTRACT && mmType.getModelInterface() != null) {
CtInterface abstractIface = mmType.getModelInterface();
new SpoonMetaModel(interfaces.getFactory()).getConcepts().forEach(mmConcept -> {
if (mmConcept.getKind() == ABSTRACT && mmConcept.getModelInterface() != null) {
CtInterface abstractIface = mmConcept.getModelInterface();
String methodName = "scan" + abstractIface.getSimpleName();
if (ctScanner.getMethodsByName(methodName).isEmpty()) {
missingMethods.add(methodName);
Expand Down
60 changes: 0 additions & 60 deletions src/test/java/spoon/test/metamodel/MMContainerType.java

This file was deleted.

24 changes: 12 additions & 12 deletions src/test/java/spoon/test/metamodel/MMMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,26 @@
import spoon.support.visitor.MethodTypingContext;

/**
* Represents a method of a {@link MMField} of a {@link MMType}.
* Represents a method of a {@link MetamodelProperty} of a {@link MetamodelConcept}.
* Each MMMethod belongs to one MMField
*/
public class MMMethod {
private final MMField ownerField;
private final MetamodelProperty ownerField;
private final CtMethod<?> method;
private final List<CtMethod<?>> ownMethods = new ArrayList<>();
private final List<MMMethod> superMethods = new ArrayList<>();
private final String signature;
private final MMMethodKind methodKind;

/**
* Creates a {@link MMMethod} of a {@link MMField}
* Creates a {@link MMMethod} of a {@link MetamodelProperty}
* @param field a owner field
* @param method a method from ownerType or nearest super type
*/
MMMethod(MMField field, CtMethod<?> method) {
MMMethod(MetamodelProperty field, CtMethod<?> method) {
this.ownerField = field;
//adapt method to scope of field.ownType
MethodTypingContext mtc = new MethodTypingContext().setClassTypingContext(field.getOwnerType().getTypeContext()).setMethod(method);
MethodTypingContext mtc = new MethodTypingContext().setClassTypingContext(field.getOwnerConcept().getTypeContext()).setMethod(method);
this.method = (CtMethod<?>) mtc.getAdaptationScope();
signature = this.method.getSignature();
methodKind = MMMethodKind.valueOf(this.method);
Expand Down Expand Up @@ -84,7 +84,7 @@ public MMMethodKind getMethodKind() {
/**
* @return first own method in super type hierarchy of `targetType`
*/
public CtMethod<?> getFirstOwnMethod(MMType targetType) {
public CtMethod<?> getFirstOwnMethod(MetamodelConcept targetType) {
for (CtMethod<?> ctMethod : ownMethods) {
if (targetType.getTypeContext().isSubtypeOf(ctMethod.getDeclaringType().getReference())) {
return ctMethod;
Expand All @@ -104,7 +104,7 @@ public CtMethod<?> getFirstOwnMethod(MMType targetType) {
* @return true of this {@link MMMethod} overrides `method`. In different words, if it represents the same method
*/
public boolean overrides(CtMethod<?> method) {
return ownerField.getOwnerType().getTypeContext().isOverriding(this.method, method);
return ownerField.getOwnerConcept().getTypeContext().isOverriding(this.method, method);
}

/**
Expand All @@ -116,17 +116,17 @@ void addSuperMethod(MMMethod mmMethod) {
}

/**
* @return {@link MMField} which owns this {@link MMMethod}
* @return {@link MetamodelProperty} which owns this {@link MMMethod}
*/
public MMField getOwnerField() {
public MetamodelProperty getOwnerField() {
return ownerField;
}

/**
* @return {@link MMType} where this {@link MMMethod} belongs to
* @return {@link MetamodelConcept} where this {@link MMMethod} belongs to
*/
public MMType getOwnerType() {
return getOwnerField().getOwnerType();
public MetamodelConcept getOwnerType() {
return getOwnerField().getOwnerConcept();
}

/**
Expand Down
Loading