Package org.jboss.forge.parser.java

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


                     help = "the annotation definition: surround with quotes",
                     description = "annotation definition") final String... def) throws FileNotFoundException
   {
      JavaSourceFacet java = project.getFacet(JavaSourceFacet.class);

      JavaAnnotation type = null;
      if (def != null)
      {
         String classDef = Strings.join(Arrays.asList(def), " ");
         type = JavaParser.parse(JavaAnnotation.class, classDef);
      }
      else if (in != null)
      {
         type = JavaParser.parse(JavaAnnotation.class, in);
      }
      else if (annotationName != null)
      {
         type = JavaParser.create(JavaAnnotation.class).setName(annotationName);
      }
      else
      {
         throw new RuntimeException("arguments required");
      }

      if (pckg != null)
      {
         type.setPackage(pckg);
      }

      if (documented)
      {
         type.addAnnotation(Documented.class);
      }
      if (retentionPolicy != null)
      {
         type.addAnnotation(Retention.class).setEnumValue(retentionPolicy);
      }
      final Set<ElementType> targetTypes;
      if (noTarget)
      {
         targetTypes = Collections.emptySet();
      }
      else
      {
         targetTypes = shell.promptMultiSelectWithWildcard("*",
                  "Select target element types", ElementType.values());
      }
      if (targetTypes.isEmpty())
      {
         shell.printlnVerbose("Skipping @Target annotation");
      }
      else
      {
         type.addAnnotation(Target.class).setEnumValue(targetTypes.toArray(new ElementType[targetTypes.size()]));
      }

      if (!type.hasSyntaxErrors())
      {
         java.saveJavaSource(type);
      }
      else
      {
         writer.println(ShellColor.RED, "Syntax Errors:");
         for (SyntaxError error : type.getSyntaxErrors())
         {
            writer.println(error.toString());
         }
         writer.println();

View Full Code Here


      }

      JavaSource<?> source = resource.getJavaSource();
      if (source.isAnnotation())
      {
         JavaAnnotation parent = JavaAnnotation.class.cast(source);

         String addName;
         if (elementDef != null)
         {
            addName = JavaParser.parse(JavaAnnotation.class, "public @interface Temp{}")
                     .addAnnotationElement(elementDef).getName();
         }
         else
         {
            addName = name;
         }

         if (parent.hasAnnotationElement(addName))
         {
            throw new IllegalStateException("Element named [" + addName + "] already exists.");
         }

         if (elementDef != null)
         {
            parent.addAnnotationElement(elementDef);
         }
         else
         {
            parent.addAnnotationElement().setName(name).setType(type);
         }
         java.saveJavaSource(source);
      }
   }
View Full Code Here

      Result result = super.execute(context);
      if (!(result instanceof Failed))
      {
         JavaSourceFacet javaSourceFacet = getSelectedProject(context).getFacet(JavaSourceFacet.class);
         JavaResource javaResource = context.getUIContext().getSelection();
         JavaAnnotation constraint = (JavaAnnotation) javaResource.getJavaSource();
         // Constraint annotation header
         constraint.addAnnotation(Constraint.class).setStringValue("validatedBy = {}");
         constraint.addAnnotation(ReportAsSingleViolation.class);
         constraint.addAnnotation(Retention.class).setEnumValue(RUNTIME);
         constraint.addAnnotation(Target.class).setEnumValue(METHOD, FIELD, PARAMETER, TYPE, ANNOTATION_TYPE, CONSTRUCTOR);
         constraint.addAnnotation(Documented.class);
         // Constraint annotation body
         constraint.addAnnotationElement("String message() default \"Invalid value\"");
         constraint.addAnnotationElement("Class<?>[] groups() default { }");
         constraint.addAnnotationElement("Class<? extends Payload>[] payload() default { }");

         javaSourceFacet.saveJavaSource(constraint);
      }
      return result;
   }
