Package org.jboss.forge.roaster.model.source

Examples of org.jboss.forge.roaster.model.source.JavaClassSource


      try
      {
         JavaResource javaResource = targetEntity.getValue();
         if (javaResource != null)
         {
            JavaClassSource javaClass = javaResource.getJavaType();
            if (javaClass.hasField(named.getValue()))
            {
               validator.addValidationWarning(targetEntity, "Field '" + named.getValue() + "' already exists");
            }
         }
      }
View Full Code Here


   @Override
   public List<JavaClassSource> generateFrom(RestGenerationContext context) throws Exception
   {
      List<JavaClassSource> result = new ArrayList<>();
      JavaClassSource entity = context.getEntity();

      Project project = context.getProject();
      String contentType = context.getContentType();
      String idType = ResourceGeneratorUtil.resolveIdType(entity);
      String persistenceUnitName = context.getPersistenceUnitName();
      String idGetterName = ResourceGeneratorUtil.resolveIdGetterName(entity);
      String entityTable = ResourceGeneratorUtil.getEntityTable(entity);
      String selectExpression = ResourceGeneratorUtil.getSelectExpression(entity, entityTable);
      String idClause = ResourceGeneratorUtil.getIdClause(entity, entityTable);
      String orderClause = ResourceGeneratorUtil.getOrderClause(entity,
               ResourceGeneratorUtil.getJpqlEntityVariable(entityTable));
      String resourcePath = ResourceGeneratorUtil.getResourcePath(context);

      DTOCollection createdDtos = from(project, entity, context.getTargetPackageName() + ".dto");
      JavaClassSource rootDto = createdDtos.getDTOFor(entity, true);

      Map<Object, Object> map = new HashMap<>();
      map.put("entity", entity);
      map.put("dto", rootDto);
      map.put("idType", idType);
      map.put("getIdStatement", idGetterName);
      map.put("contentType", contentType);
      map.put("persistenceUnitName", persistenceUnitName);
      map.put("entityTable", entityTable);
      map.put("selectExpression", selectExpression);
      map.put("idClause", idClause);
      map.put("orderClause", orderClause);
      map.put("resourcePath", resourcePath);

      Resource<URL> templateResource = resourceFactory.create(getClass().getResource("EndpointWithDTO.jv"));
      Template processor = templateFactory.create(templateResource, FreemarkerTemplate.class);
      String output = processor.process(map);
      JavaClassSource resource = Roaster.parse(JavaClassSource.class, output);
      resource.addImport(rootDto.getQualifiedName());
      resource.addImport(entity.getQualifiedName());
      resource.setPackage(context.getTargetPackageName());
      result.add(resource);
      result.addAll(createdDtos.allResources());

      return result;
   }
View Full Code Here

               // qualifiedParameterizedType
               // + " due to missing source.");
               continue;
            }

            JavaClassSource nestedDTOClass = generatedDTOGraphForEntity(project, parameterizedClass, dtoPackage, false,
                     false, dtoCollection);
            // Then update the DTO for the collection field
            Property<?> nestedDtoId = parseIdPropertyForJPAEntity(parameterizedClass);
            dtoClassBuilder.updateForCollectionProperty(property, nestedDTOClass, type, nestedDtoId);
         }
         else if (hasAssociation)
         {
            if (!topLevel)
            {
               // Do not expand associations beyond the root
               continue;
            }

            // Create another DTO having the PK-field of the type of single-valued associations,
            // if it does not exist
            JavaClass<?> associatedClass = tryGetJavaClass(project, qualifiedPropertyType);
            if (associatedClass == null)
            {
               // ShellMessages.warn(writer, "Omitting creation of fields and DTO for type " + qualifiedPropertyType
               // + " due to missing source.");
               continue;
            }

            JavaClassSource nestedDTOClass = generatedDTOGraphForEntity(project, associatedClass, dtoPackage, false,
                     false,
                     dtoCollection);
            dtoClassBuilder.updateForReferencedProperty(property, nestedDTOClass);
         }
         else if (isEmbedded)
         {
            // Create another DTO for the @Embedded type, if it does not exist
            JavaClassSource dtoForEmbeddedType = generatedDTOGraphForEntity(project, propertyClass, dtoPackage, true,
                     true,
                     dtoCollection);
            dtoClassBuilder.updateForReferencedProperty(property, dtoForEmbeddedType);
         }
         else
         {
            dtoClassBuilder.updateForSimpleProperty(property, property.getType());
         }
      }

      JavaClassSource dtoClass = dtoClassBuilder.createDTO();
      if (topLevel)
      {
         dtoCollection.addRootDTO(entity, dtoClass);
      }
      else
