org.jboss.forge.roaster.model.source.JavaClassSource Java Examples
The following examples show how to use
org.jboss.forge.roaster.model.source.JavaClassSource.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CamelJavaParserHelper.java From fabric8-forge with Apache License 2.0 | 6 votes |
public static MethodSource<JavaClassSource> findConfigureMethod(JavaClassSource clazz) { MethodSource<JavaClassSource> method = clazz.getMethod("configure"); // must be public void configure() if (method != null && method.isPublic() && method.getParameters().isEmpty() && method.getReturnType().isType("void")) { return method; } // maybe the route builder is from unit testing with camel-test as an anonymous inner class // there is a bit of code to dig out this using the eclipse jdt api method = findCreateRouteBuilderMethod(clazz); if (method != null) { return findConfigureMethodInCreateRouteBuilder(clazz, method); } return null; }
Example #2
Source File: CodeGeneratorOld.java From Entitas-Java with MIT License | 6 votes |
public static void toFile(JavaClassSource javaClass, File srcFolder) { File f = srcFolder; String[] parts = javaClass.getPackage().split("\\."); try { if (!srcFolder.getAbsolutePath().endsWith(parts[parts.length - 1])) { f = new File(f, parts[parts.length - 1]); createParentDirs(f); } f = new File(f, javaClass.getName() + ".java"); write(f, javaClass.toString()); } catch (IOException e) { e.printStackTrace(); } }
Example #3
Source File: InspectionResultProcessorTest.java From angularjs-addon with Eclipse Public License 1.0 | 6 votes |
@Test public void testInspectBasicFieldMaxSize() throws Exception { String entityName = "Customer"; String fieldName = "firstName"; generateSimpleEntity(entityName); generateStringField(fieldName); generateSizeConstraint(fieldName, null, "5"); JavaClassSource klass = getJavaClassFor(entityName); List<Map<String, String>> inspectionResult = metawidgetInspectorFacade.inspect(klass); inspectionResult = angularResultEnhancer.enhanceResults(klass, inspectionResult); assertThat(inspectionResult, hasItemWithEntry("name", fieldName)); assertThat(inspectionResult, hasItemWithEntry("maximum-length", "5")); }
Example #4
Source File: ContextGenerator.java From Entitas-Java with MIT License | 6 votes |
private void addContextReplaceMethods(String contextName, ComponentInfo info, JavaClassSource source) { if (!info.isSingletonComponent) { source.addMethod() .setName(String.format("replace%1$s", info.typeName)) .setReturnType(contextName + "Entity") .setPublic() .setParameters(info.constructores != null && info.constructores.size() > 0 ? memberNamesWithTypeFromConstructor(info.constructores.get(0)) : memberNamesWithType(info.memberInfos)) .setBody(String.format("%3$sEntity entity = get%1$sEntity();" + " if(entity == null) {" + " entity = set%1$s(%2$s);" + " } else { " + " entity.replace%1$s(%2$s);" + " }" + " return entity;" , info.typeName, info.constructores != null && info.constructores.size() > 0 ? memberNamesFromConstructor(info.constructores.get(0)) : memberNames(info.memberInfos) , contextName)); } }
Example #5
Source File: EntitasGenerator.java From Entitas-Java with MIT License | 6 votes |
private void createContextFields(JavaClassSource javaClass, Set<String> contextNames) { javaClass.addImport("Context"); javaClass.addImport("ilargia.entitas.api.*"); contextNames.forEach((contextName) -> { javaClass.addMethod() .setName(String.format("factory%1$sEntity", contextName)) .setReturnType(String.format("EntityBaseFactory<%1$sEntity>", contextName)) .setPublic() .setBody(String.format(" return () -> { \n" + " return new %1$sEntity();\n" + " };", contextName)); javaClass.addField() .setName(contextName.toLowerCase()) .setType(contextName + "Context") .setPublic(); }); }
Example #6
Source File: ComponentLookupGenerator.java From Entitas-Java with MIT License | 6 votes |
private JavaClassSource generateIndicesLookup(String contextName, List<ComponentData> dataList) { String pkgDestiny = targetPackageConfig.getTargetPackage(); JavaClassSource codeGen = Roaster.parse(JavaClassSource.class, String.format("public class %1$s {}", WordUtils.capitalize(contextName) + DEFAULT_COMPONENT_LOOKUP_TAG)); if (dataList.size() > 0 && !pkgDestiny.endsWith(dataList.get(0).getSubDir()) ) { // pkgDestiny += "." + dataList.get(0).getSubDir(); } codeGen.setPackage(pkgDestiny); addIndices(dataList, codeGen); addComponentNames(dataList, codeGen); addComponentTypes(dataList, codeGen); System.out.println(codeGen); return codeGen; }
Example #7
Source File: CreateTestClassCommand.java From thorntail-addon with Eclipse Public License 1.0 | 6 votes |
@Override public Result execute(UIExecutionContext context) throws Exception { Project project = getSelectedProject(context); final DependencyFacet dependencyFacet = project.getFacet(DependencyFacet.class); if (!isArquillianWildflySwarmDependencyInstalled(dependencyFacet)) { installArquillianWildflySwarmDependency(dependencyFacet); } JavaClassSource test = Roaster.create(JavaClassSource.class) .setPackage(targetPackage.getValue()) .setName(named.getValue()); addArquillianRunner(test); addDefaultDeploymentAnnotation(test, project); addArquillianResourceEnricher(test); addTestMethod(test); JavaSourceFacet facet = project.getFacet(JavaSourceFacet.class); facet.saveTestJavaSource(test); return Results.success(String.format("Test Class %s.%s was created", targetPackage.getValue(), named.getValue())); }
Example #8
Source File: ContextGenerator.java From Entitas-Java with MIT License | 6 votes |
private void addContextGetMethods(String contextName, ComponentInfo info, JavaClassSource source) { source.addMethod() .setName(String.format("get%1$sEntity", info.typeName)) .setReturnType(contextName + "Entity") .setPublic() .setBody(String.format("return getGroup(%1$sMatcher.%2$s()).getSingleEntity();" , CodeGeneratorOld.capitalize(info.contexts.get(0)), info.typeName)); if (!info.isSingletonComponent) { source.addMethod() .setName(String.format("get%1$s", info.typeName)) .setReturnType(info.typeName) .setPublic() .setBody(String.format("return get%1$sEntity().get%1$s();" , info.typeName)); } }
Example #9
Source File: CamelJavaParserHelper.java From fabric8-forge with Apache License 2.0 | 6 votes |
public static List<MethodSource<JavaClassSource>> findInlinedConfigureMethods(JavaClassSource clazz) { List<MethodSource<JavaClassSource>> answer = new ArrayList<>(); List<MethodSource<JavaClassSource>> methods = clazz.getMethods(); if (methods != null) { for (MethodSource<JavaClassSource> method : methods) { if (method.isPublic() && (method.getParameters() == null || method.getParameters().isEmpty()) && (method.getReturnType() == null || method.getReturnType().isType("void"))) { // maybe the method contains an inlined createRouteBuilder usually from an unit test method MethodSource<JavaClassSource> builder = findConfigureMethodInCreateRouteBuilder(clazz, method); if (builder != null) { answer.add(builder); } } } } return answer; }
Example #10
Source File: ComponentIndicesGenerator.java From Entitas-Java with MIT License | 6 votes |
public void addComponentFactories(ComponentInfo[] componentInfos, JavaClassSource javaClass) { String format = " %1$s.class,\n"; String code = "return new FactoryComponent[] {"; for (int i = 0; i < componentInfos.length; i++) { ComponentInfo info = componentInfos[i]; JavaInterfaceSource interfaceSource = Roaster.parse(JavaInterfaceSource.class, String.format("public interface Factory%1$s extends FactoryComponent {}", info.typeName)); interfaceSource.addMethod() .setName(String.format("create%1$s", info.typeName)) .setReturnType(info.typeName) .setPublic(); javaClass.addNestedType(interfaceSource.toString()); } }
Example #11
Source File: InspectionResultProcessor.java From angularjs-addon with Eclipse Public License 1.0 | 6 votes |
private List<Map<String, String>> expandEmbeddableTypes(Map<String, String> propertyAttributes) { String isEmbeddableAttribute = propertyAttributes.get(ForgeInspectionResultConstants.EMBEDDABLE); boolean isEmbeddable = isEmbeddableAttribute != null && isEmbeddableAttribute.equals(InspectionResultConstants.TRUE); if (isEmbeddable) { String embeddedType = propertyAttributes.get(TYPE); JavaClassSource javaClass = getJavaClass(embeddedType); List<Map<String, String>> embeddedTypeInspectionResults = metawidgetInspectorFacade.inspect(javaClass); List<Map<String, String>> expandedInspectionResults = new ArrayList<>(); for (Map<String, String> embeddedPropertyAttribute : embeddedTypeInspectionResults) { embeddedPropertyAttribute.put(LABEL, StringUtils.uncamelCase(embeddedPropertyAttribute.get(NAME))); embeddedPropertyAttribute.put(TYPE, embeddedPropertyAttribute.get(TYPE)); embeddedPropertyAttribute.put(JS_IDENTIFIER, propertyAttributes.get(NAME) + StringUtils.camelCase(embeddedPropertyAttribute.get(NAME))); embeddedPropertyAttribute.put(NAME, propertyAttributes.get(NAME) + "." + embeddedPropertyAttribute.get(NAME)); expandedInspectionResults.add(embeddedPropertyAttribute); } return expandedInspectionResults; } return null; }
Example #12
Source File: InspectionResultProcessorTest.java From angularjs-addon with Eclipse Public License 1.0 | 6 votes |
@Test public void testInspectManyToOneField() throws Exception { String entityName = "Customer"; String fieldName = "category"; String relatedEntityName = "CustomCategory"; generateSimpleEntity(relatedEntityName); String relatedEntityType = getJavaClassNameFor(relatedEntityName); generateSimpleEntity(entityName); generateManyToOneField(fieldName, relatedEntityType, null, null, false, null); JavaClassSource klass = getJavaClassFor(entityName); List<Map<String, String>> inspectionResult = metawidgetInspectorFacade.inspect(klass); inspectionResult = angularResultEnhancer.enhanceResults(klass, inspectionResult); assertThat(inspectionResult, hasItemWithEntry("name", fieldName)); assertThat(inspectionResult, hasItemWithEntry("many-to-one", "true")); assertThat(inspectionResult, hasItemWithEntry("type", relatedEntityType)); }
Example #13
Source File: MatcherGenerator.java From Entitas-Java with MIT License | 6 votes |
private JavaClassSource generateMatchers(String contextName, List<ComponentInfo> componentInfos, String pkgDestiny) { JavaClassSource javaClass = Roaster.parse(JavaClassSource.class, String.format("public class %1$s {}", CodeGeneratorOld.capitalize(contextName) + "Matcher")); if (componentInfos.size() > 0 && !pkgDestiny.endsWith(componentInfos.get(0).subDir)) { pkgDestiny += "." + componentInfos.get(0).subDir; } javaClass.setPackage(pkgDestiny); //javaClass.addImport("com.ilargia.games.entitas.interfaces.IMatcher"); javaClass.addImport("Matcher"); for (ComponentInfo info : componentInfos) { addMatcher(contextName, info, javaClass); addMatcherMethods(contextName, info, javaClass); } System.out.println(javaClass); return javaClass; }
Example #14
Source File: MatcherGenerator.java From Entitas-Java with MIT License | 6 votes |
private void addMatcherMethods(String contextName, ComponentInfo info, JavaClassSource javaClass) { String body = "if (_matcher%2$s == null) {" + " Matcher matcher = (Matcher)Matcher.AllOf(%1$s.%2$s);" + " matcher.componentNames = %1$s.componentNames();" + " _matcher%2$s = matcher;" + "}" + "return _matcher%2$s;"; javaClass.addMethod() .setName(info.typeName) .setReturnType("Matcher") .setPublic() .setStatic(true) .setBody(String.format(body, CodeGeneratorOld.capitalize(contextName) + CodeGeneratorOld.DEFAULT_COMPONENT_LOOKUP_TAG, info.typeName)); }
Example #15
Source File: EhDaoGenerator.java From MHViewer with Apache License 2.0 | 6 votes |
private static void adjustQuickSearch() throws Exception { JavaClassSource javaClass = Roaster.parse(JavaClassSource.class, new File(QUICK_SEARCH_PATH)); // Set all field public javaClass.getField("id").setPublic(); javaClass.getField("name").setPublic(); javaClass.getField("mode").setPublic(); javaClass.getField("category").setPublic(); javaClass.getField("keyword").setPublic(); javaClass.getField("advanceSearch").setPublic(); javaClass.getField("minRating").setPublic(); javaClass.getField("pageFrom").setPublic(); javaClass.getField("pageTo").setPublic(); javaClass.getField("time").setPublic(); javaClass.addMethod("\t@Override\n" + "\tpublic String toString() {\n" + "\t\treturn name;\n" + "\t}"); FileWriter fileWriter = new FileWriter(QUICK_SEARCH_PATH); fileWriter.write(javaClass.toString()); fileWriter.close(); }
Example #16
Source File: ContextGenerator.java From Entitas-Java with MIT License | 6 votes |
private void addContextAddMethods(String contextName, ComponentInfo info, JavaClassSource source) { if (!info.isSingletonComponent) { source.addMethod() .setName(String.format("set%1$s", info.typeName)) .setReturnType(contextName + "Entity") .setPublic() .setParameters(info.constructores != null && info.constructores.size() > 0 ? memberNamesWithTypeFromConstructor(info.constructores.get(0)) : memberNamesWithType(info.memberInfos)) .setBody(String.format("if(has%1$s()) {\n" + " throw new EntitasException(\"Could not set %1$s!\" + this + \" already has an entity with %1$s!\", " + "\"You should check if the context already has a %1$sEntity before setting it or use context.Replace%1$s().\");" + " }\n" + " %3$sEntity entity = createEntity();\n" + " entity.add%1$s(%2$s);\n" + " return entity;" , info.typeName, info.constructores != null && info.constructores.size() > 0 ? memberNamesFromConstructor(info.constructores.get(0)) : memberNames(info.memberInfos) , contextName)); source.addImport(info.fullTypeName); } }
Example #17
Source File: InspectionResultProcessorTest.java From angularjs-addon with Eclipse Public License 1.0 | 5 votes |
@Test public void testInspectIntNumberField() throws Exception { String entityName = "Customer"; String fieldName = "age"; generateSimpleEntity(entityName); generateNumericField(fieldName, "int"); JavaClassSource klass = getJavaClassFor(entityName); List<Map<String, String>> inspectionResult = metawidgetInspectorFacade.inspect(klass); inspectionResult = angularResultEnhancer.enhanceResults(klass, inspectionResult); assertThat(inspectionResult, hasItemWithEntry("name", fieldName)); assertThat(inspectionResult, hasItemWithEntry("type", "number")); }
Example #18
Source File: EntitasGenerator.java From Entitas-Java with MIT License | 5 votes |
private void createMethodConstructor(JavaClassSource javaClass, Set<String> contextNames) { String setAllContexts = contextNames.stream().reduce("\n", (acc, contextName) -> acc + " " + contextName.toLowerCase() + " = create" + CodeGeneratorOld.capitalize(contextName) + "Context();\n " ); javaClass.addMethod() .setConstructor(true) .setPublic() .setBody(setAllContexts); }
Example #19
Source File: ComponentContextGenerator.java From Entitas-Java with MIT License | 5 votes |
private void addContextAddMethods(String contextName, ComponentData data, JavaClassSource source) { if (!isUnique(data)) { List<MethodSource<JavaClassSource>> constructors = getConstructorData(data); source.addMethod() .setName(String.format("set%1$s", getTypeName(data))) .setReturnType(contextName + "Entity") .setPublic() .setParameters(constructors != null && constructors.size() > 0 ? memberNamesWithTypeFromConstructor(constructors.get(0)) : memberNamesWithType(getMemberData(data))) .setBody(String.format("if(has%1$s()) {\n" + " throw new EntitasException(\"Could not set %1$s!\" + this + \" already has an entity with %1$s!\", " + "\"You should check if the context already has a %1$sEntity before setting it or use context.Replace%1$s().\");" + " }\n" + " %3$sEntity entity = createEntity();\n" + " entity.add%1$s(%2$s);\n" + " return entity;" , getTypeName(data), constructors != null && constructors.size() > 0 ? memberNamesFromConstructor(constructors.get(0)) : memberNames(getMemberData(data)) , contextName)); source.addImport(getFullTypeName(data) ); } }
Example #20
Source File: EndpointDiagnosticService.java From camel-language-server with Apache License 2.0 | 5 votes |
private List<CamelEndpointDetails> retrieveEndpoints(String uri, String camelText) { List<CamelEndpointDetails> endpoints = new ArrayList<>(); if (uri.endsWith(".xml")) { try { XmlRouteParser.parseXmlRouteEndpoints(new ByteArrayInputStream(camelText.getBytes(StandardCharsets.UTF_8)), "", "/"+uri, endpoints); } catch (Exception e) { logExceptionValidatingDocument(uri, e); } } else if(uri.endsWith(".java")) { JavaClassSource clazz = (JavaClassSource) Roaster.parse(camelText); RouteBuilderParser.parseRouteBuilderEndpoints(clazz, "", "/"+uri, endpoints); } return endpoints; }
Example #21
Source File: ProjectHelper.java From angularjs-addon with Eclipse Public License 1.0 | 5 votes |
public void addMaxConstraint(Project project, JavaClassSource klass, String propertyName, boolean onAccessor, String message, String max) throws FileNotFoundException { PropertySource<JavaClassSource> property = klass.getProperty(propertyName); final AnnotationSource<JavaClassSource> constraintAnnotation = addConstraintOnProperty(property, onAccessor, Max.class, message); constraintAnnotation.setLiteralValue(max); JavaSourceFacet javaSourceFacet = project.getFacet(JavaSourceFacet.class); javaSourceFacet.saveJavaSource(constraintAnnotation.getOrigin()); }
Example #22
Source File: RouteBuilderParser.java From fabric8-forge with Apache License 2.0 | 5 votes |
public static void parseRouteBuilderSimpleExpressions(JavaClassSource clazz, String baseDir, String fullyQualifiedFileName, List<CamelSimpleDetails> simpleExpressions) { MethodSource<JavaClassSource> method = CamelJavaParserHelper.findConfigureMethod(clazz); if (method != null) { List<ParserResult> expressions = CamelJavaParserHelper.parseCamelSimpleExpressions(method); for (ParserResult result : expressions) { if (result.isParsed()) { String fileName = fullyQualifiedFileName; if (fileName.startsWith(baseDir)) { fileName = fileName.substring(baseDir.length() + 1); } CamelSimpleDetails details = new CamelSimpleDetails(); details.setFileName(fileName); details.setClassName(clazz.getQualifiedName()); details.setMethodName("configure"); int line = findLineNumber(fullyQualifiedFileName, result.getPosition()); if (line > -1) { details.setLineNumber("" + line); } details.setSimple(result.getElement()); simpleExpressions.add(details); } } } }
Example #23
Source File: InspectionResultProcessorTest.java From angularjs-addon with Eclipse Public License 1.0 | 5 votes |
@Test public void testInspectBasicField() throws Exception { String entityName = "Customer"; String fieldName = "firstName"; generateSimpleEntity(entityName); generateStringField(fieldName); JavaClassSource klass = getJavaClassFor(entityName); List<Map<String, String>> inspectionResult = metawidgetInspectorFacade.inspect(klass); inspectionResult = angularResultEnhancer.enhanceResults(klass, inspectionResult); assertThat(inspectionResult, hasItemWithEntry("name", fieldName)); }
Example #24
Source File: ComponentDataProvider.java From Entitas-Java with MIT License | 5 votes |
List<String> getComponentNames(JavaClassSource type) { AnnotationSource<JavaClassSource> annotation = type.getAnnotation(CustomComponentName.class); if (annotation != null) { return Arrays.asList(annotation.getStringArrayValue("componentNames")); } else { return new ArrayList<>(); } }
Example #25
Source File: CreateTestClassCommand.java From thorntail-addon with Eclipse Public License 1.0 | 5 votes |
private void addDefaultDeploymentAnnotation(JavaClassSource test, Project project) throws ClassNotFoundException, IOException { test.addImport("org.wildfly.swarm.arquillian.DefaultDeployment"); final AnnotationSource<JavaClassSource> defaultDeploymentAnnotation = test.addAnnotation("DefaultDeployment"); if (asClient.hasValue()) { defaultDeploymentAnnotation.setLiteralValue("testable", "false"); } if (archiveType.hasValue()) { defaultDeploymentAnnotation.setLiteralValue("type", String.format("DefaultDeployment.Type.%s", archiveType.getValue())); } }
Example #26
Source File: EntitasGenerator.java From Entitas-Java with MIT License | 5 votes |
@Override public List<JavaClassSource> generate(List<ComponentInfo> infos, String pkgDestiny) { Map<String, List<ComponentInfo>> mapContextsComponents = CodeGeneratorOld.generateMap(infos); List<JavaClassSource> result = new ArrayList<>(); JavaClassSource source = generateEntitas(mapContextsComponents.keySet(), pkgDestiny); mapContextsComponents.forEach((context, lista) -> { String fullTypename = lista.get(0).fullTypeName; }); result.add(source); return result; }
Example #27
Source File: MatcherGenerator.java From Entitas-Java with MIT License | 5 votes |
private JavaClassSource addMatcher(String contextName, ComponentInfo info, JavaClassSource javaClass) { javaClass.addField() .setName("_matcher" + info.typeName) .setType("Matcher") .setPrivate() .setStatic(true); return null; }
Example #28
Source File: ContextGenerator.java From Entitas-Java with MIT License | 5 votes |
private JavaClassSource generateContext(String contextName, List<ComponentInfo> infos, String pkgDestiny) { JavaClassSource contextClass = Roaster.parse(JavaClassSource.class, String.format("public class %1$sContext extends Context<%1$sEntity> {}", contextName)); // if(infos.size() > 0 && infos.get(0).directory !=null) { // pkgDestiny+= "."+infos.get(0).directory; // // } if (infos.size() > 0 && !pkgDestiny.endsWith(infos.get(0).subDir)) { pkgDestiny += "." + infos.get(0).subDir; } contextClass.setPackage(pkgDestiny); contextClass.addMethod() .setName(contextName + "Context") .setPublic() .setConstructor(true) .setParameters(String.format("int totalComponents, int startCreationIndex, ContextInfo contextInfo, EntityBaseFactory<%1$sEntity> factoryMethod", contextName)) .setBody("super(totalComponents, startCreationIndex, contextInfo, factoryMethod);"); contextClass.addImport("ilargia.entitas.api.*"); for (ComponentInfo info : infos) { if (info.isSingleEntity) { addContextMethods(contextName, info, contextClass); } } return contextClass; }
Example #29
Source File: ComponentEntityGenerator.java From Entitas-Java with MIT License | 5 votes |
private CodeGenFile<JavaClassSource> getCodeGenFile(String contextName, ComponentData data) { if (entities.containsKey(contextName)) { return entities.get(contextName); } else { JavaClassSource sourceGen = Roaster.parse(JavaClassSource.class, String.format("public class %1$sEntity extends Entity {}", contextName)); CodeGenFile<JavaClassSource> genFile = new CodeGenFile<JavaClassSource>(contextName + "Entity", sourceGen, data.getSubDir()); entities.put(contextName, genFile); return genFile; } }
Example #30
Source File: InspectionResultProcessorTest.java From angularjs-addon with Eclipse Public License 1.0 | 5 votes |
@Test public void testInspectDoubleNumberField() throws Exception { String entityName = "Customer"; String fieldName = "age"; generateSimpleEntity(entityName); generateNumericField(fieldName, Double.class); JavaClassSource klass = getJavaClassFor(entityName); List<Map<String, String>> inspectionResult = metawidgetInspectorFacade.inspect(klass); inspectionResult = angularResultEnhancer.enhanceResults(klass, inspectionResult); assertThat(inspectionResult, hasItemWithEntry("name", fieldName)); assertThat(inspectionResult, hasItemWithEntry("type", "number")); }