@Override public <T> void visitCtClass(CtClass<T> ctClass) { addClassImport(ctClass.getReference()); for (CtTypeMember t : ctClass.getTypeMembers()) { if (!(t instanceof CtType)) { continue; } addClassImport(((CtType) t).getReference()); } super.visitCtClass(ctClass); }
@Override public <T> void visitCtClass(CtClass<T> ctClass) { addReference(ctClass.getReference()); for (CtTypeMember typeMember : ctClass.getTypeMembers()) { if (typeMember instanceof CtType) { addReference(((CtType) typeMember).getReference()); } } super.visitCtClass(ctClass); } }
private static String createWrapperContent(final CtElement element, final Factory f, final CtTypeReference returnType) { CtClass<?> w = f.Class().create(WRAPPER_CLASS_NAME); CtBlock body = f.Core().createBlock(); if (element instanceof CtStatement) { body.addStatement((CtStatement) element); } else if (element instanceof CtExpression) { CtReturn ret = f.Core().createReturn(); ret.setReturnedExpression((CtExpression) element); body.addStatement(ret); } Set<ModifierKind> modifiers = EnumSet.of(ModifierKind.STATIC); Set<CtTypeReference<? extends Throwable>> thrownTypes = new HashSet<>(); thrownTypes.add(f.Class().<Throwable>get(Throwable.class).getReference()); f.Method().create( w, modifiers, returnType, WRAPPER_METHOD_NAME, CtElementImpl.<CtParameter<?>>emptyList(), thrownTypes, body); String contents = w.toString(); // Clean up (delete wrapper from factory) after it is printed. The DefaultJavaPrettyPrinter needs w in model to be able to print it correctly w.getPackage().removeType(w); return contents; }
/** * Gets the names of all the template parameters of a given template type * (including the ones defined by the super types). */ public static Collection<String> getNames(CtClass<? extends Template<?>> templateType) { Collection<String> params = new ArrayList<String>(); try { for (CtFieldReference<?> f : templateType.getReference().getAllFields()) { if (isParameterSource(f)) { params.add(getParameterName(f)); } } } catch (Exception e) { Launcher.LOGGER.error(e.getMessage(), e); } return params; }
private CtThisAccess<?> thisAccess() { return f().Code().createThisAccess(declaringType.getReference()); }
private static List<CtFieldReference<?>> getVarargs(CtClass<? extends Template<?>> root, List<CtInvocation<?>> variables) { List<CtFieldReference<?>> fields = new ArrayList<CtFieldReference<?>>(); for (CtFieldReference<?> field : root.getReference().getAllFields()) { if (field.getType().getActualClass() == CtStatementList.class) { boolean alreadyAdded = false; for (CtInvocation<?> invocation : variables) { alreadyAdded |= ((CtFieldAccess<?>) invocation.getTarget()).getVariable().getDeclaration().equals(field); } if (!alreadyAdded) { fields.add(field); } } } return fields; }
CtTypeReference refClass = ctofClass.getReference(); if (expression.getType().box().isSubtypeOf(refClass.box())) {
CtExpression<?> access(CompilationNode target, AccessType accessType) { List<CtField<?>> accessPath = accessPath(target); CompilationNode thisNodeToAccess = this; while (!target.rootAccessPath.subList(0, min(thisNodeToAccess.rootAccessPath.size(), target.rootAccessPath.size())) .equals(thisNodeToAccess.rootAccessPath)) { thisNodeToAccess = thisNodeToAccess.parent; } CtFieldAccess<?> access = FieldAccessChains.chainToAccess(accessPath, accessType); CtClass<?> classToMerge = thisNodeToAccess.classesToMerge.get(0); CtThisAccess<?> thisAccess = f.Code().createThisAccess(classToMerge.getReference()); // thisAccess.setImplicit(false); if (access != null) { CtFieldAccess<?> innerMostAccess = access; while (innerMostAccess.getTarget() != null) { innerMostAccess = (CtFieldAccess<?>) innerMostAccess.getTarget(); } innerMostAccess.setTarget(thisAccess); return access; } else { return thisAccess; } }
public void computeAccessPaths() { cxt.allClasses().forEach(ctC -> stagedClassExtensionChain(ctC).forEach(baseClass -> baseClass.getFields().forEach((CtField<?> field) -> { if (field.getAnnotation(StageRef.class) != null) { CtTypeReference<?> fieldClass = field.getType(); List<CtClass<?>> candidates = cxt.allClasses() .filter(c -> fieldClass.isAssignableFrom(c.getReference())) .collect(toList()); if (candidates.size() != 1) { throw sgce("Can assign " + candidates.stream().map(CtClass::getSimpleName).collect(toList()) + " to field " + field + " in " + baseClass.getSimpleName()); } cxt.bindReferenced(field, candidates.get(0)); CompilationNode referencingNode = cxt.getCompilationNode(ctC); CompilationNode referencedNode = cxt.getCompilationNode(candidates.get(0)); if (referencedNode.parent == referencingNode && referencedNode.parentAccessField == null) { referencedNode.parentAccessField = field; } } }))); root.computeRootAccessPath(); }
return Collections.emptyList(); } else { referenceToBeBuild = first.get().getReference();
return false; if ((template instanceof CtTypeReference) && template.equals(templateType.getReference())) { return true;
unaryOp.setType(ctofClass1.getReference()); else { System.out.println("We could not determine the ct class of " + aClassReturnOp1.getSimpleName()); op.setType(ctofClass.getReference()); else { System.out.println("We could not determine the ct class of " + aClassReturnOp.getSimpleName());
@Override public <T> void visitCtClass(CtClass<T> ctClass) { addImport(ctClass.getReference()); for (CtType<?> t : ctClass.getNestedTypes()) { addImport(t.getReference()); } super.visitCtClass(ctClass); }
@Override public <T> void visitCtClass(CtClass<T> ctClass) { addReference(ctClass.getReference()); for (CtType<?> t : ctClass.getNestedTypes()) { addReference(t.getReference()); } super.visitCtClass(ctClass); } }
@Override public <T> void visitCtFieldReference(CtFieldReference<T> ref) { CtTypeReference<?> declarationType = ref.getDeclaringType(); if (declarationType != null) { CtType<?> ctType = declarationType.getDeclaration(); if (ctType instanceof CtClass) { CompilationNode node = cxt.getNodeByAnyStagedClass((CtClass) ctType); if (node != null) { ref.setDeclaringType(node.getMergedClass().getReference()); } } } super.visitCtFieldReference(ref); } });
Collection<CtFieldReference<?>> fields = clazz.getReference().getAllFields();
void mergeChildNodes() { CtClass<?> mergedClass = getMergedClass(); for (CompilationNode child : innerNodes) { CtClass<?> childMergedClass = child.getMergedClass(); add(mergedClass, childMergedClass, mergedClass::addNestedType); CtCodeSnippetExpression constructor = f.Core().createCodeSnippetExpression(); constructor.setValue("new " + childMergedClass.getSimpleName() + "()"); child.parentAccessField.setDefaultExpression(constructor); child.parentAccessField.setType((CtTypeReference) childMergedClass.getReference()); child.parentAccessField.addModifier(FINAL); child.mergeChildNodes(); } } }
context.currentThis.push(((CtNewClass<?>) ctClass.getParent()).getType()); } else { context.currentThis.push(ctClass.getReference());