[jboss-cvs] JBossAS SVN: r85906 - in projects/jboss-reflect/trunk: .settings and 2 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Mon Mar 16 07:37:33 EDT 2009


Author: stalep
Date: 2009-03-16 07:37:33 -0400 (Mon, 16 Mar 2009)
New Revision: 85906

Added:
   projects/jboss-reflect/trunk/src/test/java/org/jboss/test/plugins/javassist/JavassistBodyTestCase.java
   projects/jboss-reflect/trunk/src/test/java/org/jboss/test/plugins/javassist/Pojo2.java
   projects/jboss-reflect/trunk/src/test/java/org/jboss/test/plugins/javassist/PojoBody.java
Modified:
   projects/jboss-reflect/trunk/.classpath
   projects/jboss-reflect/trunk/.settings/org.eclipse.jdt.core.prefs
   projects/jboss-reflect/trunk/src/main/java/org/jboss/reflect/plugins/javassist/JavassistUtil.java
   projects/jboss-reflect/trunk/src/test/java/org/jboss/test/plugins/javassist/JavassistMutableMethodInfoTestCase.java
   projects/jboss-reflect/trunk/src/test/java/org/jboss/test/plugins/javassist/JavassistUtilTestCase.java
Log:
[JBREFLECT-49]
Adding tests, fixing bugs.

Modified: projects/jboss-reflect/trunk/.classpath
===================================================================
--- projects/jboss-reflect/trunk/.classpath	2009-03-16 11:37:16 UTC (rev 85905)
+++ projects/jboss-reflect/trunk/.classpath	2009-03-16 11:37:33 UTC (rev 85906)
@@ -1,18 +1,19 @@
+<?xml version="1.0" encoding="UTF-8"?>
 <classpath>
