[Arakhnę-Dev] [37] Add class RelfectionUtil and deprecate class AutoboxingUtil. |
[ Thread Index |
Date Index
| More arakhne.org/dev Archives
]
Revision: 37
Author: galland
Date: 2009-03-10 11:17:48 +0100 (Tue, 10 Mar 2009)
Log Message:
-----------
Add class RelfectionUtil and deprecate class AutoboxingUtil.
Modified Paths:
--------------
trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/AutoboxingUtil.java
trunk/arakhneVmutils/java/src/main/resources/Changelog
trunk/arakhneVmutils/java/src/main/resources/VERSION
trunk/pom.xml
Added Paths:
-----------
trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/ReflectionUtil.java
Modified: trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/AutoboxingUtil.java
===================================================================
--- trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/AutoboxingUtil.java 2009-03-05 14:06:11 UTC (rev 36)
+++ trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/AutoboxingUtil.java 2009-03-10 10:17:48 UTC (rev 37)
@@ -28,7 +28,9 @@
* @author Stéphane GALLAND <galland@xxxxxxxxxxx>
* @version $Name$ $Revision$ $Date$
* @since since JDK 1.5
+ * @deprecated see {@link ReflectionUtil}
*/
+@Deprecated
public class AutoboxingUtil {
/**
Added: trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/ReflectionUtil.java
===================================================================
--- trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/ReflectionUtil.java (rev 0)
+++ trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/ReflectionUtil.java 2009-03-10 10:17:48 UTC (rev 37)
@@ -0,0 +1,336 @@
+/*
+ * $Id$
+ *
+ * Copyright (C) 2004-2008 Stéphane GALLAND
+ *
+ * This library 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 library 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 library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ * This program is free software; you can redistribute it and/or modify
+ */
+
+package org.arakhne.vmutil;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Enumeration;
+import java.util.jar.JarEntry;
+import java.util.jar.JarFile;
+
+/**
+ * This utility class provides a way to extend the reflection API and
+ * the Class class with autoboxing-compliant functions.
+ *
+ * @author Stéphane GALLAND <galland@xxxxxxxxxxx>
+ * @version $Name$ $Revision$ $Date$
+ * @since since JDK 1.5
+ */
+public class ReflectionUtil {
+
+ /**
+ * Determines if the specified <code>Object</code> is assignment-compatible
+ * with the object represented by the <code>Class</code>. This method extends
+ * {@link Class#isInstance(Object)} with autoboxing support.
+ *
+ * @param type is the class against which the object must be test
+ * @param obj is the object to check
+ * @return <code>true</code> if <code>obj</code> is an instance of the type
+ * @see Class#isInstance(Object)
+ */
+ public static boolean isInstance(Class<?> type, Object obj) {
+ assert(type!=null);
+ if (obj==null) return false;
+
+ // Test according to the Class's behaviour
+ if (type.isInstance(obj)) return true;
+
+ // Test according to autoboxing
+ if (type.isPrimitive()
+ &&
+ type!=Void.class && type!=void.class) {
+
+ if (type==Boolean.class) return boolean.class.isInstance(obj);
+ if (type==boolean.class) return Boolean.class.isInstance(obj);
+
+ if (type==Character.class) return char.class.isInstance(obj);
+ if (type==char.class) return Character.class.isInstance(obj);
+
+ if (type==Byte.class) return byte.class.isInstance(obj);
+ if (type==byte.class) return Byte.class.isInstance(obj);
+
+ if (type==Short.class) return short.class.isInstance(obj);
+ if (type==short.class) return Short.class.isInstance(obj);
+
+ if (type==Integer.class) return int.class.isInstance(obj);
+ if (type==int.class) return Integer.class.isInstance(obj);
+
+ if (type==Long.class) return long.class.isInstance(obj);
+ if (type==long.class) return Long.class.isInstance(obj);
+
+ if (type==Float.class) return float.class.isInstance(obj);
+ if (type==float.class) return Float.class.isInstance(obj);
+
+ if (type==Double.class) return double.class.isInstance(obj);
+ if (type==double.class) return Double.class.isInstance(obj);
+
+ if (type==Void.class) return void.class.isInstance(obj);
+ if (type==void.class) return Void.class.isInstance(obj);
+
+ assert false: "Unsupported primitive type"; //$NON-NLS-1$
+ }
+
+ return false;
+ }
+
+
+ /**
+ * Determines if the <code>assignmentTarget</code> object is either the same as,
+ * or is a superclass or superinterface of, the class or interface
+ * represented by the specified
+ * <code>assignementSource</code> parameter. This method extends
+ * {@link Class#isAssignableFrom(Class)} with autoboxing support.
+ *
+ * @param assignementTarget is the class that is tested to be a super class.
+ * @param assignementSource is the class that is tested to be a sub class.
+ * @return <code>true</code> if an object of the <var>assignementSource</var> type
+ * could be assigned to a variable of <var>assignementTarget</var> type,
+ * otherwise <code>false</code>.
+ */
+ public static boolean isAssignableFrom(Class<?> assignementTarget, Class<?> assignementSource) {
+ assert(assignementSource!=null);
+ assert(assignementTarget!=null);
+
+ // Test according to the Class's behaviour
+ if (assignementTarget.isAssignableFrom(assignementSource)) return true;
+
+ // Test according to autoboxing
+ if (assignementTarget.isPrimitive() && assignementSource.isPrimitive()
+ &&
+ assignementTarget!=Void.class && assignementTarget!=void.class
+ &&
+ assignementSource!=Void.class && assignementSource!=void.class) {
+ return true;
+ }
+
+ return false;
+ }
+
+ /** Replies the type that corresponds to the specified class.
+ * If the name corresponds to a primitive type, the low-level type
+ * will be replied.
+ * This method extends
+ * {@link Class#forName(String)} with autoboxing support.
+ *
+ * @param name is the name of the class to load.
+ * @return the loaded class
+ * @throws ClassNotFoundException if name names an
+ * unknown class or primitive
+ */
+ public static Class<?> forName(String name) throws ClassNotFoundException {
+ if (name == null || "".equals(name) || "null".equals(name) || "void".equals(name)) { //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
+ return void.class;
+ }
+ if ("boolean".equals(name)) { //$NON-NLS-1$
+ return boolean.class;
+ }
+ if ("byte".equals(name)) { //$NON-NLS-1$
+ return byte.class;
+ }
+ if ("char".equals(name)) { //$NON-NLS-1$
+ return char.class;
+ }
+ if ("double".equals(name)) { //$NON-NLS-1$
+ return double.class;
+ }
+ if ("float".equals(name)) { //$NON-NLS-1$
+ return float.class;
+ }
+ if ("int".equals(name)) { //$NON-NLS-1$
+ return int.class;
+ }
+ if ("long".equals(name)) { //$NON-NLS-1$
+ return long.class;
+ }
+ if ("short".equals(name)) { //$NON-NLS-1$
+ return short.class;
+ }
+ return Class.forName(name);
+ }
+
+ /** Replies the type that corresponds to the specified class.
+ * If the name corresponds to a primitive type, the low-level type
+ * will be replied.
+ * This method extends
+ * {@link Class#forName(String)} with autoboxing support.
+ *
+ * @param name is the name of the class to load.
+ * @param loader is the class loader to use.
+ * @return the loaded class
+ * @throws ClassNotFoundException if name names an
+ * unknown class or primitive
+ */
+ public static Class<?> forName(String name, ClassLoader loader) throws ClassNotFoundException {
+ return forName(name, true, loader);
+ }
+
+ /** Replies the type that corresponds to the specified class.
+ * If the name corresponds to a primitive type, the low-level type
+ * will be replied.
+ * This method extends
+ * {@link Class#forName(String)} with autoboxing support.
+ *
+ * @param name is the name of the class to load.
+ * @param typeInitialization must be <code>true</code> to initialize the type, <code>false</code> otherwise.
+ * @param loader is the class loader to use.
+ * @return the loaded class
+ * @throws ClassNotFoundException if name names an
+ * unknown class or primitive
+ */
+ public static Class<?> forName(String name, boolean typeInitialization, ClassLoader loader) throws ClassNotFoundException {
+ if (name == null || "".equals(name) || "null".equals(name) || "void".equals(name)) { //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
+ return void.class;
+ }
+ if ("boolean".equals(name)) { //$NON-NLS-1$
+ return boolean.class;
+ }
+ if ("byte".equals(name)) { //$NON-NLS-1$
+ return byte.class;
+ }
+ if ("char".equals(name)) { //$NON-NLS-1$
+ return char.class;
+ }
+ if ("double".equals(name)) { //$NON-NLS-1$
+ return double.class;
+ }
+ if ("float".equals(name)) { //$NON-NLS-1$
+ return float.class;
+ }
+ if ("int".equals(name)) { //$NON-NLS-1$
+ return int.class;
+ }
+ if ("long".equals(name)) { //$NON-NLS-1$
+ return long.class;
+ }
+ if ("short".equals(name)) { //$NON-NLS-1$
+ return short.class;
+ }
+ return Class.forName(name, typeInitialization, loader);
+ }
+
+ /**
+ * Replies the list of the classes in the given package.
+ * @param pkg is the package to explore.
+ * @return the list of classes in the package.
+ */
+ public static Collection<Class<?>> getPackageClasses(Package pkg) {
+ return getPackageClasses(pkg.getName());
+ }
+
+ /**
+ * Replies the list of the classes in the given package.
+ * @param packageName is the name of the package to explore.
+ * @return the list of classes in the package.
+ */
+ public static Collection<Class<?>> getPackageClasses(String packageName) {
+ Collection<Class<?>> classes = new ArrayList<Class<?>>();
+
+ String[] entries = System.getProperty("java.class.path").split(System.getProperty("path.separator")); //$NON-NLS-1$//$NON-NLS-2$
+ String lentry;
+
+ for(String path : entries) {
+ lentry = path.toLowerCase();
+ if(lentry.endsWith(".jar") || lentry.endsWith(".war")) { //$NON-NLS-1$//$NON-NLS-2$
+ getPackageClassesFromJar(classes, path, packageName);
+ }else{
+ getPackageClassesFromFileSystem(classes, path, packageName);
+ }
+
+ }
+
+ return classes;
+ }
+
+ private static String basename(String name) {
+ int idx = name.lastIndexOf('/');
+ if (idx>=0 && idx<name.length()) {
+ return name.substring(idx+1);
+ }
+ return name;
+ }
+
+ private static String filename(String name) {
+ String basename = basename(name);
+ int idx = basename.indexOf('.');
+ if (idx>=0 && idx<basename.length()) {
+ return basename.substring(0,idx);
+ }
+ return name;
+ }
+
+ private static void getPackageClassesFromJar(Collection<Class<?>> classes, String jarFilename, String packageName) {
+ try {
+ JarFile jarFile = new JarFile(jarFilename);
+ String packagePath = packageName.replace(".", "/"); //$NON-NLS-1$//$NON-NLS-2$
+
+ Enumeration<JarEntry> entries = jarFile.entries();
+ JarEntry entry;
+ String entryPath, entryClassname;
+
+ while (entries.hasMoreElements()) {
+ entry = entries.nextElement();
+ entryPath = entry.getName();
+
+ // In package and not inner class
+ if (entryPath.startsWith(packagePath)
+ && !entryPath.endsWith("/") //$NON-NLS-1$
+ && !entryPath.contains("$")) { //$NON-NLS-1$
+ entryClassname = packageName + "." + filename(entryPath); //$NON-NLS-1$
+ try {
+ classes.add(Class.forName(entryClassname));
+ }
+ catch(ClassNotFoundException _) {
+ //
+ }
+ }
+ }
+ }
+ catch(IOException _) {
+ //
+ }
+ }
+
+ private static void getPackageClassesFromFileSystem(Collection<Class<?>> classes, String directory, String packageName) {
+ String packagePath = packageName.replace(".", File.separator); //$NON-NLS-1$
+ File packageDirectory = new File(directory, packagePath);
+ String entryClassname;
+
+ if (packageDirectory.isDirectory()) {
+ for(String entryPath : packageDirectory.list()) {
+
+ // In package and not inner class
+ if (!entryPath.contains("$")) { //$NON-NLS-1$
+ entryClassname = packageName + "." + FileSystem.shortBasename(entryPath); //$NON-NLS-1$
+ try {
+ classes.add(Class.forName(entryClassname));
+ }
+ catch(Throwable _) {
+ //
+ }
+ }
+ }
+ }
+ }
+
+}
Modified: trunk/arakhneVmutils/java/src/main/resources/Changelog
===================================================================
--- trunk/arakhneVmutils/java/src/main/resources/Changelog 2009-03-05 14:06:11 UTC (rev 36)
+++ trunk/arakhneVmutils/java/src/main/resources/Changelog 2009-03-10 10:17:48 UTC (rev 37)
@@ -1,3 +1,16 @@
+arakhneVmutils-3.0
+
+* Add class ReflectionUtil.
+
+-- Stephane GALLAND <galland@xxxxxxxxxxx> Tue, 10 Mar 2009 10:51:07 +0100
+
+arakhneVmutils-2.2
+
+* Bug fix: the FileSystem#split(File) function should not use the
+ absolute path of the given file.
+
+-- Stephane GALLAND <galland@xxxxxxxxxxx> Thu, 05 Mar 2009 15:08:29 +0100
+
arakhneVmutils-2.1
* HAL changed the name of the properties where the serial number and
Modified: trunk/arakhneVmutils/java/src/main/resources/VERSION
===================================================================
--- trunk/arakhneVmutils/java/src/main/resources/VERSION 2009-03-05 14:06:11 UTC (rev 36)
+++ trunk/arakhneVmutils/java/src/main/resources/VERSION 2009-03-10 10:17:48 UTC (rev 37)
@@ -1 +1 @@
-arakhneVmutils 2.1
+arakhneVmutils 3.0
Modified: trunk/pom.xml
===================================================================
--- trunk/pom.xml 2009-03-05 14:06:11 UTC (rev 36)
+++ trunk/pom.xml 2009-03-10 10:17:48 UTC (rev 37)
@@ -19,7 +19,7 @@
<version_myjdk>1.6</version_myjdk>
<version_arakhnelogger>1.1-SNAPSHOT</version_arakhnelogger>
<version_arakhnerefs>5.1-SNAPSHOT</version_arakhnerefs>
- <version_arakhnevmutils>2.1-SNAPSHOT</version_arakhnevmutils>
+ <version_arakhnevmutils>3.0-SNAPSHOT</version_arakhnevmutils>
</properties>
<dependencyManagement>