use of javax.tools.JavaFileObject in project storio by pushtorefresh.
the class StorIOSQLiteAnnotationsProcessorTest method shouldCompileWithMethodsReturningBoxedTypesAndMarkedAsIgnoreNullAndFactoryMethodAsCreator.
@Test
public void shouldCompileWithMethodsReturningBoxedTypesAndMarkedAsIgnoreNullAndFactoryMethodAsCreator() {
JavaFileObject model = JavaFileObjects.forResource("BoxedTypesMethodsFactoryMethodIgnoreNull.java");
JavaFileObject generatedTypeMapping = JavaFileObjects.forResource("BoxedTypesMethodsFactoryMethodIgnoreNullSQLiteTypeMapping.java");
JavaFileObject generatedDeleteResolver = JavaFileObjects.forResource("BoxedTypesMethodsFactoryMethodIgnoreNullStorIOSQLiteDeleteResolver.java");
JavaFileObject generatedGetResolver = JavaFileObjects.forResource("BoxedTypesMethodsFactoryMethodIgnoreNullStorIOSQLiteGetResolver.java");
JavaFileObject generatedPutResolver = JavaFileObjects.forResource("BoxedTypesMethodsFactoryMethodIgnoreNullStorIOSQLitePutResolver.java");
assert_().about(javaSource()).that(model).processedWith(new StorIOSQLiteProcessor()).compilesWithoutError().and().generatesSources(generatedTypeMapping, generatedDeleteResolver, generatedGetResolver, generatedPutResolver);
}
use of javax.tools.JavaFileObject in project storio by pushtorefresh.
the class StorIOSQLiteAnnotationsProcessorTest method shouldNotCompileIfCreatorInsideNotAnnotatedClass.
@Test
public void shouldNotCompileIfCreatorInsideNotAnnotatedClass() {
JavaFileObject model = JavaFileObjects.forResource("CreatorInsideNotAnnotatedClass.java");
assert_().about(javaSource()).that(model).processedWith(new StorIOSQLiteProcessor()).failsToCompile().withErrorContaining("Please annotate class CreatorInsideNotAnnotatedClass with StorIOSQLiteType");
}
use of javax.tools.JavaFileObject in project querydsl by querydsl.
the class AbstractQuerydslProcessor method serialize.
private void serialize(Serializer serializer, Collection<EntityType> models) {
for (EntityType model : models) {
try {
Type type = conf.getTypeMappings().getPathType(model, model, true);
String packageName = type.getPackageName();
String className = !packageName.isEmpty() ? (packageName + "." + type.getSimpleName()) : type.getSimpleName();
// skip if type is excluded class or in excluded package
if (conf.isExcludedPackage(model.getPackageName()) || conf.isExcludedClass(model.getFullName())) {
continue;
}
Set<TypeElement> elements = context.typeElements.get(model.getFullName());
if (elements == null) {
elements = new HashSet<TypeElement>();
}
for (Property property : model.getProperties()) {
if (property.getType().getCategory() == TypeCategory.CUSTOM) {
Set<TypeElement> customElements = context.typeElements.get(property.getType().getFullName());
if (customElements != null) {
elements.addAll(customElements);
}
}
}
processingEnv.getMessager().printMessage(Kind.NOTE, "Generating " + className + " for " + elements);
JavaFileObject fileObject = processingEnv.getFiler().createSourceFile(className, elements.toArray(new Element[elements.size()]));
Writer writer = fileObject.openWriter();
try {
SerializerConfig serializerConfig = conf.getSerializerConfig(model);
serializer.serialize(model, serializerConfig, new JavaWriter(writer));
} finally {
if (writer != null) {
writer.close();
}
}
} catch (IOException e) {
System.err.println(e.getMessage());
processingEnv.getMessager().printMessage(Kind.ERROR, e.getMessage());
}
}
}
use of javax.tools.JavaFileObject in project realm-java by realm.
the class RealmProxyInterfaceGenerator method generate.
public void generate() throws IOException {
String qualifiedGeneratedInterfaceName = String.format("%s.%s", Constants.REALM_PACKAGE_NAME, Utils.getProxyInterfaceName(className));
JavaFileObject sourceFile = processingEnvironment.getFiler().createSourceFile(qualifiedGeneratedInterfaceName);
JavaWriter writer = new JavaWriter(new BufferedWriter(sourceFile.openWriter()));
writer.setIndent(Constants.INDENT);
writer.emitPackage(Constants.REALM_PACKAGE_NAME).emitEmptyLine().beginType(qualifiedGeneratedInterfaceName, "interface", EnumSet.of(Modifier.PUBLIC));
for (VariableElement field : metaData.getFields()) {
// The field is neither static nor ignored
if (!field.getModifiers().contains(Modifier.STATIC) && field.getAnnotation(Ignore.class) == null) {
String fieldName = field.getSimpleName().toString();
String fieldTypeCanonicalName = field.asType().toString();
writer.beginMethod(fieldTypeCanonicalName, metaData.getInternalGetter(fieldName), EnumSet.of(Modifier.PUBLIC)).endMethod().beginMethod("void", metaData.getInternalSetter(fieldName), EnumSet.of(Modifier.PUBLIC), fieldTypeCanonicalName, "value").endMethod();
}
}
// backlinks are final and have only a getter.
for (Backlink backlink : metaData.getBacklinkFields()) {
writer.beginMethod(backlink.getTargetFieldType(), metaData.getInternalGetter(backlink.getTargetField()), EnumSet.of(Modifier.PUBLIC)).endMethod();
}
writer.endType();
writer.close();
}
use of javax.tools.JavaFileObject in project lombok by rzwitserloot.
the class TestClassFileMetaData method compile.
static byte[] compile(File file) {
try {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
File tempDir = getTempDir();
tempDir.mkdirs();
List<String> options = Arrays.asList("-proc:none", "-d", tempDir.getAbsolutePath());
StringWriter captureWarnings = new StringWriter();
final StringBuilder compilerErrors = new StringBuilder();
DiagnosticListener<JavaFileObject> diagnostics = new DiagnosticListener<JavaFileObject>() {
@Override
public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
compilerErrors.append(diagnostic.toString()).append("\n");
}
};
CompilationTask task = compiler.getTask(captureWarnings, null, diagnostics, options, null, Collections.singleton(new ContentBasedJavaFileObject(file.getPath(), readFileAsString(file))));
Boolean taskResult = task.call();
assertTrue("Compilation task didn't succeed: \n<Warnings and Errors>\n" + compilerErrors.toString() + "\n</Warnings and Errors>", taskResult);
return PostCompilerApp.readFile(new File(tempDir, file.getName().replaceAll("\\.java$", ".class")));
} catch (Exception e) {
throw Lombok.sneakyThrow(e);
}
}
Aggregations