@Override public CtPackage getPackage() { if (parent instanceof CtPackage) { return (CtPackage) getParent(); } else if (parent instanceof CtType) { return ((CtType<?>) parent).getPackage(); } else { return null; } }
private Path getElementPath(CtType type) { return createFolders(getEnvironment().getOutputDestinationHandler() .getOutputPath(type.getPackage().getDeclaringModule(), type.getPackage(), type)); }
protected void searchForPackageProtectedField(CtField<?> field, CtConsumer<Object> outputConsumer) { //package protected fields are visible in scope of the package of the top level type of the `field` field.getTopLevelType().getPackage() //visit all children of package, where top level type of the field is declared .filterChildren(null) .forEach(outputConsumer); } }
/** * Write the compilation unit header. */ public void writeHeader(List<CtType<?>> types, Collection<CtImport> imports) { if (!types.isEmpty()) { for (CtType<?> ctType : types) { writeComment(ctType, CommentOffset.TOP_FILE); } // writing the header package if (!types.get(0).getPackage().isUnnamedPackage()) { writePackageLine(types.get(0).getPackage().getQualifiedName()); } this.writeImports(imports); printer.writeln(); printer.writeln(); } }
static String getLocalTypeRefBySimpleName(CtType<?> templateType, String typeSimpleName) { CtType<?> type = templateType.getNestedType(typeSimpleName); if (type != null) { return type.getQualifiedName(); } type = templateType.getPackage().getType(typeSimpleName); if (type != null) { return type.getQualifiedName(); } Set<String> typeQNames = new HashSet<>(); templateType .filterChildren((CtTypeReference<?> ref) -> typeSimpleName.equals(ref.getSimpleName())) .forEach((CtTypeReference<?> ref) -> typeQNames.add(ref.getQualifiedName())); if (typeQNames.size() > 1) { throw new SpoonException("The type parameter " + typeSimpleName + " is ambiguous. It matches multiple types: " + typeQNames); } if (typeQNames.size() == 1) { return typeQNames.iterator().next(); } return null; }
CtPackage pack = element.getPackage();
build(f, "package " + initialClass.getPackage().getQualifiedName() + ";" + initialClass.toString()); } finally {
public CompilationUnit getOrCreate(CtType type) { if (type == null) { return null; } if (type.getPosition().getCompilationUnit() != null) { return type.getPosition().getCompilationUnit(); } if (type.isTopLevel()) { CtModule module; if (type.getPackage() != null && factory.getEnvironment().getComplianceLevel() > 8) { module = type.getPackage().getParent(CtModule.class); } else { module = null; } File file = this.factory.getEnvironment().getOutputDestinationHandler().getOutputPath(module, type.getPackage(), type).toFile(); try { String path = file.getCanonicalPath(); CompilationUnit result = this.getOrCreate(path); result.addDeclaredType(type); type.setPosition(this.factory.createPartialSourcePosition(result)); return result; } catch (IOException e) { throw new SpoonException("Cannot get path for file: " + file.getAbsolutePath(), e); } } else { return getOrCreate(type.getTopLevelType()); } }
CtType<?> clone = type.clone(); StringBuilder tentativeTypeName = new StringBuilder(type.getSimpleName() + "Copy"); while (type.getFactory().Type().get(type.getPackage().getQualifiedName() + "." + tentativeTypeName) != null) { tentativeTypeName.append("X"); type.getPackage().addType(clone);
/** * @param includingFormalTypeParameter if true then references to formal type parameters * are added as actual type arguments of returned {@link CtTypeReference} */ public <T> CtTypeReference<T> createReference(CtType<T> type, boolean includingFormalTypeParameter) { CtTypeReference<T> ref = factory.Core().createTypeReference(); if (type.getDeclaringType() != null) { ref.setDeclaringType(createReference(type.getDeclaringType(), includingFormalTypeParameter)); } else if (type.getPackage() != null) { ref.setPackage(factory.Package().createReference(type.getPackage())); } ref.setSimpleName(type.getSimpleName()); if (includingFormalTypeParameter) { for (CtTypeParameter formalTypeParam : type.getFormalCtTypeParameters()) { ref.addActualTypeArgument(formalTypeParam.getReference()); } } return ref; }
@Override public CtPackage getPackage() { if (parent instanceof CtPackage) { return (CtPackage) parent; } else if (parent instanceof CtType) { return ((CtType<?>) parent).getPackage(); } else { return null; } }
private static CtStatement internalCompileStatement(CtElement st, CtTypeReference returnType) { Factory f = st.getFactory(); String contents = createWrapperContent(st, f, returnType); build(f, contents); CtType<?> c = f.Type().get(WRAPPER_CLASS_NAME); // Get the part we want CtMethod<?> wrapper = c.getMethod(WRAPPER_METHOD_NAME); List<CtStatement> statements = wrapper.getBody().getStatements(); CtStatement ret = statements.get(statements.size() - 1); // Clean up c.getPackage().removeType(c); //disconnect element from the parent, so it can be added to another model ret.delete(); ret.setParent(null); if (ret instanceof CtClass) { CtClass klass = (CtClass) ret; ret.getFactory().Package().getRootPackage().addType(klass); klass.setSimpleName(klass.getSimpleName().replaceAll("^[0-9]*", "")); } return ret; }
public String getPackageDeclaration() { StringBuffer bck = sbf; sbf = new StringBuffer(); for (CtAnnotation<?> a : context.currentTopLevel.getPackage().getAnnotations()) { a.accept(this); } if (!context.currentTopLevel.getPackage().getQualifiedName().equals(CtPackage.TOP_LEVEL_PACKAGE_NAME)) { write("package " + context.currentTopLevel.getPackage().getQualifiedName() + ";"); } String ret = sbf.toString(); sbf = bck; return ret; }
/** * Clones the test class and adds the test methods. * * @param original Test class * @param methods Test methods * @return Test class with new methods */ public static CtType cloneTestClassAndAddGivenTest(CtType original, List<CtMethod<?>> methods) { CtType clone = original.clone(); original.getPackage().addType(clone); methods.forEach(clone::addMethod); return clone; }
@Deprecated public static Set<CtType> computeClassProvider(CtType testClass) { List<CtType> types = Query.getElements(testClass.getParent(CtPackage.class), new TypeFilter(CtType.class)); types = types.stream() .filter(Objects::nonNull) .filter(type -> type.getPackage() != null) .filter(type -> type.getPackage().getQualifiedName().equals(testClass.getPackage().getQualifiedName())) .collect(Collectors.toList()); if (testClass.getParent(CtType.class) != null) { types.add(testClass.getParent(CtType.class)); } types.addAll(types.stream() .flatMap(type -> getImport(type).stream()) .collect(Collectors.toSet())); return new HashSet<>(types); }
protected synchronized String sourceForModelledClass(CtType<?> modelledClass) { prettyPrinter = new DefaultJavaPrettyPrinter(this.getFactory().getEnvironment()); prettyPrinter.scan(modelledClass); String sourceCode = "package " + modelledClass.getPackage().toString() + ";" + System.getProperty("line.separator") + prettyPrinter.toString(); prettyPrinter = new DefaultJavaPrettyPrinter(this.getFactory().getEnvironment()); return sourceCode; }
protected synchronized String sourceForModelledClass(CtType<?> modelledClass) { //logDebug(logger(), format("[Scanning source code of %s]", modelledClass.getQualifiedName())); prettyPrinter().scan(modelledClass); String packageDeclaration = ""; if (!modelledClass.getPackage().isUnnamedPackage()) { packageDeclaration = "package " + modelledClass.getPackage().getQualifiedName() + ";"; } String sourceCode = packageDeclaration + JavaLibrary.lineSeparator() + prettyPrinter().toString(); prettyPrinter = new DefaultJavaPrettyPrinter(spoonEnvironment()); return sourceCode; }
private static CtStatement internalCompileStatement(CtStatement st) { Factory f = st.getFactory(); CtClass<?> w = createWrapper(st, f); compile(f, w); CtType<?> c = f.Type().get("Wrapper"); // Get the part we want CtMethod<?> wrapper = Query.getElements(c, new Filter<CtMethod<?>>() { public boolean matches(CtMethod<?> element) { return element.getSimpleName().equals("wrap"); } }).get(0); CtStatement ret = wrapper.getBody().getStatements().get(0); // Clean up c.getPackage().getTypes().remove(c); // check typing? return ret; }
/** * Create a reference to a simple type */ public <T> CtTypeReference<T> createReference(CtType<T> type) { CtTypeReference<T> ref = factory.Core().createTypeReference(); if (type.getPackage() != null) { ref.setPackage(factory.Package().createReference(type.getPackage())); } if (type.getDeclaringType() != null) { ref.setDeclaringType(createReference(type.getDeclaringType())); } ref.setSimpleName(type.getSimpleName()); return ref; }
/** * Write the compilation unit header. */ public DefaultJavaPrettyPrinter writeHeader(List<CtType<?>> types, Collection<CtTypeReference<?>> imports) { if (!types.isEmpty()) { CtPackage pack = types.get(0).getPackage(); scan(pack).writeln().writeln().writeTabs(); if (env.isAutoImports()) { for (CtTypeReference<?> ref : imports) { write("import " + ref.getQualifiedName() + ";").writeln().writeTabs(); } } writeln().writeTabs(); } return this; }