View Full Code Here

   ResourceFactory resourceFactory;

   @Override
   public List<JavaClassSource> generateFrom(RestGenerationContext context) throws Exception
   {
      JavaClassSource entity = context.getEntity();
      Project project = context.getProject();
      String contentType = context.getContentType();
      if (!entity.hasAnnotation(XmlRootElement.class))
      {
         entity.addAnnotation(XmlRootElement.class);
         project.getFacet(JavaSourceFacet.class).saveJavaSource(entity);
      }
      String idType = ResourceGeneratorUtil.resolveIdType(entity);
      String persistenceUnitName = context.getPersistenceUnitName();
      String idGetterName = ResourceGeneratorUtil.resolveIdGetterName(entity);
      String entityTable = ResourceGeneratorUtil.getEntityTable(entity);
      String selectExpression = ResourceGeneratorUtil.getSelectExpression(entity, entityTable);
      String idClause = ResourceGeneratorUtil.getIdClause(entity, entityTable);
      String orderClause = ResourceGeneratorUtil.getOrderClause(entity,
               ResourceGeneratorUtil.getJpqlEntityVariable(entityTable));
      String resourcePath = ResourceGeneratorUtil.getResourcePath(context);

      Map<Object, Object> map = new HashMap<>();
      map.put("entity", entity);
      map.put("idType", idType);
      map.put("getIdStatement", idGetterName);
      map.put("contentType", contentType);
      map.put("persistenceUnitName", persistenceUnitName);
      map.put("entityTable", entityTable);
      map.put("selectExpression", selectExpression);
      map.put("idClause", idClause);
      map.put("orderClause", orderClause);
      map.put("resourcePath", resourcePath);

      Resource<URL> templateResource = resourceFactory.create(getClass().getResource("Endpoint.jv"));
      Template processor = templateFactory.create(templateResource, FreemarkerTemplate.class);
      String output = processor.process(map);
      JavaClassSource resource = Roaster.parse(JavaClassSource.class, output);
      resource.addImport(entity.getQualifiedName());
      resource.setPackage(context.getTargetPackageName());
      return Arrays.asList(resource);
   }
View Full Code Here

   }

   @Override
   public Result execute(UIExecutionContext context) throws Exception
   {
      JavaClassSource javaSource = targetClass.getValue().getJavaType();

      Result result;

      if (onProperty.hasValue())
      {
         String propertyName = onProperty.getValue().getUnderlyingResourceObject().getName();
         AnnotationTargetSource field = javaSource.getField(propertyName);

         addAnnotationToSource(field, annotation.getValue());

         result = Results.success("Annotation \"" + annotation.getValue() + "\" was successfully added to \""
                  + propertyName + "\" property declaration.");
      }
      else if (onMethod.hasValue())
      {
         List<Parameter> parameters = onMethod.getValue().getUnderlyingResourceObject().getParameters();

         String[] stringParametersArray = new String[parameters.size()];

         for (int i = 0; i < parameters.size(); i++)
         {
            stringParametersArray[i] = parameters.get(i).getType().getName();
         }

         String methodName = onMethod.getValue().getUnderlyingResourceObject().getName();
         AnnotationTargetSource method = javaSource.getMethod(methodName, stringParametersArray);

         addAnnotationToSource(method, annotation.getValue());

         result = Results.success("Annotation \"" + annotation.getValue() + "\" was successfully added to the \""
                  + methodName + "\" method declaration.");
View Full Code Here

         @Override
         public Iterable<String> call() throws Exception
         {
            List<String> strings = new ArrayList<>();
            JavaResource javaResource = targetClass.getValue();
            JavaClassSource targetClass = javaResource.getJavaType();
            List<PropertySource<JavaClassSource>> properties = targetClass.getProperties();
            for (PropertySource<JavaClassSource> property : properties)
            {
               strings.add(property.getName());
            }
            return strings;
         }
      });
     properties.setEnabled(new Callable<Boolean>()
      {
         @Override
         public Boolean call()
         {
            return (targetClass.getValue() != null);
         }
      });
      builder.add(targetClass).add(builderPattern).add(properties);
   }
