CompilationUnitWrapper(CtType type) { // char[] contents, String fileName, String encoding, String destinationPath, boolean ignoreOptionalProblems super(null, type.getSimpleName() + ".java", type.getFactory().getEnvironment().getEncoding().displayName(), type.getFactory().getEnvironment().getBinaryOutputDirectory(), false, null); this.type = type; }
/** * @param targetType - the element which is going to receive the model produced by the template. * It is needed here just to provide the spoon factory, which contains the model of the template * * @param template - java instance of the template * * @return - CtClass from the already built spoon model, which represents the template */ static <T> CtClass<T> getTemplateCtClass(CtType<?> targetType, Template<?> template) { 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(template); } return getTemplateCtClass(factory, template); }
@Override public char[] getContents() { DefaultJavaPrettyPrinter printer = new DefaultJavaPrettyPrinter(type.getFactory().getEnvironment()); List<CtType<?>> types = new ArrayList<>(); types.add(type); printer.calculate(type.getPosition().getCompilationUnit(), types); return printer.getResult().toCharArray(); } }
@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; }
/** * Changes name of a type element. * * @param type * Type in the AST. * @param name * New name of the element. */ public static void changeTypeName(final CtType<?> type, String name) { final String typeQFN = type.getQualifiedName(); final List<CtTypeReference<?>> references = Query.getElements(type.getFactory(), new TypeFilter<CtTypeReference<?>>(CtTypeReference.class) { @Override public boolean matches(CtTypeReference<?> reference) { String refFQN = reference.getQualifiedName(); return typeQFN.equals(refFQN); } }); type.setSimpleName(name); for (CtTypeReference<?> reference : references) { reference.setSimpleName(name); } }
elements2before.put(el.getPath(), el); Factory f = initialClass.getFactory();
if (!t.equals(targetType.getFactory().Type().createReference(Template.class))) { CtTypeReference<?> t1 = t; t1 = (CtTypeReference<?>) o; } else if (o instanceof Class) { t1 = targetType.getFactory().Type().createReference((Class<?>) o); } else if (o instanceof String) { t1 = targetType.getFactory().Type().createReference((String) o);
result = t.getFactory().Type().createReference(t, true); isCopy = true;
@Override public CtType<?> apply(CtType<?> target) { CtClass<? extends Template<?>> templateType = Substitution.getTemplateCtClass(target.getFactory(), this); CtType<?> generated = TemplateBuilder.createPattern(templateType, templateType, this) .setAddGeneratedBy(isAddGeneratedBy()) .substituteSingle(target, CtType.class); for (CtTypeReference<?> iface : new ArrayList<>(generated.getSuperInterfaces())) { iface.delete(); target.addSuperInterface(iface); } for (CtTypeMember tm : new ArrayList<>(generated.getTypeMembers())) { tm.delete(); target.addTypeMember(tm); } return target; } }
CtType<?> clone = type.clone(); StringBuilder tentativeTypeName = new StringBuilder(type.getSimpleName() + "Copy"); while (type.getFactory().Type().get(type.getPackage().getQualifiedName() + "." + tentativeTypeName) != null) { tentativeTypeName.append("X");
final SubInheritanceHierarchyResolver subHierarchyFnc = new SubInheritanceHierarchyResolver(declaringType.getFactory().getModel().getRootPackage());
public static String ctTypeToFullQualifiedName(CtType<?> testClass) { if (testClass.getModifiers().contains(ModifierKind.ABSTRACT)) { CtTypeReference<?> referenceOfSuperClass = testClass.getReference(); return testClass.getFactory().Class().getAll().stream() .filter(ctType -> referenceOfSuperClass.equals(ctType.getSuperclass())) .map(CtType::getQualifiedName).collect(Collectors.joining(",")); } else { return testClass.getQualifiedName(); } }
public CtBlock<?> apply(CtType<?> targetType) { CtClass<? extends BlockTemplate> c; c = targetType.getFactory().Class().get(this.getClass()); if (c == null) { c = targetType.getFactory().Class().get(this.getClass()); } return Substitution.substitute(targetType, this, getBlock(c)); }
public static void compileAndReplaceSnippetsIn(CtType<?> c) { Factory f = c.getFactory(); CtType<?> workCopy = c; Set<ModifierKind> backup = EnumSet.noneOf(ModifierKind.class); backup.addAll(workCopy.getModifiers()); workCopy.getModifiers().remove(ModifierKind.PUBLIC); try { build(f, workCopy.toString()); } finally { // restore modifiers c.setModifiers(backup); } }
@SuppressWarnings("unchecked") public CtExpression<T> apply(CtType<?> targetType) { CtClass<? extends ExpressionTemplate<?>> c; CtBlock<?> b; c = targetType.getFactory().Class().get(this.getClass()); if (c == null) { c = targetType.getFactory().Class().get(this.getClass()); } b = Substitution .substitute(targetType, this, getExpressionBlock(c)); return ((CtReturn<T>) b.getStatements().get(0)).getReturnedExpression(); }
@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; }
private static boolean isValidConstructor(CtType<?> type) { if(type instanceof CtClass<?>) { CtClass<?> ctClass = ((CtClass<?>)type); if (ctClass.getSuperclass() == null || !ctClass.getSuperclass().getSimpleName().equals("TestCase")){ return true; } return ((CtClass<?>)type).getConstructor() != null || ((CtClass<?>)type).getConstructor(type.getFactory().Class().createReference(String.class)) != null; } return false; }
public static void printCtTypeToGivenDirectory(CtType<?> type, File directory, boolean autoImports) { Factory factory = type.getFactory(); Environment env = factory.getEnvironment(); env.setAutoImports(autoImports); env.setNoClasspath(true); env.setCommentEnabled(InputConfiguration.get().withComment()); JavaOutputProcessor processor = new JavaOutputProcessor(new DefaultJavaPrettyPrinter(env)); processor.setFactory(factory); processor.getEnvironment().setSourceOutputDirectory(directory); processor.createJavaFile(type); env.setAutoImports(false); }