use of spoon.reflect.path.CtRole in project spoon by INRIA.
the class RoleHandlerHelper method getRoleHandlerWrtParent.
/**
* @param element the {@link CtElement} whose relation from `element.getParent()` to `element` is needed.
* @return {@link RoleHandler} handling relation from `element.getParent()` to `element`
*/
public static RoleHandler getRoleHandlerWrtParent(CtElement element) {
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);
}
use of spoon.reflect.path.CtRole in project spoon by INRIA.
the class RoleHandlerHelper method getRoleHandlers.
/**
* @param targetClass a Class whose handlers we are looking for
* @return all {@link RoleHandler}s 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;
}
use of spoon.reflect.path.CtRole in project spoon by INRIA.
the class MetaModelTest method checkRoleHandlersOfType.
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());
}
use of spoon.reflect.path.CtRole in project spoon by INRIA.
the class CtTypeImpl method addTypeMemberAt.
@Override
public <C extends CtType<T>> C addTypeMemberAt(int position, CtTypeMember member) {
if (member == null) {
return (C) this;
}
if (this.typeMembers == CtElementImpl.<CtTypeMember>emptyList()) {
this.typeMembers = new ArrayList<>();
}
if (!this.typeMembers.stream().anyMatch(m -> m == member)) {
member.setParent(this);
CtRole role;
if (member instanceof CtMethod) {
role = METHOD;
} else if (member instanceof CtConstructor) {
role = CONSTRUCTOR;
} else if (member instanceof CtField) {
role = FIELD;
} else if (member instanceof CtAnonymousExecutable) {
role = ANNONYMOUS_EXECUTABLE;
} else {
role = NESTED_TYPE;
}
getFactory().getEnvironment().getModelChangeListener().onListAdd(this, role, this.typeMembers, position, member);
if (position < typeMembers.size()) {
this.typeMembers.add(position, member);
} else {
this.typeMembers.add(member);
}
}
return (C) this;
}
use of spoon.reflect.path.CtRole in project spoon by INRIA.
the class CtModuleImpl method addModuleDirective.
@Override
public <T extends CtModule> T addModuleDirective(CtModuleDirective moduleDirective) {
if (moduleDirective == null) {
return (T) this;
}
if (this.moduleDirectives == CtElementImpl.<CtModuleDirective>emptyList()) {
this.moduleDirectives = new SortedList<>(new CtLineElementComparator());
}
if (!this.moduleDirectives.contains(moduleDirective)) {
moduleDirective.setParent(this);
CtRole role = this.computeRoleFromModuleDirectory(moduleDirective);
getFactory().getEnvironment().getModelChangeListener().onListAdd(this, role, this.moduleDirectives, moduleDirective);
this.moduleDirectives.add(moduleDirective);
}
return (T) this;
}
Aggregations