commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dbros...@apache.org
Subject svn commit: r1745933 - in /commons/proper/bcel/trunk/src/main/java/org/apache/commons/bcel6/util: ClassPathRepository.java MemorySensitiveClassPathRepository.java SyntheticRepository.java
Date Sat, 28 May 2016 20:31:42 GMT
Author: dbrosius
Date: Sat May 28 20:31:42 2016
New Revision: 1745933

URL: http://svn.apache.org/viewvc?rev=1745933&view=rev
Log:
Issue BCEL-73: Allow for instance based repositories

Added:
    commons/proper/bcel/trunk/src/main/java/org/apache/commons/bcel6/util/ClassPathRepository.java
    commons/proper/bcel/trunk/src/main/java/org/apache/commons/bcel6/util/MemorySensitiveClassPathRepository.java
Modified:
    commons/proper/bcel/trunk/src/main/java/org/apache/commons/bcel6/util/SyntheticRepository.java

Added: commons/proper/bcel/trunk/src/main/java/org/apache/commons/bcel6/util/ClassPathRepository.java
URL: http://svn.apache.org/viewvc/commons/proper/bcel/trunk/src/main/java/org/apache/commons/bcel6/util/ClassPathRepository.java?rev=1745933&view=auto
==============================================================================
--- commons/proper/bcel/trunk/src/main/java/org/apache/commons/bcel6/util/ClassPathRepository.java
(added)
+++ commons/proper/bcel/trunk/src/main/java/org/apache/commons/bcel6/util/ClassPathRepository.java
Sat May 28 20:31:42 2016
@@ -0,0 +1,171 @@
+/*
+ * 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.commons.bcel6.util;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.commons.bcel6.classfile.ClassParser;
+import org.apache.commons.bcel6.classfile.JavaClass;
+
+/**
+ * This repository is used in situations where a Class is created outside the realm of a
ClassLoader. Classes are loaded from the file systems using the paths
+ * specified in the given class path. By default, this is the value returned by ClassPath.getClassPath().
<br>
+ *
+ * @see org.apache.commons.bcel6.Repository
+ */
+public class ClassPathRepository implements Repository {
+
+    private ClassPath _path = null;
+    private final Map<String, JavaClass> _loadedClasses = new HashMap<>(); //
CLASSNAME X JAVACLASS
+
+    public ClassPathRepository(ClassPath path) {
+        _path = path;
+    }
+
+    /**
+     * Store a new JavaClass instance into this Repository.
+     */
+    @Override
+    public void storeClass(JavaClass clazz) {
+        _loadedClasses.put(clazz.getClassName(), clazz);
+        clazz.setRepository(this);
+    }
+
+    /**
+     * Remove class from repository
+     */
+    @Override
+    public void removeClass(JavaClass clazz) {
+        _loadedClasses.remove(clazz.getClassName());
+    }
+
+    /**
+     * Find an already defined (cached) JavaClass object by name.
+     */
+    @Override
+    public JavaClass findClass(String className) {
+        return _loadedClasses.get(className);
+    }
+
+    /**
+     * Find a JavaClass object by name. If it is already in this Repository, the Repository
version is returned. Otherwise, the Repository's classpath is
+     * searched for the class (and it is added to the Repository if found).
+     *
+     * @param className
+     *            the name of the class
+     * @return the JavaClass object
+     * @throws ClassNotFoundException
+     *             if the class is not in the Repository, and could not be found on the classpath
+     */
+    @Override
+    public JavaClass loadClass(String className) throws ClassNotFoundException {
+        if ((className == null) || className.equals("")) {
+            throw new IllegalArgumentException("Invalid class name " + className);
+        }
+        className = className.replace('/', '.'); // Just in case, canonical form
+        JavaClass clazz = findClass(className);
+        if (clazz != null) {
+            return clazz;
+        }
+        try {
+            return loadClass(_path.getInputStream(className), className);
+        } catch (IOException e) {
+            throw new ClassNotFoundException("Exception while looking for class " + className
+ ": " + e, e);
+        }
+    }
+
+    /**
+     * Find the JavaClass object for a runtime Class object. If a class with the same name
is already in this Repository, the Repository version is returned.
+     * Otherwise, getResourceAsStream() is called on the Class object to find the class's
representation. If the representation is found, it is added to the
+     * Repository.
+     *
+     * @see Class
+     * @param clazz
+     *            the runtime Class object
+     * @return JavaClass object for given runtime class
+     * @throws ClassNotFoundException
+     *             if the class is not in the Repository, and its representation could not
be found
+     */
+    @Override
+    public JavaClass loadClass(Class<?> clazz) throws ClassNotFoundException {
+        InputStream clsStream = null;
+        try {
+            String className = clazz.getName();
+            JavaClass repositoryClass = findClass(className);
+            if (repositoryClass != null) {
+                return repositoryClass;
+            }
+            String name = className;
+            int i = name.lastIndexOf('.');
+            if (i > 0) {
+                name = name.substring(i + 1);
+            }
+            clsStream = clazz.getResourceAsStream(name + ".class");
+            return loadClass(clsStream, className);
+        } finally {
+            try {
+                if (clsStream != null) {
+                    clsStream.close();
+                }
+            } catch (IOException ioe) {
+                // don't care
+            }
+        }
+    }
+
+    private JavaClass loadClass(InputStream is, String className) throws ClassNotFoundException
{
+        try {
+            if (is != null) {
+                ClassParser parser = new ClassParser(is, className);
+                JavaClass clazz = parser.parse();
+                storeClass(clazz);
+                return clazz;
+            }
+        } catch (IOException e) {
+            throw new ClassNotFoundException("Exception while looking for class " + className
+ ": " + e, e);
+        } finally {
+            if (is != null) {
+                try {
+                    is.close();
+                } catch (IOException e) {
+                    // ignored
+                }
+            }
+        }
+        throw new ClassNotFoundException("SyntheticRepository could not load " + className);
+    }
+
+    /**
+     * ClassPath associated with the Repository.
+     */
+    @Override
+    public ClassPath getClassPath() {
+        return _path;
+    }
+
+    /**
+     * Clear all entries from cache.
+     */
+    @Override
+    public void clear() {
+        _loadedClasses.clear();
+    }
+}

