use of org.jboss.forge.roaster.model.source.AnnotationSource in project camel by apache.
the class CamelJavaParserHelper method extractEndpointUriFromArgument.
private static void extractEndpointUriFromArgument(String node, JavaClassSource clazz, Block block, List<ParserResult> uris, Object arg, boolean strings, boolean fields) {
if (strings) {
String uri = getLiteralValue(clazz, block, (Expression) arg);
if (!Strings.isBlank(uri)) {
int position = ((Expression) arg).getStartPosition();
// if the node is fromF or toF, then replace all %X with {{%X}} as we cannot parse that value
if ("fromF".equals(node) || "toF".equals(node)) {
uri = uri.replaceAll("\\%s", "\\{\\{\\%s\\}\\}");
uri = uri.replaceAll("\\%d", "\\{\\{\\%d\\}\\}");
uri = uri.replaceAll("\\%b", "\\{\\{\\%b\\}\\}");
}
uris.add(new ParserResult(node, position, uri));
return;
}
}
if (fields && arg instanceof SimpleName) {
FieldSource field = getField(clazz, block, (SimpleName) arg);
if (field != null) {
// find the endpoint uri from the annotation
AnnotationSource annotation = field.getAnnotation("org.apache.camel.cdi.Uri");
if (annotation == null) {
annotation = field.getAnnotation("org.apache.camel.EndpointInject");
}
if (annotation != null) {
Expression exp = (Expression) annotation.getInternal();
if (exp instanceof SingleMemberAnnotation) {
exp = ((SingleMemberAnnotation) exp).getValue();
} else if (exp instanceof NormalAnnotation) {
List values = ((NormalAnnotation) exp).values();
for (Object value : values) {
MemberValuePair pair = (MemberValuePair) value;
if ("uri".equals(pair.getName().toString())) {
exp = pair.getValue();
break;
}
}
}
String uri = CamelJavaParserHelper.getLiteralValue(clazz, block, exp);
if (!Strings.isBlank(uri)) {
int position = ((SimpleName) arg).getStartPosition();
uris.add(new ParserResult(node, position, uri));
}
} else {
// the field may be initialized using variables, so we need to evaluate those expressions
Object fi = field.getInternal();
if (fi instanceof VariableDeclaration) {
Expression exp = ((VariableDeclaration) fi).getInitializer();
String uri = CamelJavaParserHelper.getLiteralValue(clazz, block, exp);
if (!Strings.isBlank(uri)) {
// we want the position of the field, and not in the route
int position = ((VariableDeclaration) fi).getStartPosition();
uris.add(new ParserResult(node, position, uri));
}
}
}
}
}
// cannot parse it so add a failure
uris.add(new ParserResult(node, -1, arg.toString(), false));
}
use of org.jboss.forge.roaster.model.source.AnnotationSource in project camel by apache.
the class SpringBootAutoConfigurationMojo method createDataFormatAutoConfigurationSource.
private void createDataFormatAutoConfigurationSource(String packageName, DataFormatModel model, List<String> dataFormatAliases, boolean hasOptions, String overrideDataFormatName) throws MojoFailureException {
final JavaClassSource javaClass = Roaster.create(JavaClassSource.class);
int pos = model.getJavaType().lastIndexOf(".");
String name = model.getJavaType().substring(pos + 1);
name = name.replace("DataFormat", "DataFormatAutoConfiguration");
javaClass.setPackage(packageName).setName(name);
String doc = "Generated by camel-package-maven-plugin - do not edit this file!";
javaClass.getJavaDoc().setFullText(doc);
javaClass.addAnnotation(Configuration.class);
javaClass.addAnnotation(ConditionalOnBean.class).setStringValue("type", "org.apache.camel.spring.boot.CamelAutoConfiguration");
javaClass.addAnnotation(Conditional.class).setLiteralValue(name + ".Condition.class");
javaClass.addAnnotation(AutoConfigureAfter.class).setStringValue("name", "org.apache.camel.spring.boot.CamelAutoConfiguration");
String configurationName = name.replace("DataFormatAutoConfiguration", "DataFormatConfiguration");
if (hasOptions) {
AnnotationSource<JavaClassSource> ann = javaClass.addAnnotation(EnableConfigurationProperties.class);
ann.setLiteralValue("value", configurationName + ".class");
javaClass.addImport("java.util.HashMap");
javaClass.addImport("java.util.Map");
javaClass.addImport("org.apache.camel.util.IntrospectionSupport");
}
javaClass.addImport("org.apache.camel.CamelContextAware");
javaClass.addImport(model.getJavaType());
javaClass.addImport("org.apache.camel.CamelContext");
javaClass.addImport("org.apache.camel.RuntimeCamelException");
javaClass.addImport("org.apache.camel.spi.DataFormat");
javaClass.addImport("org.apache.camel.spi.DataFormatFactory");
String body = createDataFormatBody(model.getShortJavaType(), hasOptions);
String methodName = "configure" + model.getShortJavaType() + "Factory";
MethodSource<JavaClassSource> method = javaClass.addMethod().setName(methodName).setPublic().setBody(body).setReturnType("org.apache.camel.spi.DataFormatFactory");
method.addParameter("CamelContext", "camelContext").setFinal(true);
if (hasOptions) {
method.addParameter(configurationName, "configuration").setFinal(true);
}
// Determine all the aliases
// adding the '-dataformat' suffix to prevent collision with component names
String[] springBeanAliases = dataFormatAliases.stream().map(alias -> alias + "-dataformat-factory").toArray(size -> new String[size]);
method.addAnnotation(Bean.class).setStringArrayValue("name", springBeanAliases);
method.addAnnotation(ConditionalOnClass.class).setLiteralValue("value", "CamelContext.class");
method.addAnnotation(ConditionalOnMissingBean.class).setLiteralValue("value", model.getShortJavaType() + ".class");
// Generate Condition
javaClass.addNestedType(createConditionType(javaClass, "camel.dataformat", (overrideDataFormatName != null ? overrideDataFormatName : model.getName()).toLowerCase(Locale.US)));
sortImports(javaClass);
String fileName = packageName.replaceAll("\\.", "\\/") + "/" + name + ".java";
writeSourceIfChanged(javaClass, fileName);
writeAdditionalSpringMetaData("camel", "dataformat", (overrideDataFormatName != null ? overrideDataFormatName : model.getName()).toLowerCase(Locale.US));
}
use of org.jboss.forge.roaster.model.source.AnnotationSource in project Entitas-Java by Rubentxu.
the class EntityIndexDataProvider method createCustomEntityIndexData.
ComponentData createCustomEntityIndexData(ComponentData data) {
AnnotationSource annotation = data.getSource().getAnnotation(CustomEntityIndex.class);
setEntityIndexType(data, data.getSource().getName());
isCustom(data, true);
setEntityIndexName(data, data.getSource().getCanonicalName());
setContextNames(data, Arrays.asList(annotation.getStringValue()));
setCustomMethods(data, data.getSource().getMethods().stream().filter(m -> !m.isConstructor()).collect(Collectors.toList()));
return data;
}
use of org.jboss.forge.roaster.model.source.AnnotationSource in project kie-wb-common by kiegroup.
the class DataModelerServiceImpl method validateValuePair.
@Override
public List<ValidationMessage> validateValuePair(String annotationClassName, ElementType target, String valuePairName, String literalValue) {
// Currently we only validate the syntax but additional checks may be added.
List<ValidationMessage> validationMessages = new ArrayList<ValidationMessage>();
JavaRoasterModelDriver modelDriver = new JavaRoasterModelDriver();
Pair<AnnotationSource<JavaClassSource>, List<DriverError>> parseResult = modelDriver.parseAnnotationWithValuePair(annotationClassName, target, valuePairName, literalValue);
if (parseResult.getK2() != null && parseResult.getK2().size() > 0) {
ValidationMessage validationMessage;
for (DriverError driverError : parseResult.getK2()) {
validationMessage = new ValidationMessage();
validationMessage.setText(driverError.getMessage());
validationMessage.setColumn(driverError.getColumn());
validationMessage.setLine(driverError.getLine());
validationMessage.setLevel(Level.ERROR);
validationMessages.add(validationMessage);
}
}
return validationMessages;
}
use of org.jboss.forge.roaster.model.source.AnnotationSource in project kie-wb-common by kiegroup.
the class JavaRoasterModelDriver method resolveAnnotationSource.
public org.kie.workbench.common.services.datamodeller.driver.model.AnnotationSource resolveAnnotationSource(Annotation annotation) {
org.kie.workbench.common.services.datamodeller.driver.model.AnnotationSource annotationSource = new org.kie.workbench.common.services.datamodeller.driver.model.AnnotationSource();
// TODO this method can be optimized and likely migrated to Roaster. Should be reviewed when we evaluate
// the removal of Velocity.
GenerationTools generationTools = new GenerationTools();
AnnotationDefinition annotationDefinition;
StringBuilder annotationCode = new StringBuilder();
annotationCode.append("@");
annotationCode.append(annotation.getClassName());
if ((annotationDefinition = annotation.getAnnotationDefinition()) != null) {
if (!annotationDefinition.isMarker()) {
annotationCode.append(generationTools.resolveAnnotationType(annotation));
}
if (annotationDefinition.getValuePairs() != null) {
Object value;
String valuePairCode;
for (AnnotationValuePairDefinition valuePairDefinition : annotationDefinition.getValuePairs()) {
if ((value = annotation.getValue(valuePairDefinition.getName())) != null) {
valuePairCode = generationTools.resolveMemberTypeExpression(valuePairDefinition, value);
} else {
valuePairCode = null;
}
annotationSource.withValuePairSource(valuePairDefinition.getName(), valuePairCode);
}
}
}
annotationSource.withSource(annotationCode.toString());
return annotationSource;
}
Aggregations