-  <classpathentry kind="src" path="src/main/java"/>
-  <classpathentry kind="src" path="src/test/java" output="target/test-classes"/>
-  <classpathentry kind="output" path="target/classes"/>
-  <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
-  <classpathentry kind="var" path="M2_REPO/org/apache/ant/ant/1.7.0/ant-1.7.0.jar"/>
-  <classpathentry kind="var" path="M2_REPO/org/apache/ant/ant-junit/1.7.0/ant-junit-1.7.0.jar"/>
-  <classpathentry kind="var" path="M2_REPO/org/apache/ant/ant-launcher/1.7.0/ant-launcher-1.7.0.jar"/>
-  <classpathentry kind="var" path="M2_REPO/javassist/javassist/3.8.1.GA/javassist-3.8.1.GA.jar" sourcepath="M2_REPO/javassist/javassist/3.8.1.GA/javassist-3.8.1.GA-sources.jar"/>
-  <classpathentry kind="var" path="M2_REPO/org/jboss/jboss-common-core/2.2.8.GA/jboss-common-core-2.2.8.GA.jar" sourcepath="M2_REPO/org/jboss/jboss-common-core/2.2.8.GA/jboss-common-core-2.2.8.GA-sources.jar"/>
-  <classpathentry kind="var" path="M2_REPO/org/jboss/logging/jboss-logging-log4j/2.0.5.GA/jboss-logging-log4j-2.0.5.GA.jar" sourcepath="M2_REPO/org/jboss/logging/jboss-logging-log4j/2.0.5.GA/jboss-logging-log4j-2.0.5.GA-sources.jar"/>
-  <classpathentry kind="var" path="M2_REPO/org/jboss/logging/jboss-logging-spi/2.0.5.GA/jboss-logging-spi-2.0.5.GA.jar" sourcepath="M2_REPO/org/jboss/logging/jboss-logging-spi/2.0.5.GA/jboss-logging-spi-2.0.5.GA-sources.jar"/>
-  <classpathentry kind="var" path="M2_REPO/jboss/profiler/jvmti/jboss-profiler-jvmti/1.0.0.CR5/jboss-profiler-jvmti-1.0.0.CR5.jar"/>
-  <classpathentry kind="var" path="M2_REPO/org/jboss/jbossas/jboss-server-manager/0.1.1.GA/jboss-server-manager-0.1.1.GA.jar" sourcepath="M2_REPO/org/jboss/jbossas/jboss-server-manager/0.1.1.GA/jboss-server-manager-0.1.1.GA-sources.jar"/>
-  <classpathentry kind="var" path="M2_REPO/org/jboss/test/jboss-test/1.1.1.GA/jboss-test-1.1.1.GA.jar" sourcepath="M2_REPO/org/jboss/test/jboss-test/1.1.1.GA/jboss-test-1.1.1.GA-sources.jar"/>
-  <classpathentry kind="var" path="M2_REPO/junit/junit/4.4/junit-4.4.jar" sourcepath="M2_REPO/junit/junit/4.4/junit-4.4-sources.jar"/>
-  <classpathentry kind="var" path="M2_REPO/apache-log4j/log4j/1.2.14/log4j-1.2.14.jar" sourcepath="M2_REPO/apache-log4j/log4j/1.2.14/log4j-1.2.14-sources.jar"/>
-</classpath>
\ No newline at end of file
+	<classpathentry kind="src" path="src/main/java"/>
+	<classpathentry kind="src" output="target/test-classes" path="src/test/java"/>
+	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
+	<classpathentry kind="var" path="M2_REPO/org/apache/ant/ant/1.7.0/ant-1.7.0.jar"/>
+	<classpathentry kind="var" path="M2_REPO/org/apache/ant/ant-junit/1.7.0/ant-junit-1.7.0.jar"/>
+	<classpathentry kind="var" path="M2_REPO/org/apache/ant/ant-launcher/1.7.0/ant-launcher-1.7.0.jar"/>
+	<classpathentry kind="var" path="M2_REPO/javassist/javassist/3.8.1.GA/javassist-3.8.1.GA.jar" sourcepath="/HOME/jboss/cvs/javassist/src/main"/>
+	<classpathentry kind="var" path="M2_REPO/org/jboss/jboss-common-core/2.2.8.GA/jboss-common-core-2.2.8.GA.jar" sourcepath="M2_REPO/org/jboss/jboss-common-core/2.2.8.GA/jboss-common-core-2.2.8.GA-sources.jar"/>
+	<classpathentry kind="var" path="M2_REPO/org/jboss/logging/jboss-logging-log4j/2.0.5.GA/jboss-logging-log4j-2.0.5.GA.jar" sourcepath="M2_REPO/org/jboss/logging/jboss-logging-log4j/2.0.5.GA/jboss-logging-log4j-2.0.5.GA-sources.jar"/>
+	<classpathentry kind="var" path="M2_REPO/org/jboss/logging/jboss-logging-spi/2.0.5.GA/jboss-logging-spi-2.0.5.GA.jar" sourcepath="M2_REPO/org/jboss/logging/jboss-logging-spi/2.0.5.GA/jboss-logging-spi-2.0.5.GA-sources.jar"/>
+	<classpathentry kind="var" path="M2_REPO/jboss/profiler/jvmti/jboss-profiler-jvmti/1.0.0.CR5/jboss-profiler-jvmti-1.0.0.CR5.jar"/>
+	<classpathentry kind="var" path="M2_REPO/org/jboss/jbossas/jboss-server-manager/0.1.1.GA/jboss-server-manager-0.1.1.GA.jar"/>
+	<classpathentry kind="var" path="M2_REPO/org/jboss/test/jboss-test/1.1.1.GA/jboss-test-1.1.1.GA.jar"/>
+	<classpathentry kind="var" path="M2_REPO/junit/junit/4.4/junit-4.4.jar"/>
+	<classpathentry kind="var" path="M2_REPO/apache-log4j/log4j/1.2.14/log4j-1.2.14.jar" sourcepath="M2_REPO/apache-log4j/log4j/1.2.14/log4j-1.2.14-sources.jar"/>
+	<classpathentry kind="output" path="target/classes"/>
+</classpath>

Modified: projects/jboss-reflect/trunk/.settings/org.eclipse.jdt.core.prefs
===================================================================
--- projects/jboss-reflect/trunk/.settings/org.eclipse.jdt.core.prefs	2009-03-16 11:37:16 UTC (rev 85905)
+++ projects/jboss-reflect/trunk/.settings/org.eclipse.jdt.core.prefs	2009-03-16 11:37:33 UTC (rev 85906)
@@ -1,5 +1,5 @@
-#Fri Sep 21 17:48:03 BST 2007
-org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5
-eclipse.preferences.version=1
-org.eclipse.jdt.core.compiler.source=1.5
-org.eclipse.jdt.core.compiler.compliance=1.5
+#Tue Feb 24 21:57:31 GMT 2009
+org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5
+eclipse.preferences.version=1
+org.eclipse.jdt.core.compiler.source=1.5
+org.eclipse.jdt.core.compiler.compliance=1.5

