[seam-commits] Seam SVN: r13536 - in sandbox/encore/core/src: main/java/org/jboss/encore/grammar/java/ast and 3 other directories.

seam-commits at lists.jboss.org seam-commits at lists.jboss.org
Fri Jul 30 17:38:47 EDT 2010


Author: lincolnthree
Date: 2010-07-30 17:38:46 -0400 (Fri, 30 Jul 2010)
New Revision: 13536

Added:
   sandbox/encore/core/src/main/java/org/jboss/encore/grammar/java/Annotation.java
   sandbox/encore/core/src/main/java/org/jboss/encore/grammar/java/impl/
   sandbox/encore/core/src/main/java/org/jboss/encore/grammar/java/impl/AnnotationImpl.java
   sandbox/encore/core/src/test/java/org/jboss/encore/grammar/java/ClassAnnotationTest.java
Modified:
   sandbox/encore/core/src/main/java/org/jboss/encore/grammar/java/JavaClass.java
   sandbox/encore/core/src/main/java/org/jboss/encore/grammar/java/Method.java
   sandbox/encore/core/src/main/java/org/jboss/encore/grammar/java/ast/ModifierAccessor.java
   sandbox/encore/core/src/test/java/org/jboss/encore/grammar/java/JavaClassTest.java
   sandbox/encore/core/src/test/resources/org/jboss/encore/grammar/java/MockClassFile.java
Log:
Added class annotation support (still needs value settings)... going to need to figure out a way to handle / respect / automatically manage imports.

Added: sandbox/encore/core/src/main/java/org/jboss/encore/grammar/java/Annotation.java
===================================================================
--- sandbox/encore/core/src/main/java/org/jboss/encore/grammar/java/Annotation.java	                        (rev 0)
+++ sandbox/encore/core/src/main/java/org/jboss/encore/grammar/java/Annotation.java	2010-07-30 21:38:46 UTC (rev 13536)
@@ -0,0 +1,13 @@
+package org.jboss.encore.grammar.java;
+
+import org.jboss.encore.grammar.Internal;
+import org.jboss.encore.grammar.Mutable;
+
+public interface Annotation extends Internal, Mutable
+{
+
+   String getName();
+
+   Annotation setName(String className);
+
+}

Modified: sandbox/encore/core/src/main/java/org/jboss/encore/grammar/java/JavaClass.java
===================================================================
--- sandbox/encore/core/src/main/java/org/jboss/encore/grammar/java/JavaClass.java	2010-07-30 15:13:34 UTC (rev 13535)
+++ sandbox/encore/core/src/main/java/org/jboss/encore/grammar/java/JavaClass.java	2010-07-30 21:38:46 UTC (rev 13536)
@@ -45,8 +45,11 @@
 import org.jboss.encore.grammar.java.ast.MethodFinderVisitor;
 import org.jboss.encore.grammar.java.ast.ModifierAccessor;
 import org.jboss.encore.grammar.java.ast.TypeDeclarationFinderVisitor;
+import org.jboss.encore.grammar.java.impl.AnnotationImpl;
 
 /**
+ * Represents a Java Source File
+ * 
  * @author <a href="mailto:lincolnbaxter at gmail.com">Lincoln Baxter, III</a>
  */
 public class JavaClass implements Internal, Mutable
@@ -102,6 +105,44 @@
    }
 
    /*
+    * Annotation modifiers
+    */
+   @SuppressWarnings("unchecked")
+   public Annotation addAnnotation()
+   {
+      Annotation annotation = new AnnotationImpl(this);
+      getTypeDeclaration().modifiers().add(annotation.getInternal());
+      return annotation;
+   }
+
+   public Annotation addAnnotation(Class<?> clazz)
+   {
+      return addAnnotation(clazz.getName());
+   }
+
+   public Annotation addAnnotation(final String className)
+   {
+      return addAnnotation().setName(className);
+   }
+
+   public List<Annotation> getAnnotations()
+   {
+      List<Annotation> result = new ArrayList<Annotation>();
+
+      List<?> modifiers = getTypeDeclaration().modifiers();
+      for (Object object : modifiers)
+      {
+         if (object instanceof org.eclipse.jdt.core.dom.Annotation)
+         {
+            Annotation annotation = new AnnotationImpl(this, object);
+            result.add(annotation);
+         }
+      }
+
+      return result;
+   }
+
+   /*
     * Import modifiers
     */
 
