Java Code Examples for org.jboss.forge.addon.parser.java.facets.JavaSourceFacet#saveJavaSource()

The following examples show how to use org.jboss.forge.addon.parser.java.facets.JavaSourceFacet#saveJavaSource() . 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: ProjectHelper.java    From angularjs-addon with Eclipse Public License 1.0 6 votes vote down vote up
public void addSizeConstraint(Project project, JavaClassSource klass, String propertyName, boolean onAccessor,
         String message,
         String min, String max) throws FileNotFoundException
{
   PropertySource<JavaClassSource> property = klass.getProperty(propertyName);
   final AnnotationSource<JavaClassSource> constraintAnnotation = addConstraintOnProperty(property, onAccessor,
            Size.class,
            message);

   if (min != null)
   {
      constraintAnnotation.setLiteralValue("min", min);
   }

   if (max != null)
   {
      constraintAnnotation.setLiteralValue("max", max);
   }

   JavaSourceFacet javaSourceFacet = project.getFacet(JavaSourceFacet.class);
   javaSourceFacet.saveJavaSource(constraintAnnotation.getOrigin());
}
 
Example 2
Source File: SetupFractionsStep.java    From thorntail-addon with Eclipse Public License 1.0 5 votes vote down vote up
@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 3
Source File: ProjectHelper.java    From angularjs-addon with Eclipse Public License 1.0 5 votes vote down vote up
public void addNotNullConstraint(Project project, JavaClassSource klass, String propertyName, boolean onAccessor,
         String message)
         throws FileNotFoundException
{
   PropertySource<JavaClassSource> property = klass.getProperty(propertyName);
   final Annotation<JavaClassSource> constraintAnnotation = addConstraintOnProperty(property, onAccessor,
            NotNull.class,
            message);

   JavaSourceFacet javaSourceFacet = project.getFacet(JavaSourceFacet.class);
   javaSourceFacet.saveJavaSource(constraintAnnotation.getOrigin());
}
 
Example 4
Source File: ProjectHelper.java    From angularjs-addon with Eclipse Public License 1.0 5 votes vote down vote up
public void addMinConstraint(Project project, JavaClassSource klass, String propertyName, boolean onAccessor,
         String message, String min)
         throws FileNotFoundException
{
   PropertySource<JavaClassSource> property = klass.getProperty(propertyName);
   final AnnotationSource<JavaClassSource> constraintAnnotation = addConstraintOnProperty(property, onAccessor,
            Min.class,
            message);
   constraintAnnotation.setLiteralValue(min);

   JavaSourceFacet javaSourceFacet = project.getFacet(JavaSourceFacet.class);
   javaSourceFacet.saveJavaSource(constraintAnnotation.getOrigin());
}
 
Example 5
Source File: ProjectHelper.java    From angularjs-addon with Eclipse Public License 1.0 5 votes vote down vote up
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 6
Source File: CamelNewRouteBuilderCommand.java    From fabric8-forge with Apache License 2.0 4 votes vote down vote up
@Override
public Result execute(UIExecutionContext context) throws Exception {
    Project project = getSelectedProject(context);
    JavaSourceFacet facet = project.getFacet(JavaSourceFacet.class);

    // does the project already have camel?
    Dependency core = findCamelCoreDependency(project);
    if (core == null) {
        return Results.fail("The project does not include camel-core");
    }

    // do we already have a class with the name
    String fqn = targetPackage.getValue() != null ? targetPackage.getValue() + "." + name.getValue() : name.getValue();

    JavaResource existing = facet.getJavaResource(fqn);
    if (existing != null && existing.exists()) {
        return Results.fail("A class with name " + fqn + " already exists");
    }

    // need to parse to be able to extends another class
    final JavaClassSource javaClass = Roaster.create(JavaClassSource.class);
    javaClass.setName(name.getValue());
    if (targetPackage.getValue() != null) {
        javaClass.setPackage(targetPackage.getValue());
    }
    javaClass.setSuperType("RouteBuilder");
    javaClass.addImport("org.apache.camel.builder.RouteBuilder");

    boolean cdi = CamelCommandsHelper.isCdiProject(getSelectedProject(context));
    boolean spring = CamelCommandsHelper.isSpringProject(getSelectedProject(context));

    if (cdi) {
        javaClass.addAnnotation("javax.inject.Singleton");
    } else if (spring) {
        javaClass.addAnnotation("org.springframework.stereotype.Component");
    }

    javaClass.addMethod()
            .setPublic()
            .setReturnTypeVoid()
            .setName("configure")
            .addThrows(Exception.class).setBody("");

    JavaResource javaResource = facet.saveJavaSource(javaClass);

    // if we are in an GUI editor then open the file
    if (isRunningInGui(context.getUIContext())) {
        context.getUIContext().setSelection(javaResource);
    }

    return Results.success("Created new RouteBuilder class " + name.getValue());
}
 
Example 7
Source File: InspectionResultProcessorTest.java    From angularjs-addon with Eclipse Public License 1.0 4 votes vote down vote up
private void saveJavaSource() throws FileNotFoundException
{
   JavaSourceFacet java = project.getFacet(JavaSourceFacet.class);
   java.saveJavaSource(entityClass);
}