Added: commons/proper/bcel/trunk/src/main/java/org/apache/commons/bcel6/util/MemorySensitiveClassPathRepository.java
URL: http://svn.apache.org/viewvc/commons/proper/bcel/trunk/src/main/java/org/apache/commons/bcel6/util/MemorySensitiveClassPathRepository.java?rev=1745933&view=auto
==============================================================================
--- commons/proper/bcel/trunk/src/main/java/org/apache/commons/bcel6/util/MemorySensitiveClassPathRepository.java
(added)
+++ commons/proper/bcel/trunk/src/main/java/org/apache/commons/bcel6/util/MemorySensitiveClassPathRepository.java
Sat May 28 20:31:42 2016
@@ -0,0 +1,177 @@
+/*
+ * 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.commons.bcel6.util;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.lang.ref.SoftReference;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.commons.bcel6.classfile.ClassParser;
+import org.apache.commons.bcel6.classfile.JavaClass;
+
+/**
+ * This repository is used in situations where a Class is created outside the realm of a
ClassLoader. Classes are loaded from the file systems using the paths
+ * specified in the given class path. By default, this is the value returned by ClassPath.getClassPath().
This repository holds onto classes with
+ * SoftReferences, and will reload as needed, in cases where memory sizes are important.<br>
+ *
+ * @see org.apache.commons.bcel6.Repository
+ */
+public class MemorySensitiveClassPathRepository implements Repository {
+
+    private ClassPath _path = null;
+    private final Map<String, SoftReference<JavaClass>> _loadedClasses = new
HashMap<>(); // CLASSNAME X JAVACLASS
+
+    public MemorySensitiveClassPathRepository(ClassPath path) {
+        _path = path;
+    }
+
+    /**
+     * Store a new JavaClass instance into this Repository.
+     */
+    @Override
+    public void storeClass(JavaClass clazz) {
+        _loadedClasses.put(clazz.getClassName(), new SoftReference<>(clazz));
+        clazz.setRepository(this);
+    }
+
+    /**
+     * Remove class from repository
+     */
+    @Override
+    public void removeClass(JavaClass clazz) {
+        _loadedClasses.remove(clazz.getClassName());
+    }
+
+    /**
+     * Find an already defined (cached) JavaClass object by name.
+     */
+    @Override
+    public JavaClass findClass(String className) {
+        SoftReference<JavaClass> ref = _loadedClasses.get(className);
+        if (ref == null) {
+            return null;
+        }
+        return ref.get();
+    }
+
+    /**
+     * Find a JavaClass object by name. If it is already in this Repository, the Repository
version is returned. Otherwise, the Repository's classpath is
+     * searched for the class (and it is added to the Repository if found).
+     *
+     * @param className
+     *            the name of the class
+     * @return the JavaClass object
+     * @throws ClassNotFoundException
+     *             if the class is not in the Repository, and could not be found on the classpath
+     */
+    @Override
+    public JavaClass loadClass(String className) throws ClassNotFoundException {
+        if ((className == null) || className.equals("")) {
+            throw new IllegalArgumentException("Invalid class name " + className);
+        }
+        className = className.replace('/', '.'); // Just in case, canonical form
+        JavaClass clazz = findClass(className);
+        if (clazz != null) {
+            return clazz;
+        }
+        try {
+            return loadClass(_path.getInputStream(className), className);
+        } catch (IOException e) {
+            throw new ClassNotFoundException("Exception while looking for class " + className
+ ": " + e, e);
+        }
+    }
+
+    /**
+     * Find the JavaClass object for a runtime Class object. If a class with the same name
is already in this Repository, the Repository version is returned.
+     * Otherwise, getResourceAsStream() is called on the Class object to find the class's
representation. If the representation is found, it is added to the
+     * Repository.
+     *
+     * @see Class
+     * @param clazz
+     *            the runtime Class object
+     * @return JavaClass object for given runtime class
+     * @throws ClassNotFoundException
+     *             if the class is not in the Repository, and its representation could not
be found
+     */
+    @Override
+    public JavaClass loadClass(Class<?> clazz) throws ClassNotFoundException {
+        InputStream clsStream = null;
+        try {
+            String className = clazz.getName();
+            JavaClass repositoryClass = findClass(className);
+            if (repositoryClass != null) {
+                return repositoryClass;
+            }
+            String name = className;
+            int i = name.lastIndexOf('.');
+            if (i > 0) {
+                name = name.substring(i + 1);
+            }
+            clsStream = clazz.getResourceAsStream(name + ".class");
+            return loadClass(clsStream, className);
+        } finally {
+            try {
+                if (clsStream != null) {
+                    clsStream.close();
+                }
+            } catch (IOException ioe) {
+                // don't care
+            }
+        }
+    }
+
+    private JavaClass loadClass(InputStream is, String className) throws ClassNotFoundException
{
+        try {
+            if (is != null) {
+                ClassParser parser = new ClassParser(is, className);
+                JavaClass clazz = parser.parse();
+                storeClass(clazz);
+                return clazz;
+            }
+        } catch (IOException e) {
+            throw new ClassNotFoundException("Exception while looking for class " + className
+ ": " + e, e);
+        } finally {
+            if (is != null) {
+                try {
+                    is.close();
+                } catch (IOException e) {
+                    // ignored
+                }
+            }
+        }
+        throw new ClassNotFoundException("SyntheticRepository could not load " + className);
+    }
+
+    /**
+     * ClassPath associated with the Repository.
+     */
+    @Override
+    public ClassPath getClassPath() {
+        return _path;
+    }
+
+    /**
+     * Clear all entries from cache.
+     */
+    @Override
+    public void clear() {
+        _loadedClasses.clear();
+    }
+}