Modified: projects/jboss-reflect/trunk/src/main/java/org/jboss/reflect/plugins/javassist/JavassistUtil.java
===================================================================
--- projects/jboss-reflect/trunk/src/main/java/org/jboss/reflect/plugins/javassist/JavassistUtil.java	2009-03-16 11:37:16 UTC (rev 85905)
+++ projects/jboss-reflect/trunk/src/main/java/org/jboss/reflect/plugins/javassist/JavassistUtil.java	2009-03-16 11:37:33 UTC (rev 85906)
@@ -25,6 +25,7 @@
 
 import org.jboss.reflect.spi.ClassInfo;
 
+import javassist.CannotCompileException;
 import javassist.ClassPool;
 import javassist.CtClass;
 import javassist.CtPrimitiveType;
@@ -44,6 +45,18 @@
 
    public static Class<?> ctClassToClass(CtClass ct)
    {
+      if(ct.isModified())
+      {
+         try
+         {
+            ct.toClass();
+         }
+         catch (CannotCompileException e)
+         {
+            throw new org.jboss.reflect.spi.CannotCompileException(e.toString());
+         }
+      }
+      
       try
       {
          if (ct.isArray())
@@ -86,13 +99,18 @@
       }
       catch (NotFoundException e)
       {
-         e.printStackTrace();
-         return null;
+         throw new org.jboss.reflect.spi.NotFoundException(e.toString());
       }
       catch (ClassNotFoundException e)
       {
-         e.printStackTrace();
-         return null;
+         try
+         {
+            return ct.toClass();
+         }
+         catch (CannotCompileException e1)
+         {
+            throw new org.jboss.reflect.spi.CannotCompileException(e1.toString());
+         }
       }
    }
    

Added: projects/jboss-reflect/trunk/src/test/java/org/jboss/test/plugins/javassist/JavassistBodyTestCase.java
===================================================================
--- projects/jboss-reflect/trunk/src/test/java/org/jboss/test/plugins/javassist/JavassistBodyTestCase.java	                        (rev 0)
+++ projects/jboss-reflect/trunk/src/test/java/org/jboss/test/plugins/javassist/JavassistBodyTestCase.java	2009-03-16 11:37:33 UTC (rev 85906)
@@ -0,0 +1,113 @@
+/*
+  * JBoss, Home of Professional Open Source
+  * Copyright 2005, JBoss Inc., and individual contributors as indicated
+  * 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.test.plugins.javassist;
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+
+import org.jboss.reflect.plugins.javassist.JavassistTypeInfoFactoryImpl;
+import org.jboss.reflect.spi.DefaultJavassistBody;
+import org.jboss.reflect.spi.InsertAfterJavassistBody;
+import org.jboss.reflect.spi.InsertBeforeJavassistBody;
+import org.jboss.reflect.spi.MutableClassInfo;
+import org.jboss.reflect.spi.MutableMethodInfo;
+import org.jboss.reflect.spi.TypeInfo;
+import org.jboss.test.ContainerTest;
+
+/**
+ * A JavassistBodyTestCase.
+ * 
+ * @author <a href="mailto:stale.pedersen at jboss.org">Stale W. Pedersen</a>
+ * @version $Revision: 1.1 $
+ */
+public class JavassistBodyTestCase extends ContainerTest
+{
+
+   /**
+    * Create a new JavassistBodyTestCase.
+    * 
+    * @param name
+    */
+   public JavassistBodyTestCase(String name)
+   {
+      super(name);
+   }
+   
+   public void testBody()
+   {
+      MutableClassInfo mci = new JavassistTypeInfoFactoryImpl().getMutable("org.jboss.test.plugins.javassist.PojoBody", null);
+//      MutableMethodInfo mmi = mci.getDeclaredMethods()[1];
+      try
+      {
+         MutableMethodInfo mmi = mci.getDeclaredMethod("foo", new TypeInfo[] {(TypeInfo) new JavassistTypeInfoFactoryImpl().get("int", Thread.currentThread().getContextClassLoader()) });
+    
+      System.out.println("got method: "+mmi.getName());
+      mmi.setBody(new InsertBeforeJavassistBody("i = 42;"));
+      MutableMethodInfo mmi2 = mci.getDeclaredMethods()[0];
+      System.out.println("mmi2: "+mmi2.getName());
+      mmi2.setBody(new InsertAfterJavassistBody("s = \"after\" + s; return s;"));
+      
+      
+      Class<?> clazz = mci.getType();
+      
+      
+         Object pojoBody = clazz.newInstance();
+         Method m1 = clazz.getDeclaredMethods()[0];
+         assertEquals(42, m1.invoke(pojoBody, new Object[] {1}));
+         Method m2 = clazz.getDeclaredMethods()[1];
+         assertEquals("afterbar", m2.invoke(pojoBody, new Object[] {}));
+         
+         System.out.println();
+    
+      }
+      catch (ClassNotFoundException e1)
+      {
+         // TODO Auto-generated catch block
+         e1.printStackTrace();
+      }
+      catch (IllegalArgumentException e)
+      {
+         // TODO Auto-generated catch block
+         e.printStackTrace();
+      }
+      catch (InvocationTargetException e)
+      {
+         // TODO Auto-generated catch block
+         e.printStackTrace();
+      }
+
+      catch (InstantiationException e)
+      {
+         // TODO Auto-generated catch block
+         e.printStackTrace();
+      }
+      catch (IllegalAccessException e)
+      {
+         // TODO Auto-generated catch block
+         e.printStackTrace();
+      }
+//      System.out.println("Method name: "+m1.getName());
+      
+      
+   }
+
+}