View Full Code Here

      if (!declaration.trim().endsWith(";"))
      {
         declaration = declaration + ";";
      }
      String stub = "public @interface Stub { " + declaration + " }";
      JavaAnnotation temp = (JavaAnnotation) JavaParser.parse(stub);
      List<AnnotationElement> fields = temp.getAnnotationElements();
      AnnotationTypeMemberDeclaration newField = (AnnotationTypeMemberDeclaration) fields.get(0).getInternal();
      return (AnnotationTypeMemberDeclaration) ASTNode.copySubtree(((ASTNode) parent.getInternal()).getAST(), newField);
   }
View Full Code Here

   @Override
   public AnnotationElement setType(final String typeName)
   {
      String simpleName = Types.toSimpleName(typeName);

      JavaAnnotation origin = getOrigin();
      if (!Strings.areEqual(typeName, simpleName) && origin.requiresImport(typeName))
      {
         origin.addImport(typeName);
      }

      Code primitive = PrimitiveType.toCode(typeName);

      org.eclipse.jdt.core.dom.Type type = null;
      if (primitive != null)
      {
         type = ast.newPrimitiveType(primitive);
      }
      else
      {
         if (!origin.requiresImport(typeName))
         {
            if (Types.isArray(typeName))
            {
               String arrayType = Types.stripArray(typeName);
               int arrayDimension = Types.getArrayDimension(typeName);
View Full Code Here

            member.setDefault(null);
         }
         else
         {
            String stub = "public @interface Stub { String stub() default " + value + "; }";
            JavaAnnotation temp = (JavaAnnotation) JavaParser.parse(stub);
            AnnotationTypeMemberDeclaration internal = (AnnotationTypeMemberDeclaration) temp.getAnnotationElements()
                     .get(0).getInternal();
            member.setDefault((Expression) ASTNode.copySubtree(ast, internal.getDefault()));
         }
         return this;
      }
View Full Code Here

   }

   @Test
   public void testAnnotationWithNestedClass()
   {
      JavaAnnotation javaAnnotation = (JavaAnnotation) JavaParser
               .parse("package org.example; public @interface OuterAnnotation { " +
                        "  public class InnerClass1{ " +
                        "    public class InnerClass3{}" +
                        "  } " +
                        "  public class InnerClass2{} " +
                        "}");

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

   public void testCanParseInnerAnnotation() throws Exception
   {
      assertEquals(1, javaAnnotation.getNestedClasses().size());
      assertSame(SourceType.ANNOTATION,
               javaAnnotation.getNestedClasses().get(0).getSourceType());
      JavaAnnotation nestedAnnotation = (JavaAnnotation) javaAnnotation.getNestedClasses().get(0);
      assertEquals("MockNestedJavaAnnotationType", nestedAnnotation.getName());
      assertEquals(5, nestedAnnotation.getAnnotationElements().size());

      assertTrue(nestedAnnotation.hasAnnotationElement("value"));
      AnnotationElement value = nestedAnnotation.getAnnotationElement("value");
      assertTrue(nestedAnnotation.hasAnnotationElement(value));
      assertEquals("value", value.getName());
      assertEquals("int", value.getType());
      assertTrue(value.getTypeInspector().isPrimitive());
      assertNull(value.getDefaultValue().getLiteral());

      assertTrue(nestedAnnotation.hasAnnotationElement("charSequenceType"));
      AnnotationElement charSequenceType = nestedAnnotation.getAnnotationElement("charSequenceType");
      assertTrue(nestedAnnotation.hasAnnotationElement(charSequenceType));
      assertEquals("charSequenceType", charSequenceType.getName());
      Type<JavaAnnotation> charSequenceTypeType = charSequenceType.getTypeInspector();
      assertEquals(Class.class.getSimpleName(), charSequenceTypeType.getName());
      assertTrue(charSequenceTypeType.isParameterized());
      assertEquals(1,
               charSequenceTypeType.getTypeArguments().size());
      assertEquals("? extends CharSequence",
               charSequenceTypeType.getTypeArguments().get(0).getName());
      assertTrue(charSequenceTypeType.getTypeArguments().get(0).isWildcard());
      assertEquals("String.class", charSequenceType.getDefaultValue().getLiteral());
      assertEquals(String.class, charSequenceType.getDefaultValue().getSingleClass());

      assertTrue(nestedAnnotation.hasAnnotationElement("metasyntacticVariable"));
      AnnotationElement metasyntacticVariable = nestedAnnotation.getAnnotationElement("metasyntacticVariable");
      assertTrue(nestedAnnotation.hasAnnotationElement(metasyntacticVariable));
      assertEquals("metasyntacticVariable", metasyntacticVariable.getName());
      Type<JavaAnnotation> metasyntacticVariableType = metasyntacticVariable.getTypeInspector();
      assertEquals("org.jboss.forge.test.parser.java.common.MockEnumType", metasyntacticVariableType.getQualifiedName());
      assertFalse(metasyntacticVariableType.isArray());
      assertEquals(1, metasyntacticVariable.getAnnotations().size());
      assertEquals("Deprecated",
               metasyntacticVariable.getAnnotations().get(0).getName());
      assertSame(MockEnumType.FOO, metasyntacticVariable.getDefaultValue().getEnum(MockEnumType.class));

      assertTrue(nestedAnnotation.hasAnnotationElement("numberTypes"));
      AnnotationElement numberTypes = nestedAnnotation.getAnnotationElement("numberTypes");
      assertTrue(nestedAnnotation.hasAnnotationElement(numberTypes));
      assertEquals("numberTypes", numberTypes.getName());
      Type<JavaAnnotation> numberTypesType = numberTypes.getTypeInspector();
      assertEquals(Class.class.getSimpleName() + "[]", numberTypesType.getName());
      assertTrue(numberTypesType.isParameterized());
      assertEquals(1, numberTypesType.getTypeArguments().size());
      assertEquals("? extends Number", numberTypesType.getTypeArguments().get(0).getName());
      assertTrue(numberTypesType.getTypeArguments().get(0).isWildcard());
      assertEquals(0, numberTypes.getDefaultValue().getClassArray().length);
      numberTypes.getDefaultValue().setClassArray(Long.class, Double.class);
      assertArrayEquals(new Class[] { Long.class, Double.class }, numberTypes.getDefaultValue().getClassArray());

      assertTrue(nestedAnnotation.hasAnnotationElement("metasyntacticVariables"));
      AnnotationElement metasyntacticVariables = nestedAnnotation.getAnnotationElement("metasyntacticVariables");
      assertTrue(nestedAnnotation.hasAnnotationElement(metasyntacticVariables));
      assertEquals("metasyntacticVariables", metasyntacticVariables.getName());
      Type<JavaAnnotation> metasyntacticVariablesType = metasyntacticVariables.getTypeInspector();
      assertEquals("org.jboss.forge.test.parser.java.common.MockEnumType", metasyntacticVariablesType.getQualifiedName());
      assertTrue(metasyntacticVariablesType.isArray());
      assertEquals(0, metasyntacticVariables.getDefaultValue().getEnumArray(MockEnumType.class).length);
View Full Code Here

      if (!declaration.trim().endsWith(";"))
      {
         declaration = declaration + ";";
      }
      String stub = "public @interface Stub { " + declaration + " }";
      JavaAnnotation temp = (JavaAnnotation) JavaParser.parse(stub);
      List<AnnotationElement> fields = temp.getAnnotationElements();
      AnnotationTypeMemberDeclaration newField = (AnnotationTypeMemberDeclaration) fields.get(0).getInternal();
      return (AnnotationTypeMemberDeclaration) ASTNode.copySubtree(((ASTNode) parent.getInternal()).getAST(), newField);
   }
View Full Code Here

   @Override
   public AnnotationElement setType(final String typeName)
   {
      String simpleName = Types.toSimpleName(typeName);

      JavaAnnotation origin = getOrigin();
      if (!Strings.areEqual(typeName, simpleName) && origin.requiresImport(typeName))
      {
         origin.addImport(typeName);
      }

      Code primitive = PrimitiveType.toCode(typeName);

      org.eclipse.jdt.core.dom.Type type = null;
      if (primitive != null)
      {
         type = ast.newPrimitiveType(primitive);
      }
      else
      {
         if (!origin.requiresImport(typeName))
         {
            if (Types.isArray(typeName))
            {
               String arrayType = Types.stripArray(typeName);
               int arrayDimension = Types.getArrayDimension(typeName);
View Full Code Here

TOP

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

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.