View Full Code Here

   @Override
   public Result execute(UIExecutionContext context) throws Exception
   {
      JavaResource javaResource = targetClass.getValue();
      JavaClassSource targetClass = javaResource.getJavaType();
      GetSetMethodGenerator generator;
      if (builderPattern.getValue())
      {
         generator = new BuilderGetSetMethodGenerator();
      }
      else
      {
         generator = new DefaultGetSetMethodGenerator();
      }
      List<PropertySource<JavaClassSource>> selectedProperties = new ArrayList<>();
      if(properties == null || properties.getValue() == null) {
         return Results.fail("No properties were selected");
      }
      for(String selectedProperty : properties.getValue()) {
         selectedProperties.add(targetClass.getProperty(selectedProperty));
      }
     
      for (PropertySource<JavaClassSource> property : selectedProperties)
      {
         MethodSource<JavaClassSource> accessor = targetClass
                  .getMethod("get" + Strings.capitalize(property.getName()));
         if (accessor == null)
         {
            generator.createAccessor(property);
         }
         else
         {
            if (!generator.isCorrectAccessor(accessor, property))
            {
               if(promptToFixMethod(context,accessor.getName(),property.getName())) {
                  targetClass.removeMethod(accessor);
                  generator.createMutator(property);
               }
            }
         }
         String mutatorMethodName = "set" + Strings.capitalize(property.getName());
         String mutatorMethodParameter = property.getType().getName();
         MethodSource<JavaClassSource> mutator = targetClass.getMethod(mutatorMethodName, mutatorMethodParameter);
         if (mutator == null)
         {
            generator.createMutator(property);
         }
         else
         {
            if (!generator.isCorrectMutator(mutator, property))
            {
               if(promptToFixMethod(context,mutator.getName(),property.getName())) {
                  targetClass.removeMethod(mutator);
                  generator.createMutator(property);
               }
            }
         }
      }
View Full Code Here

   @Override
   public Result execute(UIExecutionContext context) throws Exception
   {
      JavaResource javaResource = targetClass.getValue();
      String fieldNameStr = named.getValue();
      JavaClassSource targetClass = javaResource.getJavaType();
      FieldSource<JavaClassSource> field = targetClass.getField(fieldNameStr);
      String action = (field == null) ? "created" : "updated";
      if (field != null)
      {
         UIPrompt prompt = context.getPrompt();
         if (prompt.promptBoolean("Field '" + field.getName() + "' already exists. Do you want to overwrite it?"))
View Full Code Here

   public JavaResource newEntity(Project project, String entityName, String entityPackage, GenerationType idStrategy,
            String tableName)
            throws FileNotFoundException
   {
      final JavaSourceFacet java = project.getFacet(JavaSourceFacet.class);
      JavaClassSource javaClass = createJavaEntityClass(entityName, entityPackage, idStrategy, tableName);
      return java.saveJavaSource(javaClass);
   }
View Full Code Here

   @Override
   public JavaResource newEntity(DirectoryResource target, String entityName, String entityPackage,
            GenerationType idStrategy, String tableName)
   {
      JavaClassSource javaClass = createJavaEntityClass(entityName, entityPackage, idStrategy, tableName);
      JavaResource javaResource = getJavaResource(target, javaClass.getName());
      javaResource.setContents(javaClass);
      return javaResource;
   }
View Full Code Here

TOP

Related Classes of org.jboss.forge.roaster.model.source.JavaClassSource

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.