@@ -250,9 +291,22 @@
    public JavaClass setName(String name)
    {
       getTypeDeclaration().setName(unit.getAST().newSimpleName(name));
+      updateConstructorNames();
       return this;
    }
 
+   private void updateConstructorNames()
+   {
+      for (Method m : getMethods())
+      {
+         if (m.isConstructor())
+         {
+            m.setConstructor(false);
+            m.setConstructor(true);
+         }
+      }
+   }
+
    /*
     * Package modifiers
     */

Modified: sandbox/encore/core/src/main/java/org/jboss/encore/grammar/java/Method.java
===================================================================
--- sandbox/encore/core/src/main/java/org/jboss/encore/grammar/java/Method.java	2010-07-30 15:13:34 UTC (rev 13535)
+++ sandbox/encore/core/src/main/java/org/jboss/encore/grammar/java/Method.java	2010-07-30 21:38:46 UTC (rev 13536)
@@ -105,9 +105,14 @@
       return this;
    }
 
-   public Method setConstructor(final boolean isConstructor)
+   /**
+    * Toggle this method as a constructor. If true, and the name of the
+    * {@link Method} is not the same as the name of its parent {@link JavaClass}
+    * , update the name of the to match.
+    */
+   public Method setConstructor(final boolean constructor)
    {
-      method.setConstructor(isConstructor);
+      method.setConstructor(constructor);
       if (isConstructor())
       {
          method.setName(ast.newSimpleName(parent.getName()));

Modified: sandbox/encore/core/src/main/java/org/jboss/encore/grammar/java/ast/ModifierAccessor.java
===================================================================
--- sandbox/encore/core/src/main/java/org/jboss/encore/grammar/java/ast/ModifierAccessor.java	2010-07-30 15:13:34 UTC (rev 13535)
+++ sandbox/encore/core/src/main/java/org/jboss/encore/grammar/java/ast/ModifierAccessor.java	2010-07-30 21:38:46 UTC (rev 13536)
@@ -9,11 +9,10 @@
 
 public class ModifierAccessor
 {
-   @SuppressWarnings("unchecked")
    public boolean hasModifier(BodyDeclaration body, final ModifierKeyword modifier)
    {
       boolean result = false;
-      List<Modifier> modifiers = body.modifiers();
+      List<Modifier> modifiers = getModifiers(body);
       for (Modifier m : modifiers)
       {
          if (m.getKeyword() == modifier)
@@ -24,10 +23,25 @@
       return result;
    }
 
+   private List<Modifier> getModifiers(BodyDeclaration body)
+   {
+      List<Modifier> result = new ArrayList<Modifier>();
+      List<?> modifiers = body.modifiers();
+      for (Object m : modifiers)
+      {
+         if (m instanceof Modifier)
+         {
+            Modifier mod = (Modifier) m;
+            result.add(mod);
+         }
+      }
+      return result;
+   }
+
    @SuppressWarnings("unchecked")
    public List<Modifier> clearVisibility(BodyDeclaration body)
    {
-      List<Modifier> modifiers = body.modifiers();
+      List<Modifier> modifiers = getModifiers(body);
 
       List<Modifier> toBeRemoved = new ArrayList<Modifier>();
       for (Modifier modifier : modifiers)
@@ -38,7 +52,7 @@
          }
       }
 
-      modifiers.removeAll(toBeRemoved);
+      body.modifiers().removeAll(toBeRemoved);
       return modifiers;
    }
 
@@ -48,10 +62,9 @@
       body.modifiers().add(body.getAST().newModifier(keyword));
    }
 
-   @SuppressWarnings("unchecked")
    public void removeModifier(BodyDeclaration body, ModifierKeyword keyword)
    {
-      List<Modifier> modifiers = body.modifiers();
+      List<Modifier> modifiers = getModifiers(body);
 
       List<Modifier> toBeRemoved = new ArrayList<Modifier>();
       for (Modifier modifier : modifiers)
@@ -62,6 +75,6 @@
          }
       }
 
