myfaces-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From we...@apache.org
Subject svn commit: r813978 - in /myfaces/extensions/scripting/trunk/core/core/src/main/java/org/apache/myfaces/scripting: core/util/ClassUtils.java loaders/java/DynamicClassIdentifier.java loaders/java/_ScriptingClass.java
Date Fri, 11 Sep 2009 18:40:13 GMT
Author: werpu
Date: Fri Sep 11 18:40:12 2009
New Revision: 813978

URL: http://svn.apache.org/viewvc?rev=813978&view=rev
Log:
http://issues.apache.org/jira/browse/EXTSCRIPT-15

we have now moved over to ASM instead of BCEL

Removed:
    myfaces/extensions/scripting/trunk/core/core/src/main/java/org/apache/myfaces/scripting/loaders/java/_ScriptingClass.java
Modified:
    myfaces/extensions/scripting/trunk/core/core/src/main/java/org/apache/myfaces/scripting/core/util/ClassUtils.java
    myfaces/extensions/scripting/trunk/core/core/src/main/java/org/apache/myfaces/scripting/loaders/java/DynamicClassIdentifier.java

Modified: myfaces/extensions/scripting/trunk/core/core/src/main/java/org/apache/myfaces/scripting/core/util/ClassUtils.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/scripting/trunk/core/core/src/main/java/org/apache/myfaces/scripting/core/util/ClassUtils.java?rev=813978&r1=813977&r2=813978&view=diff
==============================================================================
--- myfaces/extensions/scripting/trunk/core/core/src/main/java/org/apache/myfaces/scripting/core/util/ClassUtils.java
(original)
+++ myfaces/extensions/scripting/trunk/core/core/src/main/java/org/apache/myfaces/scripting/core/util/ClassUtils.java
Fri Sep 11 18:40:12 2009
@@ -19,17 +19,21 @@
 package org.apache.myfaces.scripting.core.util;
 
 
-import org.apache.bcel.util.SyntheticRepository;
-import org.apache.bcel.util.ClassPath;
-import org.apache.bcel.classfile.JavaClass;
-import org.apache.bcel.generic.ClassGen;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 import org.apache.myfaces.shared_impl.util.ClassLoaderExtension;
+import org.apache.myfaces.scripting.loaders.java.ScriptingClass;
+import org.objectweb.asm.ClassReader;
+import org.objectweb.asm.ClassVisitor;
+import org.objectweb.asm.ClassWriter;
+import org.objectweb.asm.Type;
+import org.objectweb.asm.tree.AnnotationNode;
+import org.objectweb.asm.tree.ClassNode;
+
+
+import java.io.*;
+import java.util.LinkedList;
 
