use of io.quarkus.deployment.builditem.GeneratedClassBuildItem in project quarkus-github-app by quarkiverse.
the class GitHubAppProcessor method generateClasses.
@BuildStep
void generateClasses(CombinedIndexBuildItem combinedIndex, LaunchModeBuildItem launchMode, BuildProducer<AdditionalBeanBuildItem> additionalBeans, BuildProducer<GeneratedBeanBuildItem> generatedBeans, BuildProducer<GeneratedClassBuildItem> generatedClasses, BuildProducer<ReflectiveClassBuildItem> reflectiveClasses, BuildProducer<AnnotationsTransformerBuildItem> annotationsTransformer) {
Collection<EventDefinition> allEventDefinitions = getAllEventDefinitions(combinedIndex.getIndex());
// Add @Vetoed to all the user-defined event listening classes
annotationsTransformer.produce(new AnnotationsTransformerBuildItem(new VetoUserDefinedEventListeningClassesAnnotationsTransformer(allEventDefinitions.stream().map(d -> d.getAnnotation()).collect(Collectors.toSet()))));
// Add the qualifiers as beans
String[] subscriberAnnotations = allEventDefinitions.stream().map(d -> d.getAnnotation().toString()).toArray(String[]::new);
additionalBeans.produce(new AdditionalBeanBuildItem(subscriberAnnotations));
DispatchingConfiguration dispatchingConfiguration = getDispatchingConfiguration(combinedIndex.getIndex(), allEventDefinitions);
ClassOutput classOutput = new GeneratedClassGizmoAdaptor(generatedClasses, true);
generateAnnotationLiterals(classOutput, dispatchingConfiguration);
ClassOutput beanClassOutput = new GeneratedBeanGizmoAdaptor(generatedBeans);
generateDispatcher(beanClassOutput, combinedIndex, launchMode, dispatchingConfiguration, reflectiveClasses);
generateMultiplexers(beanClassOutput, dispatchingConfiguration, reflectiveClasses);
}
use of io.quarkus.deployment.builditem.GeneratedClassBuildItem in project camel-quarkus by apache.
the class XsltProcessor method xsltResources.
@BuildStep
void xsltResources(CamelXsltConfig config, BuildProducer<XsltGeneratedClassBuildItem> generatedNames, BuildProducer<GeneratedClassBuildItem> generatedClasses, BuildProducer<UriResolverEntryBuildItem> uriResolverEntries) throws Exception {
final Path destination = Files.createTempDirectory(XsltFeature.FEATURE);
final Set<String> translets = new LinkedHashSet<>();
try {
final BuildTimeUriResolver resolver = new BuildTimeUriResolver();
for (String uri : config.sources.orElse(Collections.emptyList())) {
ResolutionResult resolvedUri = resolver.resolve(uri);
uriResolverEntries.produce(resolvedUri.toBuildItem());
if (translets.contains(resolvedUri.transletClassName)) {
throw new RuntimeException("XSLT translet name clash: cannot add '" + resolvedUri.transletClassName + "' to previously added translets " + translets);
}
translets.add(resolvedUri.transletClassName);
try {
TransformerFactory tf = new XalanTransformerFactory();
for (Map.Entry<String, Boolean> entry : config.features.entrySet()) {
tf.setFeature(entry.getKey(), entry.getValue());
}
tf.setAttribute("generate-translet", true);
tf.setAttribute("translet-name", resolvedUri.transletClassName);
tf.setAttribute("package-name", config.packageName);
tf.setAttribute("destination-directory", destination.toString());
tf.setErrorListener(new CamelXsltErrorListener());
tf.newTemplates(resolvedUri.source);
} catch (TransformerException e) {
throw new RuntimeException("Could not compile XSLT " + uri, e);
}
}
Files.walk(destination).sorted(Comparator.reverseOrder()).filter(Files::isRegularFile).filter(path -> path.toString().endsWith(".class")).forEach(path -> {
try {
final Path rel = destination.relativize(path);
final String fqcn = StringUtils.removeEnd(rel.toString(), ".class").replace(File.separatorChar, '.');
final byte[] data = Files.readAllBytes(path);
generatedClasses.produce(new GeneratedClassBuildItem(false, fqcn, data));
generatedNames.produce(new XsltGeneratedClassBuildItem(fqcn));
} catch (IOException e) {
throw new RuntimeException(e);
}
});
} finally {
Files.walk(destination).sorted(Comparator.reverseOrder()).map(Path::toFile).forEach(File::delete);
}
}
use of io.quarkus.deployment.builditem.GeneratedClassBuildItem in project camel-quarkus by apache.
the class CSimpleProcessor method compileCSimpleExpressions.
@BuildStep
void compileCSimpleExpressions(List<CSimpleExpressionSourceBuildItem> expressionSources, BuildProducer<CompiledCSimpleExpressionBuildItem> compiledCSimpleExpression, BuildProducer<GeneratedClassBuildItem> generatedClasses) throws IOException {
if (!expressionSources.isEmpty()) {
final Set<String> imports = new TreeSet<>();
final Map<String, String> aliases = new LinkedHashMap<>();
final ClassLoader loader = Thread.currentThread().getContextClassLoader();
if (!(loader instanceof QuarkusClassLoader)) {
throw new IllegalStateException(QuarkusClassLoader.class.getSimpleName() + " expected as the context class loader");
}
final QuarkusClassLoader quarkusClassLoader = (QuarkusClassLoader) loader;
readConfig(imports, aliases, loader);
final CSimpleCodeGenerator generator = new CSimpleCodeGenerator();
generator.setAliases(aliases);
generator.setImports(imports);
final Path projectDir = Paths.get(".").toAbsolutePath().normalize();
final Path csimpleGeneratedSourceDir = projectDir.resolve("target/generated/csimple");
Files.createDirectories(csimpleGeneratedSourceDir);
final Set<File> filesToCompile = new LinkedHashSet<>();
/* We do not want to compile the same source twice, so we store here what we have compiled already */
final Map<Boolean, Set<String>> compiledExpressions = new HashMap<>();
compiledExpressions.put(true, new HashSet<>());
compiledExpressions.put(false, new HashSet<>());
/* Generate Java classes for the language expressions */
for (CSimpleExpressionSourceBuildItem expr : expressionSources) {
final boolean predicate = expr.isPredicate();
final String script = expr.getSourceCode();
if (!compiledExpressions.get(predicate).contains(script)) {
final CSimpleGeneratedCode code = predicate ? generator.generatePredicate(expr.getClassNameBase(), script) : generator.generateExpression(expr.getClassNameBase(), script);
compiledCSimpleExpression.produce(new CompiledCSimpleExpressionBuildItem(code.getCode(), predicate, code.getFqn()));
final Path javaCsimpleFile = csimpleGeneratedSourceDir.resolve(code.getFqn().replace('.', '/') + ".java");
Files.createDirectories(javaCsimpleFile.getParent());
Files.write(javaCsimpleFile, code.getCode().getBytes(StandardCharsets.UTF_8));
filesToCompile.add(javaCsimpleFile.toFile());
compiledExpressions.get(predicate).add(script);
}
}
final Path csimpleClassesDir = projectDir.resolve("target/csimple-classes");
Files.createDirectories(csimpleClassesDir);
/* Compile the generated sources */
try (JavaCompilationProvider compiler = new JavaCompilationProvider()) {
final Context context = compilationContext(projectDir, csimpleClassesDir, quarkusClassLoader);
compiler.compile(filesToCompile, context);
}
/* Register the compiled classes via Quarkus GeneratedClassBuildItem */
try (Stream<Path> classFiles = Files.walk(csimpleClassesDir)) {
classFiles.filter(Files::isRegularFile).filter(p -> p.getFileName().toString().endsWith(CLASS_EXT)).forEach(p -> {
final Path relPath = csimpleClassesDir.relativize(p);
String className = FileUtils.nixifyPath(relPath.toString());
className = className.substring(0, className.length() - CLASS_EXT.length());
try {
final GeneratedClassBuildItem item = new GeneratedClassBuildItem(true, className, Files.readAllBytes(p));
generatedClasses.produce(item);
} catch (IOException e) {
throw new RuntimeException("Could not read " + p);
}
});
}
}
}
use of io.quarkus.deployment.builditem.GeneratedClassBuildItem in project quarkus-github-action by quarkiverse.
the class GitHubActionProcessor method generateClasses.
@BuildStep
void generateClasses(CombinedIndexBuildItem combinedIndex, LaunchModeBuildItem launchMode, BuildProducer<AdditionalBeanBuildItem> additionalBeans, BuildProducer<GeneratedBeanBuildItem> generatedBeans, BuildProducer<GeneratedClassBuildItem> generatedClasses, BuildProducer<ReflectiveClassBuildItem> reflectiveClasses, BuildProducer<AnnotationsTransformerBuildItem> annotationsTransformer) {
Collection<EventDefinition> allEventDefinitions = getAllEventDefinitions(combinedIndex.getIndex());
// Add @Vetoed to all the user-defined event listening classes
annotationsTransformer.produce(new AnnotationsTransformerBuildItem(new VetoUserDefinedEventListeningClassesAnnotationsTransformer(allEventDefinitions.stream().map(d -> d.getAnnotation()).collect(Collectors.toSet()))));
// Add the qualifiers as beans
String[] subscriberAnnotations = allEventDefinitions.stream().map(d -> d.getAnnotation().toString()).toArray(String[]::new);
additionalBeans.produce(new AdditionalBeanBuildItem(subscriberAnnotations));
additionalBeans.produce(new AdditionalBeanBuildItem(Action.class));
DispatchingConfiguration dispatchingConfiguration = getDispatchingConfiguration(combinedIndex.getIndex(), allEventDefinitions);
ClassOutput classOutput = new GeneratedClassGizmoAdaptor(generatedClasses, true);
generateAnnotationLiterals(classOutput, dispatchingConfiguration);
ClassOutput beanClassOutput = new GeneratedBeanGizmoAdaptor(generatedBeans);
generatePayloadTypeResolver(beanClassOutput, reflectiveClasses, allEventDefinitions);
generateActionMain(beanClassOutput, combinedIndex, launchMode, dispatchingConfiguration, reflectiveClasses);
generateMultiplexers(beanClassOutput, dispatchingConfiguration, reflectiveClasses);
}
Aggregations