/** * Constructs a matcher for a given template. * * @param templateRoot the template to match against * */ public TemplateMatcher(CtElement templateRoot) { this(templateRoot, templateRoot.getParent(CtClass.class)); }
/** * Build absolute path to a CtElement el. * * @throws CtPathException is thrown when root is not a parent of el. * * @param el : the element to which the CtPath leads to * @return CtPath from model root package to el */ public CtPath fromElement(CtElement el) throws CtPathException { return fromElement(el, el.getParent(CtModelImpl.CtRootPackage.class)); } /**
private RoleHandler getRoleHandler(CtRole roleInParent, SourcePositionHolder otherElement) { SourcePositionHolder parent = element; if (parent == null) { if (otherElement instanceof CtElement) { parent = ((CtElement) otherElement).getParent(); } } if (parent instanceof CtElement) { CtElement ele = (CtElement) parent; return RoleHandlerHelper.getRoleHandler(ele.getClass(), roleInParent); } return null; }
@Override protected void enter(CtElement e) { if (e != null) { if (!previous.isEmpty()) { try { if (e.getParent() != previous.getLast()) { inconsistentParents.add(e); } } catch (ParentNotInitializedException ignore) { inconsistentParents.add(e); } } previous.add(e); } super.enter(e); }
private ParameterElementPair transformCtReturnIfNeeded(ParameterElementPair pep) { CtElement element = pep.element; if (element.isParentInitialized() && element.getParent() instanceof CtReturn<?>) { //we are substituting return expression. If the parameter value is CtBlock, then we have to substitute CtReturn instead Class<?> valueType = pep.parameter.getParameterValueType(); if (valueType != null && CtBlock.class.isAssignableFrom(valueType)) { //substitute CtReturn return pep.copyAndSet(element.getParent()); } } return pep; }
@SuppressWarnings("unchecked") private <T extends CtElement> T getFirst(CtElement ele, Class<T> clazz) { if (ele != null) { if (clazz.isAssignableFrom(ele.getClass())) { return (T) ele; } if (ele.isParentInitialized()) { return getFirst(ele.getParent(), clazz); } } return null; }
public static <E extends spoon.reflect.declaration.CtElement> void replace(spoon.reflect.declaration.CtElement original, java.util.Collection<E> replaces) { try { new spoon.support.visitor.replace.ReplacementVisitor(original, replaces.toArray(new spoon.reflect.declaration.CtElement[replaces.size()])).scan(original.getParent()); } catch (spoon.support.visitor.replace.InvalidReplaceException e) { throw e; } }
private boolean appendPathIn(StringBuilder sb, CtElement element, CtElement parent) { if (element != parent && element != null) { CtRole roleInParent = element.getRoleInParent(); if (roleInParent == null) { return false; } if (appendPathIn(sb, element.getParent(), parent)) { sb.append("/").append(getElementTypeName(element.getParent())); } sb.append(".").append(roleInParent.getCamelCaseName()); return true; } return false; }
public List<CtVariable> find() { if (expression.isParentInitialized()) { return getVariable(expression.getParent()); } return Collections.emptyList(); }
@Override @SuppressWarnings("unchecked") public <P extends CtElement> P getParent(Class<P> parentType) throws ParentNotInitializedException { if (parent == null) { return null; } if (parentType.isAssignableFrom(getParent().getClass())) { return (P) getParent(); } return getParent().getParent(parentType); }
private static void addParentPath(StringBuilder sb, CtElement ele) { if (ele == null || (ele instanceof CtPackage && ((CtPackage) ele).isUnnamedPackage())) { return; } if (ele.isParentInitialized()) { addParentPath(sb, ele.getParent()); } sb.append("\n\t").append(ele.getClass().getSimpleName()); if (ele instanceof CtNamedElement) { sb.append(":").append(((CtNamedElement) ele).getSimpleName()); } else if (ele instanceof CtReference) { sb.append(":").append(((CtReference) ele).getSimpleName()); } }
public static void replace(spoon.reflect.declaration.CtElement original, spoon.reflect.declaration.CtElement replace) { try { new spoon.support.visitor.replace.ReplacementVisitor(original, (replace == null ? EMPTY : new spoon.reflect.declaration.CtElement[]{ replace })).scan(original.getParent()); } catch (spoon.support.visitor.replace.InvalidReplaceException e) { throw e; } }
private void replaceElementIfExist(spoon.reflect.declaration.CtElement candidate, spoon.support.visitor.replace.ReplaceListener listener) { if (candidate == (original)) { spoon.reflect.declaration.CtElement val = null; if ((replace.length) > 0) { if ((replace.length) > 1) { throw new spoon.support.visitor.replace.InvalidReplaceException(("Cannot replace single value by multiple values in " + (listener.getClass().getSimpleName()))); } val = replace[0]; } if (val != null) { val.setParent(candidate.getParent()); } listener.set(val); } }
@Override public void scan(CtElement element) { if (element != null && element.isParentInitialized() && element.getParent() == parent) { //visit only elements whose parent is same boolean canVisit = hasVisitedInput ? visitNext : visitPrev; if (input == element) { hasVisitedInput = true; canVisit = includingSelf; } if (canVisit) { outputConsumer.accept(element); } } } });
/** * @return last implicit parent of element */ private ParameterElementPair getLastImplicitParent(ParameterElementPair pep) { CtElement element = pep.element; while (element.isParentInitialized()) { CtElement parent = element.getParent(); if ((parent instanceof CtBlock) == false || parent.isImplicit() == false) { break; } element = parent; } return pep.copyAndSet(element); } }
private CtTypeReference<?> getDeclaringTypeRef(List<? extends CtElement> template) { CtType<?> type = null; for (CtElement ctElement : template) { CtType t; if (ctElement instanceof CtType) { t = (CtType) ctElement; type = mergeType(type, t); } t = ctElement.getParent(CtType.class); if (t != null) { type = mergeType(type, t); } } return type == null ? null : type.getReference(); }
void addSubstitutionRequest(ParameterInfo parameter, CtElement element) { //remember elements substituted by current parameter to be able to use them for marking inline statements substitutedNodes.add(element); ParameterElementPair pep = getSubstitutedNodeOfElement(parameter, element); patternBuilder.setNodeOfElement(pep.element, new ParameterNode(pep.parameter), conflictResolutionMode); if (patternBuilder.isAutoSimplifySubstitutions() && pep.element.isParentInitialized()) { RootNode node = patternBuilder.getOptionalPatternNode(pep.element.getParent()); if (node != null) { node.setSimplifyGenerated(true); } } }
static void linkToParent(CtElement owner, CtElement element) { if (owner.getFactory().getEnvironment().checksAreSkipped() == false && element.isParentInitialized() && element.getParent() != owner) { //the `e` already has an different parent. Check if it is still linked to that parent if (element.getRoleInParent() != null) { throw new SpoonException("The default behavior has changed, a new check has been added! Don't worry, you can disable this check\n" + "with one of the following options:\n" + " - by configuring Spoon with getEnvironment().setSelfChecks(true)\n" + " - by removing the node from its previous parent (element.delete())\n" + " - by cloning the node before adding it here (element.clone())\n" ); } } element.setParent(owner); }
@Override public void apply(CtElement input, CtConsumer<Object> outputConsumer) { if (input == null) { return; } if (includingSelf) { outputConsumer.accept(input); } CtElement parent = input; CtModule topLevel = input.getFactory().getModel().getUnnamedModule(); while (parent != null && parent != topLevel && query.isTerminated() == false && parent.isParentInitialized()) { parent = parent.getParent(); outputConsumer.accept(parent); } }
@Override public void computeImports(CtElement element) { //look for top declaring type of that simpleType if (element instanceof CtType) { CtType simpleType = (CtType) element; targetType = simpleType.getReference().getTopLevelType(); addClassImport(simpleType.getReference()); scan(simpleType); } else { CtType<?> type = element.getParent(CtType.class); targetType = type == null ? null : type.getReference().getTopLevelType(); scan(element); } }