public Writer openSource(JPackage pkg, String fileName) throws IOException { String name; if (pkg.isUnnamed()) name = fileName; else name = pkg.name() + '.' + fileName; name = name.substring(0, name.length() - 5); // strip ".java" return filer.createSourceFile(name); }
@SuppressWarnings("unused") public void process() { Filer f = _env.getFiler(); AnnotationTypeDeclaration annoDecl = (AnnotationTypeDeclaration) _env.getTypeDeclaration(NestedHelloWorldAnnotation.class.getName()); Collection<Declaration> annotatedDecls = _env.getDeclarationsAnnotatedWith(annoDecl); try { for (Declaration annotatedDecl : annotatedDecls) { String typeName = TYPENAME; PrintWriter writer = f.createSourceFile(PACKAGENAME + "." + typeName); writer.print(getCode()); writer.close(); } reportSuccess(this.getClass()); } catch (NullPointerException npe) { reportError(this.getClass(), "Could not read annotation in order to generate text file"); } catch (IOException ioe) { reportError(this.getClass(), "Could not generate text file due to IOException"); } }
public OutputStream openBinary(JPackage pkg, String fileName) throws IOException { Filer.Location loc; if (fileName.endsWith(".java")) { // APT doesn't do the proper Unicode escaping on Java source files, // so we can't rely on Filer.createSourceFile. loc = SOURCE_TREE; } else { // put non-Java files directly to the output folder loc = CLASS_TREE; } return filer.createBinaryFile(loc, pkg.name(), new File(fileName)); }
@Override public void process() { final AnnotationTypeDeclaration beanAnn = (AnnotationTypeDeclaration) env_.getTypeDeclaration(Bean.class.getName()); for (Declaration declaration : env_.getDeclarationsAnnotatedWith(beanAnn)) { try { if (!(declaration instanceof ClassDeclaration)) { error(declaration, "You can only annotate class declarations with @Bean"); return; } Thing data = new Thing("data"); ClassDeclaration classDeclaration = (ClassDeclaration) declaration; PackageDeclaration packageDeclaration = classDeclaration.getPackage(); Map<String, AnnotationValue> beanValues = getAnnotationValues(classDeclaration, "com.javadude.annotation.Bean"); checkExtends(classDeclaration, packageDeclaration); // check that the class extends XXXGen processSuperclass(data, beanValues); // process the superclass attribute processProperties(data, beanValues); processObservers(data, beanValues); processNullObjects(data, beanValues); processDelegates(data, beanValues); processDefaultMethods(data, classDeclaration); setValue(data, beanValues, "cloneable", false); setValue(data, beanValues, "defineEqualsAndHashCode", false); setValue(data, beanValues, "equalsAndHashCodeCallSuper", false); setValue(data, beanValues, "definePropertyNameConstants", false); setValue(data, beanValues, "defineCreatePropertyMap", false); data.put("date", new Date()); data.put("year", Calendar.getInstance().get(Calendar.YEAR)); data.put("className", classDeclaration.getSimpleName()); data.put("packageName", packageDeclaration.getQualifiedName()); data.checkAllValuesSet(declaration, this); Filer f = env_.getFiler(); PrintWriter pw = f.createSourceFile(classDeclaration.getQualifiedName() + "Gen"); if (nestedEclipse) { // debugging in eclipse -- reread the template each time FileReader fileReader = new FileReader( workspace + "/com.javadude.annotation/template/$packageName$/$className$Gen.java"); template = new TemplateReader().readTemplate(fileReader); } int spacesForLeadingTabs = i(beanValues, "spacesForLeadingTabs", -1); String padding = null; if (spacesForLeadingTabs != -1) { padding = ""; for (int i = 0; i < spacesForLeadingTabs; i++) { padding += ' '; } } template.process(new Symbols(data), pw, -1, padding); pw.close(); } catch (ThreadDeath e) { throw e; } catch (ExpressionException e) { error(declaration, "@Bean generator error: " + e.getMessage()); } catch (Throwable t) { StringWriter stringWriter = new StringWriter(); PrintWriter printWriter = new PrintWriter(stringWriter); t.printStackTrace(printWriter); printWriter.close(); error(declaration, "Unexpected exception: " + stringWriter.toString()); } } }