-      modifiers.removeAll(toBeRemoved);
+      body.modifiers().removeAll(toBeRemoved);
    }
 }

Added: sandbox/encore/core/src/main/java/org/jboss/encore/grammar/java/impl/AnnotationImpl.java
===================================================================
--- sandbox/encore/core/src/main/java/org/jboss/encore/grammar/java/impl/AnnotationImpl.java	                        (rev 0)
+++ sandbox/encore/core/src/main/java/org/jboss/encore/grammar/java/impl/AnnotationImpl.java	2010-07-30 21:38:46 UTC (rev 13536)
@@ -0,0 +1,59 @@
+package org.jboss.encore.grammar.java.impl;
+
+import org.eclipse.jdt.core.dom.AST;
+import org.eclipse.jdt.core.dom.CompilationUnit;
+import org.jboss.encore.grammar.java.Annotation;
+import org.jboss.encore.grammar.java.JavaClass;
+
+public class AnnotationImpl implements Annotation
+{
+   private JavaClass parent = null;
+   private CompilationUnit cu = null;
+   private AST ast = null;
+   private final org.eclipse.jdt.core.dom.Annotation annotation;
+
+   private void init(final JavaClass parent)
+   {
+      this.parent = parent;
+      cu = (CompilationUnit) parent.getInternal();
+      ast = cu.getAST();
+   }
+
+   public AnnotationImpl(JavaClass parent)
+   {
+      init(parent);
+      this.annotation = ast.newNormalAnnotation();
+   }
+
+   public AnnotationImpl(JavaClass parent, Object internal)
+   {
+      init(parent);
+      this.annotation = (org.eclipse.jdt.core.dom.Annotation) internal;
+   }
+
+   @Override
+   public String getName()
+   {
+      return annotation.getTypeName().getFullyQualifiedName();
+   }
+
+   @Override
+   public Annotation setName(String className)
+   {
+      annotation.setTypeName(ast.newName(className));
+      return this;
+   }
+
+   @Override
+   public Object getInternal()
+   {
+      return annotation;
+   }
+
+   @Override
+   public void applyChanges()
+   {
+      parent.applyChanges();
+   }
+
+}

Added: sandbox/encore/core/src/test/java/org/jboss/encore/grammar/java/ClassAnnotationTest.java
===================================================================
--- sandbox/encore/core/src/test/java/org/jboss/encore/grammar/java/ClassAnnotationTest.java	                        (rev 0)
+++ sandbox/encore/core/src/test/java/org/jboss/encore/grammar/java/ClassAnnotationTest.java	2010-07-30 21:38:46 UTC (rev 13536)
@@ -0,0 +1,105 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.encore.grammar.java;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import java.io.InputStream;
+import java.util.List;
+
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * @author <a href="mailto:lincolnbaxter at gmail.com">Lincoln Baxter, III</a>
+ */
+public class ClassAnnotationTest
+{
+   private InputStream stream;
+   private JavaClass javaClass;
+
+   @Before
+   public void reset()
+   {
+      stream = ClassAnnotationTest.class.getResourceAsStream("/org/jboss/encore/grammar/java/MockClassFile.java");
+      javaClass = new JavaClass(stream);
+   }
+
+   @Test
+   public void testParseAnnotation() throws Exception
+   {
+      List<Annotation> annotations = javaClass.getAnnotations();
+      assertEquals(1, annotations.size());
+   }
+
+   @Test
+   public void testAddAnnotation() throws Exception
+   {
+      javaClass.addAnnotation().setName("RequestScoped");
+      List<Annotation> annotations = javaClass.getAnnotations();
+      assertEquals(2, annotations.size());
+      assertEquals("RequestScoped", annotations.get(annotations.size() - 1).getName());
+   }
+
+   @Test
+   public void testAddAnnotationByClass() throws Exception
+   {
+      javaClass.addAnnotation(Test.class);
+      List<Annotation> annotations = javaClass.getAnnotations();
+      assertEquals(2, annotations.size());
+      assertEquals(Test.class.getName(), annotations.get(annotations.size() - 1).getName());
+      javaClass.applyChanges();
+      assertTrue(javaClass.toString().contains("@" + Test.class.getName()));
+   }
+
+   @Test
+   public void testAddAnnotationByName() throws Exception
+   {
+      javaClass.addAnnotation("RequestScoped");
+      List<Annotation> annotations = javaClass.getAnnotations();
+      assertEquals(2, annotations.size());
+      assertEquals("RequestScoped", annotations.get(annotations.size() - 1).getName());
+      javaClass.applyChanges();
+      assertTrue(javaClass.toString().contains("@RequestScoped"));
+   }
+
+   @Test
+   public void testCanAddAnnotationDuplicate() throws Exception
+   {
+      javaClass.addAnnotation(Test.class);
+      javaClass.addAnnotation(Test.class);
+      List<Annotation> annotations = javaClass.getAnnotations();
+      assertEquals(3, annotations.size());
+      assertEquals(Test.class.getName(), annotations.get(annotations.size() - 1).getName());
+      assertEquals(Test.class.getName(), annotations.get(annotations.size() - 2).getName());
+      javaClass.applyChanges();
+      String pattern = "@" + Test.class.getName() + "() " + "@" + Test.class.getName() + "()";
+      assertTrue(javaClass.toString().contains(pattern));
+   }
+
+   @Test(expected = IllegalArgumentException.class)
+   public void testCannotAddAnnotationWithIllegalName() throws Exception
+   {
+      javaClass.addAnnotation("sdf*(&#$%");
+   }
+}

