-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #390 from eclipse-vertx/remove-codegen-output
Cleanup
- Loading branch information
Showing
26 changed files
with
1,585 additions
and
1,051 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
import javax.annotation.processing.FilerException; | ||
import javax.annotation.processing.ProcessingEnvironment; | ||
import javax.annotation.processing.RoundEnvironment; | ||
import javax.lang.model.SourceVersion; | ||
import javax.lang.model.element.Element; | ||
import javax.lang.model.element.ElementKind; | ||
import javax.lang.model.element.TypeElement; | ||
|
@@ -16,7 +17,6 @@ | |
import javax.tools.JavaFileObject; | ||
import javax.tools.StandardLocation; | ||
import java.io.File; | ||
import java.io.FileWriter; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.Writer; | ||
|
@@ -30,23 +30,19 @@ | |
import java.util.regex.Pattern; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
import java.util.stream.StreamSupport; | ||
|
||
/** | ||
* @author <a href="mailto:[email protected]">Julien Viet</a> | ||
*/ | ||
@javax.annotation.processing.SupportedOptions({"codegen.output","codegen.generators"}) | ||
@javax.annotation.processing.SupportedSourceVersion(javax.lang.model.SourceVersion.RELEASE_8) | ||
@javax.annotation.processing.SupportedOptions({"codegen.generators"}) | ||
@javax.annotation.processing.SupportedSourceVersion(SourceVersion.RELEASE_11) | ||
public class Processor extends AbstractProcessor { | ||
|
||
private static final int JAVA= 0, RESOURCE = 1, OTHER = 2; | ||
private static final String JSON_MAPPERS_PROPERTIES_PATH = "META-INF/vertx/json-mappers.properties"; | ||
public static final Logger log = Logger.getLogger(Processor.class.getName()); | ||
private File outputDirectory; | ||
private List<? extends Generator<?>> codeGenerators; | ||
private Map<String, GeneratedFile> generatedFiles = new HashMap<>(); | ||
private Map<String, GeneratedFile> generatedResources = new HashMap<>(); | ||
private Map<String, String> relocations = new HashMap<>(); | ||
private Set<Class<? extends Annotation>> supportedAnnotation = new HashSet<>(); | ||
private List<CodeGen.Converter> mappers; | ||
|
||
|
@@ -58,7 +54,6 @@ public Set<String> getSupportedAnnotationTypes() { | |
@Override | ||
public synchronized void init(ProcessingEnvironment processingEnv) { | ||
super.init(processingEnv); | ||
generatedFiles.clear(); | ||
generatedResources.clear(); | ||
supportedAnnotation = new HashSet<>(Arrays.asList(DataObject.class, VertxGen.class)); | ||
getCodeGenerators() | ||
|
@@ -88,18 +83,6 @@ private Predicate<Generator> filterGenerators() { | |
|
||
private Collection<? extends Generator<?>> getCodeGenerators() { | ||
if (codeGenerators == null) { | ||
String outputDirectoryOption = processingEnv.getOptions().get("codegen.output"); | ||
if (outputDirectoryOption != null) { | ||
outputDirectory = new File(outputDirectoryOption); | ||
if (!outputDirectory.exists()) { | ||
if (!outputDirectory.mkdirs()) { | ||
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Output directory " + outputDirectoryOption + " does not exist"); | ||
} | ||
} | ||
if (!outputDirectory.isDirectory()) { | ||
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Output directory " + outputDirectoryOption + " is not a directory"); | ||
} | ||
} | ||
// load GeneratorLoader by ServiceLoader | ||
ServiceLoader<GeneratorLoader> genLoaders = ServiceLoader.load(GeneratorLoader.class, Processor.class.getClassLoader()); | ||
Iterator<GeneratorLoader> it = genLoaders.iterator(); | ||
|
@@ -126,15 +109,6 @@ private Collection<? extends Generator<?>> getCodeGenerators() { | |
} | ||
} | ||
|
||
relocations = processingEnv.getOptions() | ||
.entrySet() | ||
.stream() | ||
.filter(e -> e.getKey().startsWith("codegen.output.")) | ||
.collect(Collectors.toMap( | ||
e -> e.getKey().substring("codegen.output.".length()), | ||
Map.Entry::getValue) | ||
); | ||
|
||
codeGenerators = generators; | ||
} | ||
return codeGenerators; | ||
|
@@ -278,22 +252,7 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment | |
if (codeGenerator.kinds.contains(model.getKind())) { | ||
String relativeName = codeGenerator.filename(model); | ||
if (relativeName != null) { | ||
int kind; | ||
if (relativeName.endsWith(".java") && !relativeName.contains("/")) { | ||
String relocation = relocations.get(codeGenerator.name); | ||
if (relocation != null) { | ||
kind = OTHER; | ||
relativeName = relocation + '/' + | ||
relativeName.substring(0, relativeName.length() - ".java".length()).replace('.', '/') + ".java"; | ||
} else { | ||
kind = JAVA; | ||
} | ||
} else if (relativeName.startsWith("resources/")) { | ||
kind = RESOURCE; | ||
} else { | ||
kind = OTHER; | ||
} | ||
if (kind == JAVA) { | ||
if (relativeName.endsWith(".java")) { | ||
// Special handling for .java | ||
String fqn = relativeName.substring(0, relativeName.length() - ".java".length()); | ||
// Avoid to recreate the same file (this may happen as we unzip and recompile source trees) | ||
|
@@ -302,12 +261,9 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment | |
} | ||
List<ModelProcessing> processings = generatedClasses.computeIfAbsent(fqn, GeneratedFile::new); | ||
processings.add(new ModelProcessing(model, codeGenerator)); | ||
} else if (kind == RESOURCE) { | ||
relativeName = relativeName.substring("resources/".length()); | ||
List<ModelProcessing> processings = generatedResources.computeIfAbsent(relativeName, GeneratedFile::new); | ||
processings.add(new ModelProcessing(model, codeGenerator)); | ||
} else { | ||
List<ModelProcessing> processings = generatedFiles.computeIfAbsent(relativeName, GeneratedFile::new); | ||
// RESOURCE | ||
List<ModelProcessing> processings = generatedResources.computeIfAbsent(relativeName, GeneratedFile::new); | ||
processings.add(new ModelProcessing(model, codeGenerator)); | ||
} | ||
} | ||
|
@@ -322,7 +278,6 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment | |
|
||
// Generate classes | ||
generatedClasses.values().forEach(generated -> { | ||
boolean shouldWarningsBeSuppressed = false; | ||
try { | ||
String content = generated.generate(); | ||
if (content.length() > 0) { | ||
|
@@ -343,11 +298,11 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment | |
|
||
// Generate resources | ||
for (GeneratedFile generated : generatedResources.values()) { | ||
boolean shouldWarningsBeSuppressed = false; | ||
try { | ||
String content = generated.generate(); | ||
if (content.length() > 0) { | ||
try (Writer w = processingEnv.getFiler().createResource(StandardLocation.CLASS_OUTPUT, "", generated.uri).openWriter()) { | ||
if (!content.isEmpty()) { | ||
try (Writer w = processingEnv.getFiler() | ||
.createResource(StandardLocation.CLASS_OUTPUT, "", generated.uri).openWriter()) { | ||
w.write(content); | ||
} | ||
boolean createSource; | ||
|
@@ -371,30 +326,6 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment | |
reportException(e, generated.get(0).model.getElement()); | ||
} | ||
} | ||
// Generate files | ||
generatedFiles.values().forEach(generated -> { | ||
Path path = new File(generated.uri).toPath(); | ||
if (path.isAbsolute()) { | ||
// Nothing to do | ||
} else if (outputDirectory != null) { | ||
path = outputDirectory.toPath().resolve(path); | ||
} else { | ||
return; | ||
} | ||
File file = path.toFile(); | ||
Helper.ensureParentDir(file); | ||
String content = generated.generate(); | ||
if (content.length() > 0) { | ||
try (FileWriter fileWriter = new FileWriter(file)) { | ||
fileWriter.write(content); | ||
} catch (GenException e) { | ||
reportGenException(e); | ||
} catch (Exception e) { | ||
reportException(e, generated.get(0).model.getElement()); | ||
} | ||
processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, "Generated model " + generated.get(0).model.getFqn() + ": " + generated.uri); | ||
} | ||
}); | ||
} | ||
return true; | ||
} | ||
|
Oops, something went wrong.