-import java.lang.reflect.Method;
-import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Constructor;
-import java.io.IOException;
-import java.io.File;
 
 /**
  * @author werpu
@@ -39,7 +43,6 @@
  */
 public class ClassUtils {
 
-
     public static Class forName(String name) {
         try {
             return Class.forName(name);
@@ -48,41 +51,94 @@
         }
     }
 
-
     /**
-     * we use the BCEL here to add a marker interface dynamically on the compiled java class
-     * so that later we can identify the marked class as being of dynamic origin
-     * that way we dont have to hammer any data structure but can work over introspection
-     * to check for an implemented marker interface
-     * <p/>
-     * I cannot use the planned annotation for now
-     * because the BCEL has annotation support only
-     * in the trunk but in no official release,
-     * the annotation support will be added as soon as it is possible to use it
+     * We use asm here to add the marker annotation
+     * to the list of our public annotations
      *
      * @param classPath the root classPath which hosts our class
      * @param className the className from the class which has to be rewritten
      * @throws ClassNotFoundException
      */
     public static void markAsDynamicJava(String classPath, String className) throws ClassNotFoundException
{
-        SyntheticRepository repo = SyntheticRepository.getInstance(new ClassPath(classPath));
-        repo.clear();
-        JavaClass javaClass = repo.loadClass(className);
-        ClassGen classGen = new ClassGen(javaClass);
+        FileInputStream fIstr = null;
+        FileOutputStream foStream = null;
+        try {
+            File classFile = classNameToFile(classPath, className);
+            fIstr = new FileInputStream(classFile);
 
-        classGen.addInterface("org.apache.myfaces.scripting.loaders.java._ScriptingClass");
-        classGen.update();
+            ClassNode node = new ClassNode();
+            ClassReader clsReader = new ClassReader(fIstr);
+            //ClassWriter wrt = new ClassWriter();
+            clsReader.accept((ClassVisitor) node, ClassReader.SKIP_FRAMES );
+            //node.accept(wrt);
+            ClassWriter wrt = new ClassWriter(0);
+
+            if (node.visibleAnnotations == null) {
+                node.visibleAnnotations = new LinkedList<AnnotationNode>();
+            }
+            
+            boolean hasAnnotation = false;
+            String annotationMarker = Type.getDescriptor(ScriptingClass.class);
+            for (Object elem : node.visibleAnnotations) {
+                AnnotationNode aNode = (AnnotationNode) elem;
+                if (aNode.desc.equals(annotationMarker)) {
+                    hasAnnotation = true;
+                    break;
+                }
+            }
+            if (!hasAnnotation) {
+                node.visibleAnnotations.add(new AnnotationNode(annotationMarker));
+            }
+            node.accept(wrt);
+
+            byte[] finalClass = wrt.toByteArray();
+            fIstr.close();
+            fIstr = null;
+
+            foStream = new FileOutputStream(classNameToFile(classPath, className));
+            foStream.write(finalClass);
+            foStream.flush();
+          
 
-        File target = classNameToFile(classPath, className);
+        } catch (FileNotFoundException ex) {
+            throw new ClassNotFoundException("Class " + className + " not found ");
 
+        } catch (IOException e) {
+            logError(e);
+        } finally {
+            closeStreams(fIstr, foStream);
+        }
+
+    }
+
+    private static void logError(IOException e) {
+        Log log = LogFactory.getLog(ClassUtils.class);
+        log.error(e);
+    }
+
+    private static void closeStreams(FileInputStream fIstr, FileOutputStream foStream) {
         try {
-            classGen.getJavaClass().dump(target);
+            if (fIstr != null) {
+                fIstr.close();
+            }
         } catch (IOException e) {
-            e.printStackTrace();
+            logError(e);
+        }
+        try {
+            if (foStream != null) {
+                foStream.close();
+            }
+        } catch (IOException e) {
+            logError(e);
         }
     }
 
 
+
+
+
+
+
     public static File classNameToFile(String classPath, String className) {
         String classFileName = classNameToRelativeFileName(className);
         File target = new File(classPath + File.separator + classFileName);
@@ -110,4 +166,5 @@
     public Class classForName(String name) throws ClassNotFoundException {
         return org.apache.myfaces.shared_impl.util.ClassUtils.classForName(name);
     }
+
 }

Modified: myfaces/extensions/scripting/trunk/core/core/src/main/java/org/apache/myfaces/scripting/loaders/java/DynamicClassIdentifier.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/scripting/trunk/core/core/src/main/java/org/apache/myfaces/scripting/loaders/java/DynamicClassIdentifier.java?rev=813978&r1=813977&r2=813978&view=diff
==============================================================================
--- myfaces/extensions/scripting/trunk/core/core/src/main/java/org/apache/myfaces/scripting/loaders/java/DynamicClassIdentifier.java
(original)
+++ myfaces/extensions/scripting/trunk/core/core/src/main/java/org/apache/myfaces/scripting/loaders/java/DynamicClassIdentifier.java
Fri Sep 11 18:40:12 2009
@@ -40,9 +40,9 @@
             alreadyChecked.put(clazz.getName(), Boolean.TRUE);
             return true;
         }
-        boolean retVal = checkForInterface(clazz);
-        //alreadyChecked.put(clazz.getName(), retVal ? Boolean.TRUE : Boolean.FALSE);
-        return retVal;
+
+        alreadyChecked.put(clazz.getName(), Boolean.FALSE);
+        return false;
     }
 
 
@@ -50,25 +50,12 @@
         Map<String, Boolean> checked = (Map<String, Boolean>) _checked.get();
         if (checked == null) {
             checked = new HashMap<String, Boolean>();
+            _checked.set(checked);
         }
         return checked;
     }
 
-    private boolean checkForInterface(Class clazz) {
-        Class[] interfaces = clazz.getInterfaces();
-        if (interfaces == null) {
-            return false;
-
-        }
-
-        for (Class theInterface : interfaces) {
-            if (theInterface.equals(_ScriptingClass.class)) {
-                return true;
-            }
-        }
 
-        return false;
-    }
 
     private boolean checkForAnnotation(Class clazz) {
         Annotation identifier = clazz.getAnnotation(ScriptingClass.class);



Mime
View raw message