openwebbeans-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From strub...@apache.org
Subject svn commit: r931607 - in /openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/subclassing: ./ ClassToGetIntercepted.java TestSubclassCreation.java
Date Wed, 07 Apr 2010 16:18:26 GMT
Author: struberg
Date: Wed Apr  7 16:18:25 2010
New Revision: 931607

URL: http://svn.apache.org/viewvc?rev=931607&view=rev
Log:
OWB-344 a first working sample for dynamically creating subclasses

Added:
    openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/subclassing/
    openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/subclassing/ClassToGetIntercepted.java
    openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/subclassing/TestSubclassCreation.java

Added: openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/subclassing/ClassToGetIntercepted.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/subclassing/ClassToGetIntercepted.java?rev=931607&view=auto
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/subclassing/ClassToGetIntercepted.java
(added)
+++ openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/subclassing/ClassToGetIntercepted.java
Wed Apr  7 16:18:25 2010
@@ -0,0 +1,33 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.webbeans.newtests.subclassing;
+
+
+public class ClassToGetIntercepted
+{
+
+    /**
+     * This function should get intercepted via subclassing
+     * @return "X"
+     */
+    public String getResult()
+    {
+        return "X";
+    }
+}

Added: openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/subclassing/TestSubclassCreation.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/subclassing/TestSubclassCreation.java?rev=931607&view=auto
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/subclassing/TestSubclassCreation.java
(added)
+++ openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/subclassing/TestSubclassCreation.java
Wed Apr  7 16:18:25 2010
@@ -0,0 +1,103 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.webbeans.newtests.subclassing;
+
+import org.junit.Test;
+
+import javassist.CannotCompileException;
+import javassist.ClassPool;
+import javassist.CtClass;
+import javassist.CtMethod;
+import javassist.CtNewMethod;
+import javassist.Modifier;
+import javassist.NotFoundException;
+import junit.framework.Assert;
+
+
+public class TestSubclassCreation
+{
+
+    @Test
+    public void testSubclassCreation() throws Exception
+    {
+        Class<ClassToGetIntercepted> ctgiClass = createSubClass(ClassToGetIntercepted.class);
+        ClassToGetIntercepted ctgi = ctgiClass.newInstance();
+        
+        Assert.assertNotNull(ctgi);
+        String x = ctgi.getResult();
+        Assert.assertEquals("X", x);
+
+    }
+    
+    
+    private <T> Class<T> createSubClass(Class<T> cls)
+    throws NotFoundException, CannotCompileException
+    {
+        ClassPool pool = ClassPool.getDefault();
+        
+        // first we need to load the class via javassist
+        CtClass origClass = pool.get(cls.getName());
+        
+        // we create a new subclass with a certain postfix
+        CtClass subClass = pool.makeClass(cls.getName() + "_intcpted", origClass);
+        
+        overrideInterceptedMethods(subClass);
+        
+        // now let's get the real class from it
+        @SuppressWarnings("unchecked")
+        Class<T> interceptedClass = subClass.toClass();
+        
+        return interceptedClass;
+    }
+
+
+    private void overrideInterceptedMethods(CtClass subClass)
+    throws CannotCompileException
+    {
+        CtMethod[] allMethods = subClass.getMethods();
+        
+        for (CtMethod method : allMethods)
+        {
+            overrideInterceptedMethod(subClass, method);
+        }
+    }
+
+
+    private void overrideInterceptedMethod(CtClass subClass, CtMethod method)
+    throws CannotCompileException {
+        if (!method.visibleFrom(subClass) ||
+            ((method.getModifiers() & (Modifier.FINAL | Modifier.STATIC)) > 0))
+        {
+            // we cannot delegate non visible, final or static methods
+            return;
+        }
+        
+        String methodName = method.getLongName();
+        if (methodName.startsWith("java.lang.Object."))
+        {
+            // we also have to skip methods we derive from 'java.lang.Object'
+            return;
+        }
+        
+        CtMethod overridenMethod = CtNewMethod.delegator(method, subClass);
+        subClass.addMethod(overridenMethod);
+        
+        overridenMethod.insertBefore("{System.out.println(\"juuubel!:\");};");
+    }
+}



Mime
View raw message