/** * Returns the block. */ public static CtBlock<?> getBlock(CtClass<? extends BlockTemplate> p) { return p.getMethod("block").getBody(); }
private static CtBlock<?> getExpressionBlock( CtClass<? extends ExpressionTemplate<?>> p) { return p.getMethod("expression").getBody(); }
/** * Gets a body from a template executable with all the template parameters * substituted. * * @param targetClass * the target class * @param template * the template that holds the executable * @param executableName * the source executable template * @param parameterTypes * the parameter types of the source executable * @return the body expression of the source executable template with all * the template parameters substituted */ public static CtBlock<?> substituteMethodBody(CtClass<?> targetClass, Template<?> template, String executableName, CtTypeReference<?>... parameterTypes) { CtClass<?> sourceClass = getTemplateCtClass(targetClass, template); CtExecutable<?> sourceExecutable = executableName.equals(template.getClass().getSimpleName()) ? sourceClass.getConstructor(parameterTypes) : sourceClass.getMethod(executableName, parameterTypes); return substitute(targetClass, template, sourceExecutable.getBody()); }
@Override public CtStatement apply(CtType<?> targetType) { CtClass<?> c = Substitution.getTemplateCtClass(targetType, this); // we substitute the first statement of method statement CtStatement patternModel = c.getMethod("statement").getBody().getStatements().get(0); List<CtStatement> statements = TemplateBuilder.createPattern(patternModel, this) .setAddGeneratedBy(isAddGeneratedBy()) .substituteList(c.getFactory(), targetType, CtStatement.class); if (statements.size() != 1) { throw new IllegalStateException(); } return statements.get(0); }
/** * Gets a statement from a template executable with all the template * parameters substituted. * * @param targetClass * the target class * @param template * the template that holds the executable * @param statementIndex * the statement index in the executable's body * @param executableName * the source executable template * @param parameterTypes * the parameter types of the source executable * @return the body expression of the source executable template with all * the template parameters substituted */ public static CtStatement substituteStatement(CtClass<?> targetClass, Template<?> template, int statementIndex, String executableName, CtTypeReference<?>... parameterTypes) { CtClass<?> sourceClass = getTemplateCtClass(targetClass, template); CtExecutable<?> sourceExecutable = executableName.equals(template.getClass().getSimpleName()) ? sourceClass.getConstructor(parameterTypes) : sourceClass.getMethod(executableName, parameterTypes); return substitute(targetClass, template, sourceExecutable.getBody().getStatement(statementIndex)); }
/** * Gets all the main methods stored in this factory. */ public Collection<CtMethod<Void>> getMainMethods() { Collection<CtMethod<Void>> methods = new ArrayList<>(); for (CtType<?> t : factory.Type().getAll()) { if (t instanceof CtClass) { CtMethod<Void> m = ((CtClass<?>) t).getMethod(factory.Type().createReference(void.class), "main", factory.Type().createArrayReference(factory.Type().createReference(String.class))); if ((m != null) && m.getModifiers().contains(ModifierKind.STATIC)) { methods.add(m); } } } return methods; }
@SuppressWarnings("unchecked") public CtBlock<R> getSubstitution(CtType<?> targetType) { CtClass<?> c; c = targetType.getFactory().Class().get(this.getClass()); if (c == null) { c = targetType.getFactory().Class().get(this.getClass()); } CtMethod m = c.getMethod("block"); if (this instanceof Template) { return Substitution.substitute(targetType, (Template<?>) this, m.getBody()); } return m.getBody().clone(); }
public CtStatementList getSubstitution(CtType<?> targetType) { CtClass<?> c; CtBlock<?> b; c = targetType.getFactory().Class().get(this.getClass()); if (c == null) { c = targetType.getFactory().Class().get(this.getClass()); } CtStatementList l = targetType.getFactory().Core().createStatementList(); if (this instanceof Template) { b = Substitution.substitute(targetType, (Template<?>) this, c.getMethod("statements").getBody()); } else { b = c.getMethod("statements").getBody().clone(); } l.setStatements(b.getStatements()); return l; }
/** * Returns the block. */ public static CtBlock<?> getBlock(CtClass<? extends BlockTemplate> p) { CtBlock<?> b = p.getMethod("block").getBody(); return b; }
private static CtBlock<?> getExpressionBlock( CtClass<? extends ExpressionTemplate<?>> p) { CtBlock<?> b = p.getMethod("expression").getBody(); return b; }
/** * Gets a body from a template executable with all the template parameters * substituted. * * @param targetClass * the target class * @param template * the template that holds the executable * @param executableName * the source executable template * @param parameterTypes * the parameter types of the source executable * @return the body expression of the source executable template with all * the template parameters substituted */ public static CtBlock<?> substituteMethodBody(CtClass<?> targetClass, Template<?> template, String executableName, CtTypeReference<?>... parameterTypes) { CtClass<?> sourceClass = targetClass.getFactory().Class().get(template.getClass()); CtExecutable<?> sourceExecutable = executableName.equals(template.getClass().getSimpleName()) ? sourceClass.getConstructor(parameterTypes) : sourceClass.getMethod(executableName, parameterTypes); return substitute(targetClass, template, sourceExecutable.getBody()); }
/** * Gets a statement from a template executable with all the template * parameters substituted. * * @param targetClass * the target class * @param template * the template that holds the executable * @param statementIndex * the statement index in the executable's body * @param executableName * the source executable template * @param parameterTypes * the parameter types of the source executable * @return the body expression of the source executable template with all * the template parameters substituted */ public static CtStatement substituteStatement(CtClass<?> targetClass, Template<?> template, int statementIndex, String executableName, CtTypeReference<?>... parameterTypes) { CtClass<?> sourceClass = targetClass.getFactory().Class().get(template.getClass()); CtExecutable<?> sourceExecutable = executableName.equals(template.getClass().getSimpleName()) ? sourceClass.getConstructor(parameterTypes) : sourceClass.getMethod(executableName, parameterTypes); return substitute(targetClass, template, sourceExecutable.getBody().getStatement(statementIndex)); }
/** * Gets all the main methods stored in this factory. */ public Collection<CtMethod<Void>> getMainMethods() { Collection<CtMethod<Void>> methods = new ArrayList<CtMethod<Void>>(); for (CtType<?> t : factory.Type().getAll()) { if (t instanceof CtClass) { CtMethod<Void> m = ((CtClass<?>) t).getMethod(factory.Type().createReference(void.class), "main", factory.Type().createArrayReference(factory.Type().createReference(String.class))); if ((m != null) && m.getModifiers().contains(ModifierKind.STATIC)) { methods.add(m); } } } return methods; }
@SuppressWarnings("unchecked") public CtBlock<R> getSubstitution(CtType<?> targetType) { CtClass<?> c; c = targetType.getFactory().Class().get(this.getClass()); if (c == null) { c = targetType.getFactory().Class().get(this.getClass()); } CtMethod m = c.getMethod("block"); if (this instanceof Template) { return Substitution.substitute(targetType, (Template<?>) this, m.getBody()); } return targetType.getFactory().Core().clone(m.getBody()); }
public CtStatementList getSubstitution(CtType<?> targetType) { CtClass<?> c; CtBlock<?> b; c = targetType.getFactory().Class().get(this.getClass()); if (c == null) { c = targetType.getFactory().Class().get(this.getClass()); } CtStatementList l = targetType.getFactory().Core().createStatementList(); if (this instanceof Template) { b = Substitution.substitute(targetType, (Template<?>) this, c.getMethod("statements").getBody()); } else { b = targetType.getFactory().Core().clone(c.getMethod("statements").getBody()); } l.setStatements(b.getStatements()); return l; }
public CtStatement apply(CtType<?> targetType) { CtClass<?> c; Factory factory; // we first need a factory if (targetType != null) { // if it's template with reference replacement factory = targetType.getFactory(); } else { // else we have at least one template parameter with a factory factory = getFactory(); } c = factory.Class().get(this.getClass()); if (c == null) { c = factory.Class().get(this.getClass()); } // we substitute the first statement of method statement CtStatement result = factory.Core().clone(c.getMethod("statement").getBody().getStatements().get(0)); new SubstitutionVisitor(factory, targetType, this) .scan(result); return result; }