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

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


   @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


         Assert.assertFalse(wizard.isValid());
         wizard.setValueFor("regexp", "[0-9]");
         Assert.assertTrue(wizard.isValid());
      }
      wizard.execute();
      JavaClassSource javaClass = jpaEntity.getJavaType();
      FieldSource<JavaClassSource> field = javaClass.getField("id");
      AnnotationSource<JavaClassSource> pattern = field.getAnnotation(Pattern.class);
      Assert.assertNotNull(pattern);
      Assert.assertEquals("[0-9]", pattern.getStringValue("regexp"));

   }
View Full Code Here

         Assert.assertFalse(wizard.isValid());
         wizard.setValueFor("regexp", "^\\d{9}[\\d|X]$");
         Assert.assertTrue(wizard.isValid());
      }
      wizard.execute();
      JavaClassSource javaClass = jpaEntity.getJavaType();
      FieldSource<JavaClassSource> field = javaClass.getField("id");
      AnnotationSource<JavaClassSource> pattern = field.getAnnotation(Pattern.class);
      Assert.assertNotNull(pattern);
      Assert.assertEquals("^\\d{9}[\\d|X]$", pattern.getStringValue("regexp"));

   }
View Full Code Here

   private ResourceFactory factory;

   @Test
   public void testJavaResourceCreation() throws Exception
   {
      JavaClassSource javaClass = Roaster.create(JavaClassSource.class).setPackage("org.jboss.forge.test")
               .setName("Example");
      JavaResource resource = factory.create(JavaResource.class, File.createTempFile("forge", ".java"));
      resource.createNewFile();
      resource.setContents(javaClass);
View Full Code Here

   }

   @Test
   public void testJavaResourceCreationSpecialized() throws Exception
   {
      JavaClassSource javaClass = Roaster.create(JavaClassSource.class).setPackage("org.jboss.forge.test")
               .setName("Example");
      JavaResource resource = factory.create(JavaResource.class, File.createTempFile("forge", ".java"));
      resource.createNewFile();
      resource.setContents(javaClass);
View Full Code Here

   @Test
   public void testUpdateExistingField() throws Exception
   {
      JavaResource entity = projectHelper.createJPAEntity(project, "Customer");
      JavaClassSource javaSource = entity.getJavaType();
      beanOperations.addFieldTo(javaSource, "String", "firstName");
      entity.setContents(javaSource.toString());
      try (WizardCommandController controller = uiTestHarness.createWizardController(NewFieldWizard.class,
               project.getRoot()))
      {
         controller.initialize();
         Assert.assertTrue(controller.isEnabled());
View Full Code Here

               .saveJavaSource(Roaster.parse(JavaClassSource.class, "package org.example; public class DemoBean {}"));

      MethodSource<?> method = operations.addValidatorMethod(resource, "validateUsername");
      Assert.assertEquals(3, method.getParameters().size());

      JavaClassSource source = resource.getJavaType();
      Assert.assertEquals(1, source.getMethods().size());
      Assert.assertEquals(method.toSignature(), source.getMethods().get(0).toSignature());
   }
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 = ResourceGeneratorUtil.getContentType(context.getContentType());
      String idType = JPAEntityUtil.resolveIdType(entity);
      String persistenceUnitName = context.getPersistenceUnitName();
      String idGetterName = JPAEntityUtil.resolveIdGetterName(entity);
      String entityTable = JPAEntityUtil.getEntityTable(entity);
      String selectExpression = JPAEntityUtil.getSelectExpression(entity, entityTable);
      String idClause = JPAEntityUtil.getIdClause(entity, entityTable);
      String orderClause = JPAEntityUtil.getOrderClause(entity,
               JPAEntityUtil.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();
      if (!entity.hasAnnotation(XmlRootElement.class))
      {
         entity.addAnnotation(XmlRootElement.class);
         project.getFacet(JavaSourceFacet.class).saveJavaSource(entity);
      }
      String contentType = ResourceGeneratorUtil.getContentType(context.getContentType());
      String idType = JPAEntityUtil.resolveIdType(entity);
      String persistenceUnitName = context.getPersistenceUnitName();
      String idGetterName = JPAEntityUtil.resolveIdGetterName(entity);
      String entityTable = JPAEntityUtil.getEntityTable(entity);
      String selectExpression = JPAEntityUtil.getSelectExpression(entity, entityTable);
      String idClause = JPAEntityUtil.getIdClause(entity, entityTable);
      String orderClause = JPAEntityUtil.getOrderClause(entity,
               JPAEntityUtil.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

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.