org.jboss.forge.roaster.model.source.MethodSource Java Examples
The following examples show how to use
org.jboss.forge.roaster.model.source.MethodSource.
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 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 #2
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 #3
Source File: ComponentContextGenerator.java From Entitas-Java with MIT License | 6 votes |
private void addContextReplaceMethods(String contextName, ComponentData data, JavaClassSource source) { if (!isUnique(data)) { List<MethodSource<JavaClassSource>> constructors = getConstructorData(data); source.addMethod() .setName(String.format("replace%1$s", getTypeName(data))) .setReturnType(contextName + "Entity") .setPublic() .setParameters(constructors != null && constructors.size() > 0 ? memberNamesWithTypeFromConstructor(constructors.get(0)) : memberNamesWithType(getMemberData(data))) .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;" , getTypeName(data), constructors != null && constructors.size() > 0 ? memberNamesFromConstructor(constructors.get(0)) : memberNames(getMemberData(data)) , contextName)); } }
Example #4
Source File: CamelJavaParserHelper.java From fabric8-forge with Apache License 2.0 | 6 votes |
public static List<ParserResult> parseCamelSimpleExpressions(MethodSource<JavaClassSource> method) { List<ParserResult> answer = new ArrayList<ParserResult>(); MethodDeclaration md = (MethodDeclaration) method.getInternal(); Block block = md.getBody(); if (block != null) { for (Object statement : block.statements()) { // must be a method call expression if (statement instanceof ExpressionStatement) { ExpressionStatement es = (ExpressionStatement) statement; Expression exp = es.getExpression(); List<ParserResult> expressions = new ArrayList<ParserResult>(); parseExpression(null, method.getOrigin(), block, exp, expressions); if (!expressions.isEmpty()) { // reverse the order as we will grab them from last->first Collections.reverse(expressions); answer.addAll(expressions); } } } } return answer; }
Example #5
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 #6
Source File: ComponentEntityGenerator.java From Entitas-Java with MIT License | 5 votes |
private void addGetMethods(String contextName, ComponentData data, JavaClassSource codeGenerated) { codeGenerated.addImport(getFullTypeName(data)); if (isUnique(data)) { codeGenerated.addField() .setName(getTypeNameSuffix(data)) .setType(getTypeName(data)) .setLiteralInitializer(String.format("new %1$s();", getTypeName(data))) .setPublic(); } else { MethodSource<JavaClassSource> methodGET = codeGenerated.addMethod() .setName(String.format("get%1$s", getTypeName(data))) .setReturnType(getTypeName(data)) .setPublic(); String typeName = getTypeName(data); List<TypeVariableSource<JavaClassSource>> generics = getGenericsData(data); if (generics != null && generics.size() > 0) { typeName += "<"; for (TypeVariableSource<JavaClassSource> generic : generics) { String javaType[] = new String[generic.getBounds().size()]; for (int i = 0; i < generic.getBounds().size(); i++) { javaType[i] = (String) generic.getBounds().get(i).getSimpleName(); } methodGET.addTypeVariable().setName(generic.getName()).setBounds(javaType); if (typeName.indexOf("<") != typeName.length() - 1) typeName += ","; typeName += generic.getName(); } typeName += ">"; } methodGET.setBody(String.format("return (%1$s)getComponent(%2$s.%3$s);" , typeName, (isSharedContext(data) ? "Shared" : contextName) + DEFAULT_COMPONENT_LOOKUP_TAG, getTypeName(data))); } }
Example #7
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 #8
Source File: RoasterSimpleRouteBuilderConfigureTest.java From fabric8-forge with Apache License 2.0 | 5 votes |
@Test public void parse() throws Exception { JavaClassSource clazz = (JavaClassSource) Roaster.parse(new File("src/test/java/io/fabric8/forge/camel/java/MySimpleRouteBuilder.java")); MethodSource<JavaClassSource> method = clazz.getMethod("configure"); List<ParserResult> list = CamelJavaParserHelper.parseCamelSimpleExpressions(method); for (ParserResult simple : list) { System.out.println("Simple: " + simple.getElement()); System.out.println(" Line: " + findLineNumber(simple.getPosition())); } Assert.assertEquals("${body} > 100", list.get(0).getElement()); Assert.assertEquals(26, findLineNumber(list.get(0).getPosition())); Assert.assertEquals("${body} > 200", list.get(1).getElement()); Assert.assertEquals(29, findLineNumber(list.get(1).getPosition())); }
Example #9
Source File: CamelCommandsHelper.java From fabric8-forge with Apache License 2.0 | 5 votes |
public static void createSpringComponentFactoryClass(JavaClassSource javaClass, CamelComponentDetails details, String camelComponentName, String componentInstanceName, String configurationCode, Set<String> extraJavaImports) { javaClass.addAnnotation("Component"); javaClass.addImport("org.springframework.beans.factory.config.BeanDefinition"); javaClass.addImport("org.springframework.beans.factory.annotation.Qualifier"); javaClass.addImport("org.springframework.context.annotation.Bean"); javaClass.addImport("org.springframework.context.annotation.Scope"); javaClass.addImport("org.springframework.stereotype.Component"); javaClass.addImport(details.getComponentClassQName()); if (extraJavaImports != null) { for (String extra : extraJavaImports) { javaClass.addImport(extra); } } String componentClassName = details.getComponentClassName(); String methodName = "create" + Strings.capitalize(componentInstanceName) + "Component"; String body = componentClassName + " component = new " + componentClassName + "();" + configurationCode + "\nreturn component;"; MethodSource<JavaClassSource> method = javaClass.addMethod() .setPublic() .setReturnType(componentClassName) .setName(methodName) .setBody(body); method.addAnnotation("Qualifier").setStringValue(camelComponentName); method.addAnnotation("Bean"); method.addAnnotation("Scope").setLiteralValue("BeanDefinition.SCOPE_SINGLETON"); }
Example #10
Source File: CamelCommandsHelper.java From fabric8-forge with Apache License 2.0 | 5 votes |
public static void createCdiComponentProducerClass(JavaClassSource javaClass, CamelComponentDetails details, String camelComponentName, String componentInstanceName, String configurationCode, Set<String> extraJavaImports) { javaClass.addImport("javax.enterprise.inject.Produces"); javaClass.addImport("javax.inject.Singleton"); javaClass.addImport("javax.inject.Named"); javaClass.addImport(details.getComponentClassQName()); if (extraJavaImports != null) { for (String extra : extraJavaImports) { javaClass.addImport(extra); } } String componentClassName = details.getComponentClassName(); String methodName = "create" + Strings.capitalize(componentInstanceName) + "Component"; String body = componentClassName + " component = new " + componentClassName + "();" + configurationCode + "\nreturn component;"; MethodSource<JavaClassSource> method = javaClass.addMethod() .setPublic() .setReturnType(componentClassName) .setName(methodName) .setBody(body); method.addAnnotation("Named").setStringValue(camelComponentName); method.addAnnotation("Produces"); method.addAnnotation("Singleton"); }
Example #11
Source File: ConstructorDataProvider.java From Entitas-Java with MIT License | 5 votes |
@Override public void provide(ComponentData data) { List<MethodSource<JavaClassSource>> contructores = data.getSource().getMethods().stream() .filter(method -> method.isPublic()) .filter(method -> method.isConstructor()) .collect(Collectors.toList()); setConstructorData(data, contructores); }
Example #12
Source File: CreateTestClassCommand.java From thorntail-addon with Eclipse Public License 1.0 | 5 votes |
private void addTestMethod(JavaClassSource test) { test.addImport("org.junit.Test"); MethodSource<JavaClassSource> testMethod = test.addMethod() .setPublic() .setReturnTypeVoid() .setName("should_start_service") .setBody(""); testMethod.addAnnotation("Test"); }
Example #13
Source File: SetupFractionsStep.java From thorntail-addon with Eclipse Public License 1.0 | 5 votes |
@Override public Result execute(UIExecutionContext context) { Project project = getSelectedProject(context); ThorntailFacet thorntail = project.getFacet(ThorntailFacet.class); List<FractionDescriptor> installedFractions = thorntail.getInstalledFractions(); if (enableJAXRS(installedFractions)) { JavaSourceFacet facet = project.getFacet(JavaSourceFacet.class); JavaClassSource restEndpoint = Roaster.create(JavaClassSource.class) .setPackage(facet.getBasePackage() + ".rest") .setName("HelloWorldEndpoint"); if (hasCDI(installedFractions)) { restEndpoint.addAnnotation(ApplicationScoped.class); } restEndpoint.addAnnotation(Path.class).setStringValue("/hello"); MethodSource<JavaClassSource> method = restEndpoint.addMethod().setPublic().setReturnType(Response.class) .setName("doGet") .setBody("return Response.ok(\"Hello from Thorntail!\").build();"); method.addAnnotation(GET.class); method.addAnnotation(javax.ws.rs.Produces.class).setStringArrayValue(new String[] { MediaType.TEXT_PLAIN }); facet.saveJavaSource(restEndpoint); } if (hasTopologyJgroups(installedFractions)) { ThorntailConfiguration config = thorntail.getConfiguration(); Map<String, String> props = new TreeMap<>(config.getProperties()); props.put("swarm.bind.address", "127.0.0.1"); props.put("java.net.preferIPv4Stack", "true"); props.put("jboss.node.name", "${project.artifactId}"); thorntail.setConfiguration(ThorntailConfigurationBuilder.create(config).properties(props)); } return Results.success(); }
Example #14
Source File: CamelJavaParserHelper.java From fabric8-forge with Apache License 2.0 | 5 votes |
private static List<ParserResult> doParseCamelUris(MethodSource<JavaClassSource> method, boolean consumers, boolean producers, boolean strings, boolean fields) { List<ParserResult> answer = new ArrayList<ParserResult>(); if (method != null) { MethodDeclaration md = (MethodDeclaration) method.getInternal(); Block block = md.getBody(); if (block != null) { for (Object statement : md.getBody().statements()) { // must be a method call expression if (statement instanceof ExpressionStatement) { ExpressionStatement es = (ExpressionStatement) statement; Expression exp = es.getExpression(); List<ParserResult> uris = new ArrayList<ParserResult>(); parseExpression(method.getOrigin(), block, exp, uris, consumers, producers, strings, fields); if (!uris.isEmpty()) { // reverse the order as we will grab them from last->first Collections.reverse(uris); answer.addAll(uris); } } } } } return answer; }
Example #15
Source File: CamelJavaParserHelper.java From fabric8-forge with Apache License 2.0 | 5 votes |
private static MethodSource<JavaClassSource> findCreateRouteBuilderMethod(JavaClassSource clazz) { MethodSource method = clazz.getMethod("createRouteBuilder"); if (method != null && (method.isPublic() || method.isProtected()) && method.getParameters().isEmpty()) { return method; } return null; }
Example #16
Source File: CreateTestClassCommandTest.java From thorntail-addon with Eclipse Public License 1.0 | 4 votes |
@Test public void should_set_archivetype_test() throws Exception { assertThat(project.hasFacet(ThorntailFacet.class), is(true)); try (CommandController controller = uiTestHarness.createCommandController(CreateTestClassCommand.class, project.getRoot())) { controller.initialize(); controller.setValueFor("targetPackage", "org.example"); controller.setValueFor("named", "HelloWorldTest"); controller.setValueFor("archiveType", "WAR"); assertThat(controller.isValid(), is(true)); final AtomicBoolean flag = new AtomicBoolean(); controller.getContext().addCommandExecutionListener(new AbstractCommandExecutionListener() { @Override public void postCommandExecuted(UICommand command, UIExecutionContext context, Result result) { if (result.getMessage().equals("Test Class org.example.HelloWorldTest was created")) { flag.set(true); } } }); controller.execute(); assertThat(flag.get(), is(true)); } JavaResource javaResource = project.getFacet(JavaSourceFacet.class) .getTestJavaResource("org.example.HelloWorldTest"); assertThat(javaResource.exists(), is(true)); JavaClassSource testClass = Roaster.parse(JavaClassSource.class, javaResource.getContents()); assertThat(testClass.getAnnotation(RunWith.class), is((notNullValue()))); final AnnotationSource<JavaClassSource> defaultDeployment = testClass.getAnnotation("DefaultDeployment"); assertThat(defaultDeployment, is((notNullValue()))); final String testable = defaultDeployment.getLiteralValue("type"); assertThat(testable, is("DefaultDeployment.Type.WAR")); final MethodSource<JavaClassSource> testMethod = testClass.getMethod("should_start_service"); assertThat(testMethod, is(notNullValue())); assertThat(testMethod.getAnnotation(Test.class), is(notNullValue())); }
Example #17
Source File: CreateTestClassCommandTest.java From thorntail-addon with Eclipse Public License 1.0 | 4 votes |
@Test public void should_create_asclient_test() throws Exception { assertThat(project.hasFacet(ThorntailFacet.class), is(true)); try (CommandController controller = uiTestHarness.createCommandController(CreateTestClassCommand.class, project.getRoot())) { controller.initialize(); controller.setValueFor("targetPackage", "org.example"); controller.setValueFor("named", "HelloWorldTest"); controller.setValueFor("asClient", true); assertThat(controller.isValid(), is(true)); final AtomicBoolean flag = new AtomicBoolean(); controller.getContext().addCommandExecutionListener(new AbstractCommandExecutionListener() { @Override public void postCommandExecuted(UICommand command, UIExecutionContext context, Result result) { if (result.getMessage().equals("Test Class org.example.HelloWorldTest was created")) { flag.set(true); } } }); controller.execute(); assertThat(flag.get(), is(true)); } JavaResource javaResource = project.getFacet(JavaSourceFacet.class) .getTestJavaResource("org.example.HelloWorldTest"); assertThat(javaResource.exists(), is(true)); JavaClassSource testClass = Roaster.parse(JavaClassSource.class, javaResource.getContents()); assertThat(testClass.getAnnotation(RunWith.class), is((notNullValue()))); final AnnotationSource<JavaClassSource> defaultDeployment = testClass.getAnnotation("DefaultDeployment"); assertThat(defaultDeployment, is((notNullValue()))); final String testable = defaultDeployment.getLiteralValue("testable"); assertThat(testable, is("false")); final MethodSource<JavaClassSource> testMethod = testClass.getMethod("should_start_service"); assertThat(testMethod, is(notNullValue())); assertThat(testMethod.getAnnotation(Test.class), is(notNullValue())); }
Example #18
Source File: CreateTestClassCommandTest.java From thorntail-addon with Eclipse Public License 1.0 | 4 votes |
@Test public void should_create_incontainer_test() throws Exception { assertThat(project.hasFacet(ThorntailFacet.class), is(true)); try (CommandController controller = uiTestHarness.createCommandController(CreateTestClassCommand.class, project.getRoot())) { controller.initialize(); controller.setValueFor("targetPackage", "org.example"); controller.setValueFor("named", "HelloWorldTest"); assertThat(controller.isValid(), is(true)); final AtomicBoolean flag = new AtomicBoolean(); controller.getContext().addCommandExecutionListener(new AbstractCommandExecutionListener() { @Override public void postCommandExecuted(UICommand command, UIExecutionContext context, Result result) { if (result.getMessage().equals("Test Class org.example.HelloWorldTest was created")) { flag.set(true); } } }); controller.execute(); assertThat(flag.get(), is(true)); } JavaResource javaResource = project.getFacet(JavaSourceFacet.class) .getTestJavaResource("org.example.HelloWorldTest"); assertThat(javaResource.exists(), is(true)); JavaClassSource testClass = Roaster.parse(JavaClassSource.class, javaResource.getContents()); assertThat(testClass.getAnnotation(RunWith.class), is((notNullValue()))); final AnnotationSource<JavaClassSource> defaultDeployment = testClass.getAnnotation("DefaultDeployment"); assertThat(defaultDeployment, is((notNullValue()))); assertThat(defaultDeployment.getValues().size(), is(0)); final MethodSource<JavaClassSource> testMethod = testClass.getMethod("should_start_service"); assertThat(testMethod, is(notNullValue())); assertThat(testMethod.getAnnotation(Test.class), is(notNullValue())); }
Example #19
Source File: EntityIndexDataProvider.java From Entitas-Java with MIT License | 4 votes |
public static void setCustomMethods(ComponentData data, List<MethodSource<JavaClassSource>> methods) { data.put(ENTITY_INDEX_CUSTOM_METHODS, methods); }
Example #20
Source File: ContextGenerator.java From Entitas-Java with MIT License | 4 votes |
public String memberNamesFromConstructor(MethodSource<JavaClassSource> constructor) { return constructor.getParameters().stream() .map(info -> info.getName()) .collect(Collectors.joining(", ")); }
Example #21
Source File: ConstructorDataProvider.java From Entitas-Java with MIT License | 4 votes |
public static void setConstructorData(ComponentData data, List<MethodSource<JavaClassSource>> contructores) { data.put(CONSTRUCTOR_INFOS, contructores); }
Example #22
Source File: ConstructorDataProvider.java From Entitas-Java with MIT License | 4 votes |
public static List<MethodSource<JavaClassSource>> getConstructorData(ComponentData data) { return (List<MethodSource<JavaClassSource>>) data.get(CONSTRUCTOR_INFOS); }
Example #23
Source File: ContextGenerator.java From Entitas-Java with MIT License | 4 votes |
public String memberNamesWithTypeFromConstructor(MethodSource<JavaClassSource> constructor) { return constructor.getParameters().stream() .map(info -> info.toString()) .collect(Collectors.joining(", ")); }
Example #24
Source File: ComponentContextGenerator.java From Entitas-Java with MIT License | 4 votes |
public String memberNamesFromConstructor(MethodSource<JavaClassSource> constructor) { return constructor.getParameters().stream() .map(data -> data.getName()) .collect(Collectors.joining(", ")); }
Example #25
Source File: AnonymousMethodSource.java From fabric8-forge with Apache License 2.0 | 4 votes |
@Override public MethodSource<JavaClassSource> setReturnType(Type<?> arg0) { return null; }
Example #26
Source File: AnonymousMethodSource.java From fabric8-forge with Apache License 2.0 | 4 votes |
@Override public MethodSource<JavaClassSource> setProtected() { return null; }
Example #27
Source File: ContextGenerator.java From Entitas-Java with MIT License | 4 votes |
public String memberNamesWithTypeFromConstructor(MethodSource<JavaClassSource> constructor) { return constructor.getParameters().stream() .map(info -> info.toString()) .collect(Collectors.joining(", ")); }
Example #28
Source File: ContextGenerator.java From Entitas-Java with MIT License | 4 votes |
public String memberNamesFromConstructor(MethodSource<JavaClassSource> constructor) { return constructor.getParameters().stream() .map(info -> info.getName()) .collect(Collectors.joining(", ")); }
Example #29
Source File: AnonymousMethodSource.java From fabric8-forge with Apache License 2.0 | 4 votes |
@Override public MethodSource<JavaClassSource> setPrivate() { return null; }
Example #30
Source File: AnonymousMethodSource.java From fabric8-forge with Apache License 2.0 | 4 votes |
@Override public MethodSource<JavaClassSource> setVisibility(Visibility visibility) { return null; }