Modified: commons/proper/bcel/trunk/src/main/java/org/apache/commons/bcel6/util/SyntheticRepository.java
URL: http://svn.apache.org/viewvc/commons/proper/bcel/trunk/src/main/java/org/apache/commons/bcel6/util/SyntheticRepository.java?rev=1745933&r1=1745932&r2=1745933&view=diff
==============================================================================
--- commons/proper/bcel/trunk/src/main/java/org/apache/commons/bcel6/util/SyntheticRepository.java
(original)
+++ commons/proper/bcel/trunk/src/main/java/org/apache/commons/bcel6/util/SyntheticRepository.java
Sat May 28 20:31:42 2016
@@ -17,48 +17,33 @@
  */
 package org.apache.commons.bcel6.util;
 
-import java.io.IOException;
-import java.io.InputStream;
-import java.lang.ref.SoftReference;
 import java.util.HashMap;
 import java.util.Map;
 
-import org.apache.commons.bcel6.classfile.ClassParser;
-import org.apache.commons.bcel6.classfile.JavaClass;
-
 /**
- * This repository is used in situations where a Class is created
- * outside the realm of a ClassLoader. Classes are loaded from
- * the file systems using the paths specified in the given
- * class path. By default, this is the value returned by
- * ClassPath.getClassPath().
- * <br>
- * It is designed to be used as a singleton, however it
- * can also be used with custom classpaths.
+ * This repository is used in situations where a Class is created outside the realm of a
ClassLoader. Classes are loaded from the file systems using the paths
+ * specified in the given class path. By default, this is the value returned by ClassPath.getClassPath().
<br>
+ * This repository uses a factory design, allowing it to maintain a collection of different
classpaths, and as such It is designed to be used as a singleton per
+ * classpath.
  *
  * @see org.apache.commons.bcel6.Repository
  *
  * @version $Id$
  */
