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

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


   public void testIsFieldEnum() throws Exception
   {
      JavaProjectType javaProjectType = new JavaProjectType();
      Project project = projectFactory.createTempProject(javaProjectType.getRequiredFacets());
      JavaClassSource targetClass = Roaster.parse(JavaClassSource.class, "public class Test{}");
      JavaEnumSource testEnum = Roaster.create(JavaEnumSource.class).setName("TestEnum");

      String testPackage = "org.jboss.forge.testpkg";

      targetClass.setPackage(testPackage);
      testEnum.setPackage(testPackage);

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

      javaSourceFacet.saveJavaSource(targetClass);
      javaSourceFacet.saveJavaSource(testEnum);
View Full Code Here


   public void testCreateEnum() throws Exception
   {
      Project project = projectFactory.createTempProject();
      facetFactory.install(project, JavaSourceFacet.class);
      JavaSourceFacet facet = project.getFacet(JavaSourceFacet.class);
      JavaEnumSource javaEnum = Roaster.parse(JavaEnumSource.class,
               "package org.jboss.forge.test; public enum CreditCardType{}");
      facet.saveJavaSource(javaEnum);

      JavaResource javaResource = facet.getJavaResource("org.jboss.forge.test.CreditCardType");
      Assert.assertNotNull(javaResource);
      Assert.assertThat(javaResource.getJavaType(), is(instanceOf(JavaEnum.class)));

      CommandController controller = testHarness.createCommandController(JavaEnumConstantCommand.class,
               javaResource);
      controller.initialize();
      controller.setValueFor("arguments", Arrays.asList("VISA", "AMERICAN_EXPRESS", "MASTERCARD"));
      Assert.assertTrue(controller.isValid());
      Assert.assertTrue(controller.canExecute());
      Result result = controller.execute();
      Assert.assertThat(result, is(not(instanceOf(Failed.class))));

      javaResource = facet.getJavaResource("org.jboss.forge.test.CreditCardType");
      JavaEnumSource enumClass = javaResource.getJavaType();
      List<EnumConstantSource> enumConstants = enumClass.getEnumConstants();
      Assert.assertEquals(3, enumConstants.size());

      Assert.assertEquals("VISA", enumConstants.get(0).getName());
      Assert.assertEquals("AMERICAN_EXPRESS", enumConstants.get(1).getName());
      Assert.assertEquals("MASTERCARD", enumConstants.get(2).getName());
View Full Code Here

   @Override
   public Result execute(UIExecutionContext context) throws Exception
   {
      JavaResource resource = enumClass.getValue();
      JavaEnumSource source = resource.getJavaType();
      Project project = getSelectedProject(context.getUIContext());
      JavaSourceFacet facet = project.getFacet(JavaSourceFacet.class);
      for (String enumConstant : arguments.getValue())
      {
         source.addEnumConstant(enumConstant);
      }
      facet.saveJavaSource(source);
      return Results.success();
   }
View Full Code Here

   }

   @Test
   public void testEnumWithNestedClass()
   {
      JavaEnumSource javaEnum = Roaster
               .parse(JavaEnumSource.class, "package org.example; public enum OuterEnum { " +
                        "  FOO, BAR, BAZ; " +
                        "  public class InnerClass1{ " +
                        "    public class InnerClass3{}" +
                        "  } " +
                        "  public class InnerClass2{} " +
                        "}");

      Assert.assertEquals("org.example.OuterEnum", javaEnum.getCanonicalName());
      List<JavaSource<?>> nestedClasses = javaEnum.getNestedTypes();
      JavaClassSource inner1 = (JavaClassSource) nestedClasses.get(0);
      JavaClassSource inner2 = (JavaClassSource) nestedClasses.get(1);
      Assert.assertEquals(javaEnum, inner1.getEnclosingType());
      Assert.assertEquals("org.example.OuterEnum.InnerClass1", inner1.getCanonicalName());
      Assert.assertEquals("org.example.OuterEnum$InnerClass1", inner1.getQualifiedName());
View Full Code Here

                        "  public enum InnerEnum{A,B,C;} " +
                        "}");

      Assert.assertEquals("org.example.OuterClass", javaClass.getCanonicalName());
      List<JavaSource<?>> nestedClasses = javaClass.getNestedTypes();
      JavaEnumSource inner1 = (JavaEnumSource) nestedClasses.get(0);
      Assert.assertEquals(javaClass, inner1.getEnclosingType());
      Assert.assertEquals("org.example.OuterClass.InnerEnum", inner1.getCanonicalName());
      Assert.assertEquals("org.example.OuterClass$InnerEnum", inner1.getQualifiedName());
      Assert.assertEquals("InnerEnum", inner1.getName());
      Assert.assertEquals(1, nestedClasses.size());
   }
