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

feature: RoleHandlerHelper#getRoleHandlers(Class), forEachRoleHandler, getParentRoleHandler #1794

Merged
merged 4 commits into from
Jan 4, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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
54 changes: 54 additions & 0 deletions src/main/java/spoon/reflect/meta/impl/RoleHandlerHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@
package spoon.reflect.meta.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

import spoon.SpoonException;
import spoon.reflect.declaration.CtElement;
Expand All @@ -32,6 +36,8 @@ public class RoleHandlerHelper {
private RoleHandlerHelper() {
}

private static Map<Class<?>, List<RoleHandler>> roleHandlersByClass = new HashMap<>();

@SuppressWarnings("unchecked")
private static final List<RoleHandler>[] roleHandlers = new List[CtRole.values().length];
static {
Expand All @@ -56,6 +62,7 @@ public static RoleHandler getRoleHandler(Class<? extends CtElement> targetClass,
}
return rh;
}

/**
* @param targetClass the class of the to be manipulated node
* @param role defines the to be manipulated attribute
Expand All @@ -71,4 +78,51 @@ public static RoleHandler getOptionalRoleHandler(Class<? extends CtElement> targ
}
return null;
}

/**
* @param targetClass a Close whose handlers we are looking for
Copy link
Collaborator

@surli surli Jan 3, 2018

Choose a reason for hiding this comment

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

I don't understand your documentation here: what's a "Close"?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I should sleep more ... at least sometime ... "Close" is wrong. Correct is "Class" ;-) I will fix it.

* @return all RoleHandlers available for the `targetClass`
*/
public static List<RoleHandler> getRoleHandlers(Class<? extends CtElement> targetClass) {
List<RoleHandler> handlers = roleHandlersByClass.get(targetClass);
if (handlers == null) {
List<RoleHandler> modifiableHandlers = new ArrayList<>();
for (CtRole role : CtRole.values()) {
RoleHandler roleHandler = getOptionalRoleHandler(targetClass, role);
if (roleHandler != null) {
modifiableHandlers.add(roleHandler);
}
}
handlers = Collections.unmodifiableList(modifiableHandlers);
roleHandlersByClass.put(targetClass, handlers);
}
return handlers;
}

/**
* @param consumer is called for each RoleHandler of SpoonModel
*/
public static void forEachRoleHandler(Consumer<RoleHandler> consumer) {
for (List<RoleHandler> list : roleHandlers) {
for (RoleHandler roleHandler : list) {
consumer.accept(roleHandler);
}
}
}

/**
* @param element the {@link CtElement} whose {@link RoleHandler} in `element.getParent()` is needed.
* @return {@link RoleHandler} of {@link CtRole} of parent's attribute, which contains `element`
*/
public static RoleHandler getParentRoleHandler(CtElement element) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

rename to getRoleHandler?

add API documentation?

thanks!

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

rename to getRoleHandler?

are you sure? I think that there should be something about parent in the method name, because it returns role of this element in parent.

api doc.
I forgot that ... It compiles without documentation well :-) I will add it

Copy link
Collaborator

Choose a reason for hiding this comment

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

because it returns role of this element in parent.

a role is always with respect to the parent, right?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

in case of this method yes.

if (element.isParentInitialized() == false) {
return null;
}
CtElement parent = element.getParent();
CtRole roleInParent = element.getRoleInParent();
if (roleInParent == null) {
return null;
}
return RoleHandlerHelper.getRoleHandler(parent.getClass(), roleInParent);
}
}
46 changes: 46 additions & 0 deletions src/test/java/spoon/test/reflect/meta/MetaModelTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
import org.junit.Test;

import spoon.Launcher;
import spoon.Metamodel;
import spoon.reflect.declaration.CtAnnotation;
import spoon.reflect.declaration.CtClass;
import spoon.reflect.declaration.CtElement;
import spoon.reflect.declaration.CtField;
import spoon.reflect.declaration.CtType;
import spoon.reflect.factory.Factory;
import spoon.reflect.meta.ContainerKind;
import spoon.reflect.meta.RoleHandler;
Expand Down Expand Up @@ -69,6 +72,49 @@ public void spoonMetaModelTest() {
// assertTrue(String.join("\n", problems), problems.isEmpty());
}
@Test
public void testGetRoleHandlersOfClass() {
int countOfIfaces = 0;
for (CtType spoonIface : Metamodel.getAllMetamodelInterfaces()) {
countOfIfaces++;
checkRoleHandlersOfType(spoonIface);
}
assertTrue(countOfIfaces > 10);
}

private void checkRoleHandlersOfType(CtType iface) {
Class ifaceClass = iface.getActualClass();
//contract: check that for each Spoon model interface we have correct list of Role handlers
List<RoleHandler> roleHandlersOfIFace = new ArrayList<>(RoleHandlerHelper.getRoleHandlers(ifaceClass));
Set<RoleHandler> allRoleHandlers = new HashSet<>();
RoleHandlerHelper.forEachRoleHandler(rh -> allRoleHandlers.add(rh));
for (CtRole role : CtRole.values()) {
RoleHandler rh = RoleHandlerHelper.getOptionalRoleHandler(ifaceClass, role);
if (rh != null) {
assertTrue("RoleHandler for role " + role + " is missing for " + ifaceClass, roleHandlersOfIFace.remove(rh));
assertTrue("RoleHandler " + rh + " is not accessible by RoleHandlerHelper#forEachRoleHandler()", allRoleHandlers.contains(rh));
}
}
assertTrue("There are unexpected RoleHandlers " + roleHandlersOfIFace + " for " + ifaceClass, roleHandlersOfIFace.isEmpty());
}

@Test
public void testGetParentRoleHandler() {
Launcher launcher = new Launcher();
Factory factory = launcher.getFactory();
CtClass<?> type = (CtClass) factory.Core().create(CtClass.class);
CtField<?> field = factory.Field().create(type, Collections.emptySet(), factory.Type().booleanPrimitiveType(), "someField");
assertSame(type, field.getDeclaringType());
//contract: RoleHandlerHelper#getParentRoleHandler returns role handler which handles it's relationship to parent
assertSame(CtRole.TYPE_MEMBER, RoleHandlerHelper.getParentRoleHandler(field).getRole());
assertSame(CtRole.TYPE_MEMBER, field.getRoleInParent());
//contract: RoleHandlerHelper#getParentRoleHandler returns null if there is no parent
field.setParent(null);
assertNull(RoleHandlerHelper.getParentRoleHandler(field));
//contract: RoleHandlerHelper#getParentRoleHandler returns null if parent relation cannot be handled in this case
//parent of new CtClass is root package - there is no way how to modify that
assertNull(RoleHandlerHelper.getParentRoleHandler(type));
}
@Test
public void elementAnnotationRoleHandlerTest() {
Launcher launcher = new Launcher();
Factory factory = launcher.getFactory();
Expand Down