Modified: projects/jboss-reflect/trunk/src/test/java/org/jboss/test/plugins/javassist/JavassistMutableMethodInfoTestCase.java
===================================================================
--- projects/jboss-reflect/trunk/src/test/java/org/jboss/test/plugins/javassist/JavassistMutableMethodInfoTestCase.java	2009-03-16 11:37:16 UTC (rev 85905)
+++ projects/jboss-reflect/trunk/src/test/java/org/jboss/test/plugins/javassist/JavassistMutableMethodInfoTestCase.java	2009-03-16 11:37:33 UTC (rev 85906)
@@ -21,10 +21,13 @@
   */
 package org.jboss.test.plugins.javassist;
 
+import org.jboss.reflect.plugins.javassist.JavassistTypeInfo;
 import org.jboss.reflect.plugins.javassist.JavassistTypeInfoFactoryImpl;
+import org.jboss.reflect.spi.InsertBeforeJavassistBody;
 import org.jboss.reflect.spi.MethodInfo;
 import org.jboss.reflect.spi.MutableClassInfo;
 import org.jboss.reflect.spi.MutableMethodInfo;
+import org.jboss.reflect.spi.TypeInfo;
 import org.jboss.test.ContainerTest;
 
 /**
@@ -50,9 +53,39 @@
    {
       MutableClassInfo mci = new JavassistTypeInfoFactoryImpl().getMutable("org.jboss.test.plugins.javassist.Pojo", null);
       
-      MethodInfo[] methods = mci.getDeclaredMethods();
+      MutableMethodInfo[] methods = mci.getDeclaredMethods();
+      try
+      {
+         MutableMethodInfo bar = mci.getDeclaredMethod("bar", new TypeInfo[] {(TypeInfo) new JavassistTypeInfoFactoryImpl().get("java.lang.String", Thread.currentThread().getContextClassLoader()) });
+    
+      assertEquals(3, methods.length);
+      System.out.println("should expect bar, but got "+bar.getName());
+      assertEquals("bar", bar.getName());
+      assertEquals("java.lang.String", bar.getParameterTypes()[0].getName());
       
+      
+      }
+      catch (ClassNotFoundException e)
+      {
+         // TODO Auto-generated catch block
+         e.printStackTrace();
+      }
+      
+      
    }
    
+   public void testNewMethods()
+   {
+      MutableClassInfo mci = new JavassistTypeInfoFactoryImpl().getMutable("org.jboss.test.plugins.javassist.Pojo", null);
+      MutableMethodInfo newMethod1 = mci.createMutableMethod(new InsertBeforeJavassistBody("public void test1() { }"));
+      mci.addMethod(newMethod1);
+      
+      assertEquals(4, mci.getDeclaredMethods().length);
+      
+      Class<?> theClass = mci.getType();
+      
+      assertEquals(4, theClass.getDeclaredMethods().length);
+   }
+   
 
 }

Modified: projects/jboss-reflect/trunk/src/test/java/org/jboss/test/plugins/javassist/JavassistUtilTestCase.java
===================================================================
--- projects/jboss-reflect/trunk/src/test/java/org/jboss/test/plugins/javassist/JavassistUtilTestCase.java	2009-03-16 11:37:16 UTC (rev 85905)
+++ projects/jboss-reflect/trunk/src/test/java/org/jboss/test/plugins/javassist/JavassistUtilTestCase.java	2009-03-16 11:37:33 UTC (rev 85906)
@@ -25,11 +25,13 @@
 import java.util.HashMap;
 import java.util.Map;
 
+import javassist.CannotCompileException;
 import javassist.ClassPool;
 import javassist.CtClass;
 import javassist.CtConstructor;
 import javassist.CtField;
 import javassist.CtMethod;
+import javassist.CtNewMethod;
 import javassist.Modifier;
 import javassist.NotFoundException;
 
@@ -82,6 +84,46 @@
       assertEquals("[[[Ljava.lang.String;", clazz.getName());
    }
    
+   public void testGeneratedCtClassToClass()
+   {
+      CtClass clazz = ClassPool.getDefault().makeClass("TestClass");
+      try
+      {
+         CtMethod foo = CtNewMethod.make("public void foo() { }", clazz);
+         clazz.addMethod(foo);
+         Class<?> theClass = JavassistUtil.ctClassToClass(clazz);
+         assertEquals("TestClass", theClass.getName());
+      }
+      catch (CannotCompileException e)
+      {
+         e.printStackTrace();
+      }
+   }
+   
+   public void testChangedCtClassToClass()
+   {
+      try
+      {
+         CtClass clazz = ClassPool.getDefault().get("org.jboss.test.plugins.javassist.Pojo2");
+         CtMethod foo = CtNewMethod.make("public void test1() { }", clazz);
+         clazz.addMethod(foo);
+         
+         
+         Class theClass = JavassistUtil.ctClassToClass(clazz);
+         assertEquals(2, theClass.getDeclaredMethods().length);
+      }
+      catch (CannotCompileException e)
+      {
+         // TODO Auto-generated catch block
+         e.printStackTrace();
+      }
+      catch (NotFoundException e)
+      {
+         e.printStackTrace();
+      }
+      
+   }
+   
    public void testModifier()
    {
       assertEquals(ModifierInfo.PUBLIC, Modifier.PUBLIC);

Added: projects/jboss-reflect/trunk/src/test/java/org/jboss/test/plugins/javassist/Pojo2.java
===================================================================
--- projects/jboss-reflect/trunk/src/test/java/org/jboss/test/plugins/javassist/Pojo2.java	                        (rev 0)
+++ projects/jboss-reflect/trunk/src/test/java/org/jboss/test/plugins/javassist/Pojo2.java	2009-03-16 11:37:33 UTC (rev 85906)
@@ -0,0 +1,38 @@
+/*
+  * JBoss, Home of Professional Open Source
+  * Copyright 2005, JBoss Inc., and individual contributors as indicated
+  * 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.test.plugins.javassist;
+
+/**
+ * A Pojo.
+ * 
+ * @author <a href="mailto:stale.pedersen at jboss.org">Stale W. Pedersen</a>
+ * @version $Revision: 1.1 $
+ */
+public class Pojo2
+{
+  
+   public int foo(int i)
+   {
+      return i;
+   }
+   
+}

Added: projects/jboss-reflect/trunk/src/test/java/org/jboss/test/plugins/javassist/PojoBody.java
===================================================================
--- projects/jboss-reflect/trunk/src/test/java/org/jboss/test/plugins/javassist/PojoBody.java	                        (rev 0)
+++ projects/jboss-reflect/trunk/src/test/java/org/jboss/test/plugins/javassist/PojoBody.java	2009-03-16 11:37:33 UTC (rev 85906)
@@ -0,0 +1,45 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., and individual contributors as indicated
+ * 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.test.plugins.javassist;
+
+/**
+ * A Pojo.
+ * 
+ * @author <a href="mailto:stale.pedersen at jboss.org">Stale W. Pedersen</a>
+ * @version $Revision: 1.1 $
+ */
+public class PojoBody
+{
+
+   private String s;
+   public int foo(int i)
+   {
+      return i;
+   }
+
+   public String bar()
+   {
+      s = "bar";
+      return s;
+   }
+
+}




More information about the jboss-cvs-commits mailing list