Package org.jboss.forge.parser.java

Examples of org.jboss.forge.parser.java.JavaClass


   {
      JavaSourceFacet java = project.getFacet(JavaSourceFacet.class);

      try
      {
         JavaClass entityClass = getJavaClass();
         JavaClass fieldEntityClass;
         if (areTypesSame(fieldType, entityClass.getCanonicalName()))
         {
            fieldEntityClass = entityClass;
         }
         else
View Full Code Here


      JavaSourceFacet java = project.getFacet(JavaSourceFacet.class);

      try
      {
         JavaClass entity = getJavaClass();
         JavaClass otherEntity;
         if (areTypesSame(fieldType, entity.getCanonicalName()))
         {
            otherEntity = entity;
         }
         else
         {
            otherEntity = findEntity(fieldType);
            entity.addImport(otherEntity.getQualifiedName());
         }

         if (entity.hasField(fieldName))
         {
            throw new IllegalStateException("Entity [" + entity.getCanonicalName() + "] already has a field named ["
                     + fieldName + "]");
         }
         if (!Strings.isNullOrEmpty(inverseFieldName) && otherEntity.hasField(inverseFieldName))
         {
            throw new IllegalStateException("Entity [" + otherEntity.getCanonicalName()
                     + "] already has a field named ["
                     + inverseFieldName + "]");
         }

         entity.addImport(Set.class);
         entity.addImport(HashSet.class);
         Field<JavaClass> field = entity.addField("private Set<" + otherEntity.getName() + "> " + fieldName
                  + "= new HashSet<"
                  + otherEntity.getName() + ">();");
         Annotation<JavaClass> annotation = field.addAnnotation(ManyToMany.class);
         Refactory.createGetterAndSetter(entity, field);

         if (!Strings.isNullOrEmpty(inverseFieldName))
         {
            annotation.setStringValue("mappedBy", inverseFieldName);

            otherEntity.addImport(Set.class);
            otherEntity.addImport(HashSet.class);
            if (!otherEntity.getCanonicalName().equals(entity.getCanonicalName()))
            {
               otherEntity.addImport(entity.getQualifiedName());
            }
            Field<JavaClass> otherField = otherEntity.addField("private Set<" + entity.getName() + "> "
                     + inverseFieldName
                     + "= new HashSet<" + entity.getName() + ">();");
            otherField.addAnnotation(ManyToMany.class);
            Refactory.createGetterAndSetter(otherEntity, otherField);
View Full Code Here

   {
      JavaSourceFacet java = project.getFacet(JavaSourceFacet.class);

      try
      {
         JavaClass one = getJavaClass();
         JavaClass many;
         // Field type may end with .java
         if (areTypesSame(fieldType, one.getCanonicalName()))
         {
            many = one;
         }
         else
         {
            many = findEntity(fieldType);
            one.addImport(many.getQualifiedName());
         }

         if (one.hasField(fieldName))
         {
            throw new IllegalStateException("Entity [" + one.getCanonicalName() + "] already has a field named ["
                     + fieldName + "]");
         }
         if (!Strings.isNullOrEmpty(inverseFieldName) && many.hasField(inverseFieldName))
         {
            throw new IllegalStateException("Entity [" + many.getCanonicalName() + "] already has a field named ["
                     + inverseFieldName + "]");
         }

         one.addImport(Set.class);
         one.addImport(HashSet.class);

         Field<JavaClass> oneField = one.addField("private Set<" + many.getName() + "> " + fieldName + "= new HashSet<"
                  + many.getName() + ">();");
         Annotation<JavaClass> annotation = oneField.addAnnotation(OneToMany.class);
         Refactory.createGetterAndSetter(one, oneField);

         if (!Strings.isNullOrEmpty(inverseFieldName))
         {
            annotation.setStringValue("mappedBy", inverseFieldName);
            annotation.setLiteralValue("cascade", "CascadeType.ALL");
            annotation.getOrigin().addImport(CascadeType.class);
            annotation.setLiteralValue("orphanRemoval", "true");
            if (!many.getCanonicalName().equals(one.getCanonicalName()))
            {
               many.addImport(one);
            }
            Field<JavaClass> manyField = many.addField("private " + one.getName() + " " + inverseFieldName + ";");
            manyField.addAnnotation(ManyToOne.class);
            Refactory.createGetterAndSetter(many, manyField);
            java.saveJavaSource(many);
         }
        
View Full Code Here

   {
      JavaSourceFacet java = project.getFacet(JavaSourceFacet.class);

      try
      {
         JavaClass many = getJavaClass();
         JavaClass one;
         if (areTypesSame(fieldType, many.getCanonicalName()))
         {
            one = many;
         }
         else
         {
            one = findEntity(fieldType);
            many.addImport(one);
         }
         if (many.hasField(fieldName))
         {
            throw new IllegalStateException("Entity [" + many.getCanonicalName() + "] already has a field named ["
                     + fieldName + "]");
         }
         if (!Strings.isNullOrEmpty(inverseFieldName) && one.hasField(inverseFieldName))
         {
            throw new IllegalStateException("Entity [" + one.getCanonicalName() + "] already has a field named ["
                     + inverseFieldName + "]");
         }

         Field<JavaClass> manyField = many.addField("private " + one.getName() + " " + fieldName + ";");
         Annotation<JavaClass> manyAnnotation = manyField.addAnnotation(ManyToOne.class);
         Refactory.createGetterAndSetter(many, manyField);

         if (!Strings.isNullOrEmpty(inverseFieldName))
         {
            one.addImport(Set.class);
            one.addImport(HashSet.class);
            if (!one.getCanonicalName().equals(many.getCanonicalName()))
            {
               one.addImport(many.getQualifiedName());
            }
            Field<JavaClass> oneField = one.addField("private Set<" + many.getName() + "> " + inverseFieldName
                     + "= new HashSet<"
                     + many.getName() + ">();");
            Annotation<JavaClass> oneAnnotation = oneField.addAnnotation(OneToMany.class).setStringValue("mappedBy",
                     fieldName);
            oneAnnotation.setLiteralValue("cascade", "CascadeType.ALL");
View Full Code Here

      return (JavaClass) source;
   }

   private JavaClass findEntity(final String entity) throws FileNotFoundException
   {
      JavaClass result = null;

      PersistenceFacet scaffold = project.getFacet(PersistenceFacet.class);
      JavaSourceFacet java = project.getFacet(JavaSourceFacet.class);

      if (entity != null)
View Full Code Here

      }

      final JavaSourceFacet java = project.getFacet(JavaSourceFacet.class);
      for (JavaResource jr : javaTargets)
      {
         JavaClass entity = (JavaClass) (jr).getJavaSource();
         if (!entity.hasAnnotation(XmlRootElement.class))
            entity.addAnnotation(XmlRootElement.class);

         String idType = resolveIdType(entity);
         if (!Types.isBasicType(idType))
         {
            ShellMessages.error(out, "Skipped class [" + entity.getQualifiedName() + "] because @Id type [" + idType
                     + "] is not supported by endpoint generation.");
            continue;
         }
         String idSetterName = resolveIdSetterName(entity);
         String idGetterName = resolveIdGetterName(entity);

         CompiledTemplateResource template = compiler.compileResource(getClass().getResourceAsStream(
                  "/org/jboss/forge/rest/Endpoint.jv"));

         Map<Object, Object> map = new HashMap<Object, Object>();
         map.put("entity", entity);
         map.put("idType", idType);
         map.put("setIdStatement", idSetterName);
         map.put("getIdStatement", idGetterName);
         map.put("contentType", contentType);
         String entityTable = getEntityTable(entity);
         map.put("entityTable", entityTable);
         map.put("resourcePath", entityTable.toLowerCase() + "s");

         JavaClass resource = JavaParser.parse(JavaClass.class, template.render(map));
         resource.addImport(entity.getQualifiedName());
         resource.setPackage(java.getBasePackage() + ".rest");

         /*
          * Save the sources
          */
         java.saveJavaSource(entity);

         if (!java.getJavaResource(resource).exists()
                  || prompt.promptBoolean("Endpoint [" + resource.getQualifiedName() + "] already, exists. Overwrite?"))
         {
            java.saveJavaSource(resource);
            ShellMessages.success(out, "Generated REST endpoint for [" + entity.getQualifiedName() + "]");
         }
         else
View Full Code Here

         throw new IllegalArgumentException("The parameter 'annotationClass' cannot be null");
      }

      if (resource instanceof JavaResource)
      {
         final JavaClass javaClass = ResourceUtil.getJavaClassFromResource(resource);
         return javaClass.hasAnnotation(annotationClass);
      }
      else if (resource instanceof JavaMemberResource)
      {
         final JavaMemberResource<?> javaMemberResource = (JavaMemberResource<?>) resource;
         return javaMemberResource.getUnderlyingResourceObject().hasAnnotation(annotationClass);
View Full Code Here

      final Resource<?> currentResource = shell.getCurrentResource();

      try
      {

         final JavaClass javaClass = ResourceUtil.getJavaClassFromResource(currentResource);
         for (Field<JavaClass> oneField : javaClass.getFields())
         {
            tokens.add(oneField.getName());
         }

      }
View Full Code Here

   @Override
   public List<Resource<?>> getOptionalResources()
   {
      List<Resource<?>> result = new ArrayList<Resource<?>>();
      JavaClass entity = (JavaClass) field.getOrigin();
      String methodNameSuffix = Strings.capitalize(field.getName());
      // Condition to remove getField()
      if (entity.hasMethodSignature("get" + methodNameSuffix))
      {
         Method<JavaClass> method = entity.getMethod("get" + methodNameSuffix);
         result.add(new JavaMethodResource(this.getParent(), method));
      }
      // Condition to remove setField()
      if (entity.hasMethodSignature("set" + methodNameSuffix, field.getQualifiedType()))
      {
         Method<JavaClass> method = entity.getMethod("set" + methodNameSuffix, field.getQualifiedType());
         result.add(new JavaMethodResource(this.getParent(), method));
      }
      return result;
   }
View Full Code Here

      targetDir = selectTargetDir(provider, targetDir);
      verifyTemplate(provider, template);

      for (JavaResource jr : javaTargets)
      {
         JavaClass entity = (JavaClass) (jr).getJavaSource();
         List<Resource<?>> generatedResources = provider.generateFromEntity(targetDir, template, entity, overwrite);

         // TODO give plugins a chance to react to generated resources, use event bus?
         if (!generatedResources.isEmpty())
         {
            generatedEvent.fire(new ScaffoldGeneratedResources(provider, prepareResources(generatedResources)));
         }

         ShellMessages.success(writer, "Generated UI for [" + entity.getQualifiedName() + "]");
      }

   }
View Full Code Here

TOP

Related Classes of org.jboss.forge.parser.java.JavaClass

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.