/** * @param targetType the type which will receive the model generated using returned parameters * @return Map of template parameters from `template` */ public Map<String, Object> getTemplateParameters(CtType<?> targetType) { Factory f = templateType.getFactory(); return Parameters.getTemplateParametersAsMap(f, targetType, template); }
@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); }
/** * Generates a constructor from a template method by substituting all the * template parameters by their values. * * @param targetClass * the target class where to insert the generated constructor * @param template * the template instance that holds the source template method * and that defines the parameter values * @param sourceMethod * the source template method * @return the generated method */ public static <T> CtConstructor<T> insertConstructor(CtClass<T> targetClass, Template<?> template, CtMethod<?> sourceMethod) { if (targetClass instanceof CtInterface) { return null; } CtConstructor<T> newConstructor = targetClass.getFactory().Constructor().create(targetClass, sourceMethod); newConstructor = substitute(targetClass, template, newConstructor); targetClass.addConstructor(newConstructor); return newConstructor; }
protected Factory f() { return declaringType.getFactory(); }
Set<CtMethod<?>> methodsOfTemplate = sourceClass.getFactory().Type().get(Template.class).getMethods();
public <T> void visitCtClass(final spoon.reflect.declaration.CtClass<T> ctClass) { spoon.reflect.declaration.CtClass<T> aCtClass = ctClass.getFactory().Core().createClass(); this.builder.copy(ctClass, aCtClass); aCtClass.setAnnotations(this.cloneHelper.clone(ctClass.getAnnotations())); aCtClass.setSuperclass(this.cloneHelper.clone(ctClass.getSuperclass())); aCtClass.setSuperInterfaces(this.cloneHelper.clone(ctClass.getSuperInterfaces())); aCtClass.setFormalCtTypeParameters(this.cloneHelper.clone(ctClass.getFormalCtTypeParameters())); aCtClass.setTypeMembers(this.cloneHelper.clone(ctClass.getTypeMembers())); aCtClass.setComments(this.cloneHelper.clone(ctClass.getComments())); this.cloneHelper.tailor(ctClass, aCtClass); this.other = aCtClass; }
private static List<CtInvocation<?>> getMethods(CtClass<? extends Template<?>> root) { CtExecutableReference<?> methodRef = root.getFactory().Executable() .createReference(root.getFactory().Type().createReference(TemplateParameter.class), root.getFactory().Type().createTypeParameterReference("T"), "S"); List<CtInvocation<?>> meths = Query.getElements(root, new InvocationFilter(methodRef)); return meths; }
/** * 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)); }
if (te + 1 >= teList.size()) { multi.addAll(taList.subList(te, taList.size())); CtStatementList tpl = templateType.getFactory().Core().createStatementList(); tpl.setStatements((List<CtStatement>) (List<?>) multi); if (!invokeCallBack(tpl, inMulti)) { ta++; CtStatementList tpl = templateType.getFactory().Core().createStatementList(); tpl.setStatements((List<CtStatement>) (List<?>) multi); if (!invokeCallBack(tpl, inMulti)) { CtStatementList tpl = templateType.getFactory().Core().createStatementList(); for (Object o : multi) { tpl.addStatement((CtStatement) o);
/** * Generates a constructor from a template method by substituting all the * template parameters by their values. * * @param targetClass * the target class where to insert the generated constructor * @param template * the template instance that holds the source template method * and that defines the parameter values * @param sourceMethod * the source template method * @return the generated method */ public static <T> CtConstructor<T> insertConstructor(CtClass<T> targetClass, Template<?> template, CtMethod<?> sourceMethod) { if (targetClass instanceof CtInterface) { return null; } CtConstructor<T> newConstructor = targetClass.getFactory().Constructor().create(targetClass, sourceMethod); newConstructor = substitute(targetClass, template, newConstructor); targetClass.addConstructor(newConstructor); // newConstructor.setParent(targetClass); return newConstructor; }
CtClass<?> declaringClass = (CtClass) declaringType; if (declaringClass.getConstructors().isEmpty()) { Factory factory = declaringClass.getFactory(); CtConstructor ctr = factory.Constructor().create(declaringClass, EnumSet.of(PUBLIC), emptyList(), emptySet());
List<?> l = (List<?>) value; for (Object f : l) { CtField<?> f2 = ctClass.getFactory().Core().clone((CtField<?>) f); f2.setParent(ctClass); ctClass.getFields().add(i++, f2);
@SuppressWarnings("unchecked") private static CtElement addSaveStatementInTearDownAfterClass(CtClass<?> testClass) { final Factory factory = testClass.getFactory(); CtMethod<?> testDownAfterClass = testClass.filterChildren(new TypeFilter<CtMethod>(CtMethod.class) { @Override public boolean matches(CtMethod element) { return element.getAnnotations().contains(factory.Annotation().get(AfterClass.class)); } }).first(); boolean methodCreated = false; if (testDownAfterClass == null) { methodCreated = true; testDownAfterClass = initializeTestDownAfterClassMethod(factory, testClass); } final CtType<?> loggerType = factory.Type().get(Logger.class); final CtMethod<?> save = loggerType.getMethodsByName("save").get(0); CtInvocation invocation = factory.createInvocation(factory.Code().createTypeAccess(loggerType.getReference()), save.getReference()); testDownAfterClass.getBody().insertEnd( invocation ); if (methodCreated) { return testDownAfterClass; } else { return invocation; } }
File outFile = ADLFileFactory.createADLFile(processedClass.getFactory() .getEnvironment().getDefaultFileGenerator() .getOutputDirectory(), processedClass);