-public class SyntheticRepository implements Repository {
+public class SyntheticRepository extends MemorySensitiveClassPathRepository implements Repository
{
 
-    //private static final String DEFAULT_PATH = ClassPath.getClassPath();
+    // private static final String DEFAULT_PATH = ClassPath.getClassPath();
     private static final Map<ClassPath, SyntheticRepository> _instances = new HashMap<>();
// CLASSPATH X REPOSITORY
-    private ClassPath _path = null;
-    private final Map<String, SoftReference<JavaClass>> _loadedClasses = new
HashMap<>(); // CLASSNAME X JAVACLASS
-
 
     private SyntheticRepository(ClassPath path) {
-        _path = path;
+        super(path);
     }
 
-
     public static SyntheticRepository getInstance() {
         return getInstance(ClassPath.SYSTEM_CLASS_PATH);
     }
 
-
-    public static SyntheticRepository getInstance( ClassPath classPath ) {
+    public static SyntheticRepository getInstance(ClassPath classPath) {
         SyntheticRepository rep = _instances.get(classPath);
         if (rep == null) {
             rep = new SyntheticRepository(classPath);
@@ -66,147 +51,4 @@ public class SyntheticRepository impleme
         }
         return rep;
     }
-
-
-    /**
-     * Store a new JavaClass instance into this Repository.
-     */
-    @Override
-    public void storeClass( JavaClass clazz ) {
-        _loadedClasses.put(clazz.getClassName(), new SoftReference<>(clazz));
-        clazz.setRepository(this);
-    }
-
-
-    /**
-     * Remove class from repository
-     */
-    @Override
-    public void removeClass( JavaClass clazz ) {
-        _loadedClasses.remove(clazz.getClassName());
-    }
-
-
-    /**
-     * Find an already defined (cached) JavaClass object by name.
-     */
-    @Override
-    public JavaClass findClass( String className ) {
-        SoftReference<JavaClass> ref = _loadedClasses.get(className);
-        if (ref == null) {
-            return null;
-        }
-        return ref.get();
-    }
-
-
-    /**
-     * Find a JavaClass object by name.
-     * If it is already in this Repository, the Repository version
-     * is returned.  Otherwise, the Repository's classpath is searched for
-     * the class (and it is added to the Repository if found).
-     *
-     * @param className the name of the class
-     * @return the JavaClass object
-     * @throws ClassNotFoundException if the class is not in the
-     *   Repository, and could not be found on the classpath
-     */
-    @Override
-    public JavaClass loadClass( String className ) throws ClassNotFoundException {
-        if (className == null || className.equals("")) {
-            throw new IllegalArgumentException("Invalid class name " + className);
-        }
-        className = className.replace('/', '.'); // Just in case, canonical form
-        JavaClass clazz = findClass(className);
-        if (clazz != null) {
-            return clazz;
-        }
-        try {
-            return loadClass(_path.getInputStream(className), className);
-        } catch (IOException e) {
-            throw new ClassNotFoundException("Exception while looking for class " + className
-                    + ": " + e, e);
-        }
-    }
-
-
-    /**
-     * Find the JavaClass object for a runtime Class object.
-     * If a class with the same name is already in this Repository,
-     * the Repository version is returned.  Otherwise, getResourceAsStream()
-     * is called on the Class object to find the class's representation.
-     * If the representation is found, it is added to the Repository.
-     *
-     * @see Class
-     * @param clazz the runtime Class object
-     * @return JavaClass object for given runtime class
-     * @throws ClassNotFoundException if the class is not in the
-     *   Repository, and its representation could not be found
-     */
-    @Override
-    public JavaClass loadClass( Class<?> clazz ) throws ClassNotFoundException {
-        InputStream clsStream = null;
-        try{
-            String className = clazz.getName();
-            JavaClass repositoryClass = findClass(className);
-            if (repositoryClass != null) {
-                return repositoryClass;
-            }
-            String name = className;
-            int i = name.lastIndexOf('.');
-            if (i > 0) {
-                name = name.substring(i + 1);
-            }
-            clsStream = clazz.getResourceAsStream(name + ".class");
-            return loadClass(clsStream, className);
-        } finally {
-            try{
-                if (clsStream != null){
-                    clsStream.close();
-                }
-            } catch(IOException ioe){
-                //don't care
-            }
-        }
-    }
-
-
-    private JavaClass loadClass( InputStream is, String className ) throws ClassNotFoundException
{
-        try {
-            if (is != null) {
-                ClassParser parser = new ClassParser(is, className);
-                JavaClass clazz = parser.parse();
-                storeClass(clazz);
-                return clazz;
-            }
-        } catch (IOException e) {
-            throw new ClassNotFoundException("Exception while looking for class " + className
-                    + ": " + e, e);
-        } finally {
-            if (is != null){
-                try {
-                    is.close();
-                } catch (IOException e) {
-                    // ignored
-                }
-            }
-        }
-        throw new ClassNotFoundException("SyntheticRepository could not load " + className);
-    }
-
-
-    /** ClassPath associated with the Repository.
-     */
-    @Override
-    public ClassPath getClassPath() {
-        return _path;
-    }
-
-
-    /** Clear all entries from cache.
-     */
-    @Override
-    public void clear() {
-        _loadedClasses.clear();
-    }
 }



Mime
View raw message