View Full Code Here

   public EnumConstantImpl(final JavaEnumSource parent, final String declaration)
   {
      init(parent);

      String stub = "public enum Stub { " + declaration + " }";
      JavaEnumSource temp = (JavaEnumSource) Roaster.parse(stub);
      List<EnumConstantSource> constants = temp.getEnumConstants();
      EnumConstantDeclaration newField = (EnumConstantDeclaration) constants.get(0).getInternal();
      EnumConstantDeclaration subtree = (EnumConstantDeclaration) ASTNode.copySubtree(ast, newField);
      this.enumConstant = subtree;
   }
View Full Code Here

   {
      enumConstant.arguments().clear();
      if (literalArguments != null && literalArguments.length > 0)
      {
         final String stub = "public enum Stub { FOO(" + Strings.join(Arrays.asList(literalArguments), ", ") + "); }";
         final JavaEnumSource temp = Roaster.parse(JavaEnumSource.class, stub);
         final List<EnumConstantSource> constants = temp.getEnumConstants();
         final EnumConstantDeclaration newConstant = (EnumConstantDeclaration) constants.get(0).getInternal();
         final List<Expression> arguments = newConstant.arguments();
         for (Expression argument : arguments)
         {
            final Expression subtree = (Expression) ASTNode.copySubtree(ast, argument);
View Full Code Here

      {
         AnonymousClassDeclaration result = enumConstantDeclaration.getAnonymousClassDeclaration();
         if (result == null)
         {
            final String stub = "enum StubEnum { FOO() {}; }";
            final JavaEnumSource temp = Roaster.parse(JavaEnumSource.class, stub);
            final AnonymousClassDeclaration body = ((EnumConstantBodyImpl) temp.getEnumConstants().get(0).getBody())
                     .getBody();
            final AST ast = ((ASTNode) javaEnum.getInternal()).getAST();
            result = (AnonymousClassDeclaration) ASTNode.copySubtree(ast, body);
            enumConstantDeclaration.setAnonymousClassDeclaration(result);
         }
View Full Code Here

   }

   public JavaResource createEmptyEnum(Project project, String enumName) throws IOException
   {
      JavaSourceFacet javaSourceFacet = project.getFacet(JavaSourceFacet.class);
      JavaEnumSource enumSource = Roaster.create(JavaEnumSource.class).setName(enumName);
      String packageName = project.getFacet(MetadataFacet.class).getTopLevelPackage() + ".model";
      enumSource.setPackage(packageName);
      return javaSourceFacet.saveJavaSource(enumSource);
   }
View Full Code Here

   @Override
   public Result execute(UIExecutionContext context) throws Exception
   {
      JavaResource resource = enumClass.getValue();
      JavaEnumSource source = resource.getJavaType();
      Project project = getSelectedProject(context.getUIContext());
      JavaSourceFacet facet = project.getFacet(JavaSourceFacet.class);
      for (String enumConstant : arguments.getValue())
      {
         source.addEnumConstant(enumConstant);
      }
      facet.saveJavaSource(source);
      return Results.success();
   }
View Full Code Here

TOP

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

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.