Modified: sandbox/encore/core/src/test/java/org/jboss/encore/grammar/java/JavaClassTest.java
===================================================================
--- sandbox/encore/core/src/test/java/org/jboss/encore/grammar/java/JavaClassTest.java	2010-07-30 15:13:34 UTC (rev 13535)
+++ sandbox/encore/core/src/test/java/org/jboss/encore/grammar/java/JavaClassTest.java	2010-07-30 21:38:46 UTC (rev 13536)
@@ -50,6 +50,20 @@
    }
 
    @Test
+   public void testApplyChangesRequiredForModification() throws Exception
+   {
+      assertEquals("MockClassFile", javaClass.getName());
+      javaClass.setName("Telephone");
+      assertEquals("Telephone", javaClass.getName());
+      assertFalse(javaClass.toString().contains("Telephone"));
+      assertTrue(javaClass.toString().contains("MockClassFile"));
+
+      javaClass.applyChanges();
+      assertTrue(javaClass.toString().contains("Telephone"));
+      assertFalse(javaClass.toString().contains("MockClassFile"));
+   }
+
+   @Test
    public void testParse() throws Exception
    {
       assertEquals(URL.class.getName(), javaClass.getImports().get(0).getName());
@@ -68,6 +82,16 @@
    }
 
    @Test
+   public void testSetNameUpdatesConstructorNames() throws Exception
+   {
+      assertEquals("MockClassFile", javaClass.getName());
+      assertEquals("MockClassFile", javaClass.getMethods().get(0).getName());
+      javaClass.setName("Telephone");
+      assertEquals("Telephone", javaClass.getName());
+      assertEquals("Telephone", javaClass.getMethods().get(0).getName());
+   }
+
+   @Test
    public void testSetPackage() throws Exception
    {
       javaClass.setPackage("org.lincoln");

Modified: sandbox/encore/core/src/test/resources/org/jboss/encore/grammar/java/MockClassFile.java
===================================================================
--- sandbox/encore/core/src/test/resources/org/jboss/encore/grammar/java/MockClassFile.java	2010-07-30 15:13:34 UTC (rev 13535)
+++ sandbox/encore/core/src/test/resources/org/jboss/encore/grammar/java/MockClassFile.java	2010-07-30 21:38:46 UTC (rev 13536)
@@ -2,6 +2,7 @@
 
 import java.net.URL;
 
+ at Deprecated
 public class MockClassFile
 {
    private String field;



More information about the seam-commits mailing list