Author: mturk Date: Mon Apr 11 15:21:11 2011 New Revision: 1091099 URL: http://svn.apache.org/viewvc?rev=1091099&view=rev Log: Add reflection helper classes
Added: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/ReflectedClass.java (with props) commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/ReflectedField.java (with props) commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/ReflectedFieldImpl.java (with props) commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/ReflectedMethod.java (with props) commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/ReflectedMethodImpl.java (with props) Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Reflect.java commons/sandbox/runtime/trunk/src/main/native/shared/reflect.c commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestReflect.java Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Reflect.java URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Reflect.java?rev=1091099&r1=1091098&r2=1091099&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Reflect.java (original) +++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Reflect.java Mon Apr 11 15:21:11 2011 @@ -24,7 +24,7 @@ import java.lang.reflect.Method; * @since Runtime 1.0 * */ -public final class Reflect +class Reflect { private Reflect() @@ -32,22 +32,18 @@ public final class Reflect // No instance. } - private static native Class get0(String name) + private static native Class findClass0(String name) throws NoClassDefFoundError, OutOfMemoryError; - private static native Class get1(String name, boolean weakReference) - throws NoClassDefFoundError, OutOfMemoryError; - private static native long get2(Class clazz, String name, String signature, boolean isStatic) + private static native long getMethod0(Class clazz, String name, String signature, boolean isStatic) throws NoSuchMethodError, OutOfMemoryError; - private static native long get3(Class clazz, String name, String signature, boolean isStatic) + private static native long getField0(Class clazz, String name, String signature, boolean isStatic) throws NoSuchFieldError, OutOfMemoryError; - private static native Method get4(long methodID) + private static native Method toMethod0(Class clazz, long methodID, boolean isStatic) throws OutOfMemoryError; - private static native Field get5(long fieldID) + private static native Field toField0(Class clazz, long fieldID, boolean isStatic) throws OutOfMemoryError; - private static native Object new0(Class clazz) + private static native Object allocObject0(Class clazz) throws InstantiationException, OutOfMemoryError; - private static native void clr0(Object inst); - private static native void clr1(Object inst); /** * Allocates a new java.lang.Object without invoking any @@ -58,7 +54,7 @@ public final class Reflect { if (clazz == null) throw new IllegalArgumentException(); - return new0(clazz); + return allocObject0(clazz); } public static Class findClass(String name) @@ -66,7 +62,47 @@ public final class Reflect { if (name == null) throw new IllegalArgumentException(); - return get0(name); + return findClass0(name); + } + + public static long getConstructorID(Class clazz, String signature) + throws IllegalArgumentException, NoSuchMethodError, OutOfMemoryError + { + if (clazz == null || signature == null) + throw new IllegalArgumentException(); + return getMethod0(clazz, "<init>", signature, false); + } + + public static long getMethodID(Class clazz, String name, String signature, boolean isStatic) + throws IllegalArgumentException, NoSuchMethodError, OutOfMemoryError + { + if (clazz == null || name == null || signature == null) + throw new IllegalArgumentException(); + return getMethod0(clazz, name, signature, isStatic); + } + + public static long getFieldID(Class clazz, String name, String signature, boolean isStatic) + throws IllegalArgumentException, NoSuchFieldError, OutOfMemoryError + { + if (clazz == null || name == null || signature == null) + throw new IllegalArgumentException(); + return getField0(clazz, name, signature, isStatic); + } + + public static Method toMethod(Class clazz, long methodID, boolean isStatic) + throws IllegalArgumentException, OutOfMemoryError + { + if (clazz == null || methodID == 0) + throw new IllegalArgumentException(); + return toMethod0(clazz, methodID, isStatic); } - + + public static Field toField(Class clazz, long fieldID, boolean isStatic) + throws IllegalArgumentException, OutOfMemoryError + { + if (clazz == null || fieldID == 0) + throw new IllegalArgumentException(); + return toField0(clazz, fieldID, isStatic); + } + } Added: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/ReflectedClass.java URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/ReflectedClass.java?rev=1091099&view=auto ============================================================================== --- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/ReflectedClass.java (added) +++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/ReflectedClass.java Mon Apr 11 15:21:11 2011 @@ -0,0 +1,94 @@ +/* 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.runtime; +import java.lang.reflect.Field; +import java.lang.reflect.Method; + +/** + * JVM reflection support. + * + * @since Runtime 1.0 + * + */ +public final class ReflectedClass +{ + + private Class clazz; + + private ReflectedClass() + { + // No instance. + } + + public ReflectedClass(String name) + throws IllegalArgumentException, NoClassDefFoundError, OutOfMemoryError + { + clazz = Reflect.findClass(name); + } + + public ReflectedClass(Class cls) + throws IllegalArgumentException, NoClassDefFoundError, OutOfMemoryError + { + clazz = Reflect.findClass(cls.getCanonicalName()); + } + + /** + * Allocates a new java.lang.Object without invoking any + * of the constructors for the object. + */ + public Object allocObject() + throws IllegalArgumentException, InstantiationException, OutOfMemoryError + { + return Reflect.allocObject(clazz); + } + + public ReflectedMethod getConstructor(String signature) + throws IllegalArgumentException, NoSuchMethodError, OutOfMemoryError + { + return new ReflectedMethodImpl(clazz, signature); + } + + public ReflectedMethod getMethod(String name, String signature) + throws IllegalArgumentException, NoSuchMethodError, OutOfMemoryError + { + return new ReflectedMethodImpl(clazz, name, signature, false); + } + + public ReflectedMethod getStaticMethod(String name, String signature) + throws IllegalArgumentException, NoSuchMethodError, OutOfMemoryError + { + return new ReflectedMethodImpl(clazz, name, signature, true); + } + + public ReflectedField getField(String name, String signature) + throws IllegalArgumentException, NoSuchFieldError, OutOfMemoryError + { + return new ReflectedFieldImpl(clazz, name, signature, false); + } + + public ReflectedField getStaticField(String name, String signature) + throws IllegalArgumentException, NoSuchFieldError, OutOfMemoryError + { + return new ReflectedFieldImpl(clazz, name, signature, true); + } + + @Override + public String toString() + { + return clazz.getCanonicalName(); + } +} Propchange: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/ReflectedClass.java ------------------------------------------------------------------------------ svn:eol-style = native Added: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/ReflectedField.java URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/ReflectedField.java?rev=1091099&view=auto ============================================================================== --- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/ReflectedField.java (added) +++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/ReflectedField.java Mon Apr 11 15:21:11 2011 @@ -0,0 +1,56 @@ +/* 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.runtime; +import java.lang.reflect.Field; +import java.lang.reflect.Method; + +/** + * JVM reflection support. + * + * @since Runtime 1.0 + * + */ +public abstract class ReflectedField +{ + + protected Class clazz; + protected long fieldID; + protected boolean isStatic; + + protected ReflectedField() + { + } + + public ReflectedField(Class clazz, String name, String signature, boolean isStatic) + throws IllegalArgumentException, NoSuchFieldError, OutOfMemoryError + { + this.isStatic = isStatic; + this.clazz = clazz; + fieldID = Reflect.getMethodID(clazz, name, signature, isStatic); + } + + public Field toField() + throws IllegalArgumentException, OutOfMemoryError + { + return Reflect.toField(clazz, fieldID, isStatic); + } + + public boolean isStatic() + { + return isStatic; + } +} Propchange: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/ReflectedField.java ------------------------------------------------------------------------------ svn:eol-style = native Added: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/ReflectedFieldImpl.java URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/ReflectedFieldImpl.java?rev=1091099&view=auto ============================================================================== --- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/ReflectedFieldImpl.java (added) +++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/ReflectedFieldImpl.java Mon Apr 11 15:21:11 2011 @@ -0,0 +1,38 @@ +/* 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.runtime; +import java.lang.reflect.Field; +import java.lang.reflect.Method; + +/** + * JVM reflection support. + * + * @since Runtime 1.0 + * + */ +class ReflectedFieldImpl extends ReflectedField +{ + + public ReflectedFieldImpl(Class clazz, String name, String signature, boolean isStatic) + throws IllegalArgumentException, NoSuchFieldError, OutOfMemoryError + { + this.isStatic = isStatic; + this.clazz = clazz; + fieldID = Reflect.getMethodID(clazz, name, signature, isStatic); + } + +} Propchange: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/ReflectedFieldImpl.java ------------------------------------------------------------------------------ svn:eol-style = native Added: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/ReflectedMethod.java URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/ReflectedMethod.java?rev=1091099&view=auto ============================================================================== --- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/ReflectedMethod.java (added) +++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/ReflectedMethod.java Mon Apr 11 15:21:11 2011 @@ -0,0 +1,48 @@ +/* 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.runtime; +import java.lang.reflect.Field; +import java.lang.reflect.Method; + +/** + * JVM reflection support. + * + * @since Runtime 1.0 + * + */ +public abstract class ReflectedMethod +{ + + protected Class clazz; + protected long methodID; + protected boolean isStatic; + + protected ReflectedMethod() + { + } + + public Method toMethod() + throws IllegalArgumentException, OutOfMemoryError + { + return Reflect.toMethod(clazz, methodID, isStatic); + } + + public boolean isStatic() + { + return isStatic; + } +} Propchange: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/ReflectedMethod.java ------------------------------------------------------------------------------ svn:eol-style = native Added: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/ReflectedMethodImpl.java URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/ReflectedMethodImpl.java?rev=1091099&view=auto ============================================================================== --- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/ReflectedMethodImpl.java (added) +++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/ReflectedMethodImpl.java Mon Apr 11 15:21:11 2011 @@ -0,0 +1,46 @@ +/* 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.runtime; +import java.lang.reflect.Field; +import java.lang.reflect.Method; + +/** + * JVM reflection support. + * + * @since Runtime 1.0 + * + */ +class ReflectedMethodImpl extends ReflectedMethod +{ + + public ReflectedMethodImpl(Class clazz, String signature) + throws IllegalArgumentException, NoSuchMethodError, OutOfMemoryError + { + this.clazz = clazz; + methodID = Reflect.getConstructorID(clazz, signature); + isStatic = false; + } + + public ReflectedMethodImpl(Class clazz, String name, String signature, boolean isStatic) + throws IllegalArgumentException, NoSuchMethodError, OutOfMemoryError + { + this.isStatic = isStatic; + this.clazz = clazz; + methodID = Reflect.getMethodID(clazz, name, signature, isStatic); + } + +} Propchange: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/ReflectedMethodImpl.java ------------------------------------------------------------------------------ svn:eol-style = native Modified: commons/sandbox/runtime/trunk/src/main/native/shared/reflect.c URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/reflect.c?rev=1091099&r1=1091098&r2=1091099&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/native/shared/reflect.c (original) +++ commons/sandbox/runtime/trunk/src/main/native/shared/reflect.c Mon Apr 11 15:21:11 2011 @@ -26,7 +26,7 @@ ACR_INLINE(void) _jni_classname(char *na } } -ACR_JNI_EXPORT(jobject, Reflect, get0)(JNI_STDARGS, jstring name) +ACR_JNI_EXPORT(jobject, Reflect, findClass0)(JNI_STDARGS, jstring name) { jobject c = 0; WITH_CSTR(name) { @@ -39,30 +39,11 @@ ACR_JNI_EXPORT(jobject, Reflect, get0)(J return c; } -ACR_JNI_EXPORT(jobject, Reflect, get1)(JNI_STDARGS, jstring name, - jboolean isWeak) -{ - jobject c = 0; - WITH_CSTR(name) { - _jni_classname(J2S(name)); - c = (jobject)(*_E)->FindClass(_E, J2S(name)); - if (c != 0) { - /* Can throw OutOfMemoryError */ - if (isWeak) - c = (*_E)->NewWeakGlobalRef(_E, c); - else - c = (*_E)->NewGlobalRef(_E, c); - } - } - DONE_WITH_STR(name); - return c; -} - -ACR_JNI_EXPORT(jlong, Reflect, get2)(JNI_STDARGS, - jclass clazz, - jstring name, - jstring sig, - jboolean isStatic) +ACR_JNI_EXPORT(jlong, Reflect, getMethod0)(JNI_STDARGS, + jclass clazz, + jstring name, + jstring sig, + jboolean isStatic) { jlong id = 0; @@ -79,11 +60,11 @@ ACR_JNI_EXPORT(jlong, Reflect, get2)(JNI return id; } -ACR_JNI_EXPORT(jlong, Reflect, get3)(JNI_STDARGS, - jclass clazz, - jstring name, - jstring sig, - jboolean isStatic) +ACR_JNI_EXPORT(jlong, Reflect, getField0)(JNI_STDARGS, + jclass clazz, + jstring name, + jstring sig, + jboolean isStatic) { jlong id = 0; @@ -100,44 +81,28 @@ ACR_JNI_EXPORT(jlong, Reflect, get3)(JNI return id; } -ACR_JNI_EXPORT(jobject, Reflect, get4)(JNI_STDARGS, - jclass clazz, - jlong methodId, - jboolean isStatic) +ACR_JNI_EXPORT(jobject, Reflect, toMethod0)(JNI_STDARGS, + jclass clazz, + jlong methodId, + jboolean isStatic) { return (*_E)->ToReflectedMethod(_E, clazz, J2P(methodId, jmethodID), isStatic); } -ACR_JNI_EXPORT(jobject, Reflect, get5)(JNI_STDARGS, - jclass clazz, - jlong fieldId, - jboolean isStatic) +ACR_JNI_EXPORT(jobject, Reflect, toField0)(JNI_STDARGS, + jclass clazz, + jlong fieldId, + jboolean isStatic) { return (*_E)->ToReflectedField(_E, clazz, J2P(fieldId, jfieldID), isStatic); } -ACR_JNI_EXPORT(jint, Reflect, get6)(JNI_STDARGS, - jobject obj) +ACR_JNI_EXPORT(jint, Reflect, getRefType0)(JNI_STDARGS, jobject obj) { return (jint)(*_E)->GetObjectRefType(_E, obj); } -ACR_JNI_EXPORT(jobject, Reflect, new0)(JNI_STDARGS, - jclass clazz) +ACR_JNI_EXPORT(jobject, Reflect, allocObject0)(JNI_STDARGS, jclass clazz) { return (*_E)->AllocObject(_E, clazz); } - -ACR_JNI_EXPORT(void, Reflect, clr0)(JNI_STDARGS, jobject obj) -{ - jobject o = (*_E)->NewLocalRef(_E, obj); - if (o != 0) { - (*_E)->DeleteWeakGlobalRef(_E, obj); - (*_E)->DeleteLocalRef(_E, o); - } -} - -ACR_JNI_EXPORT(void, Reflect, clr1)(JNI_STDARGS, jobject obj) -{ - (*_E)->DeleteGlobalRef(_E, obj); -} Modified: commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestReflect.java URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestReflect.java?rev=1091099&r1=1091098&r2=1091099&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestReflect.java (original) +++ commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestReflect.java Mon Apr 11 15:21:11 2011 @@ -46,4 +46,15 @@ public class TestReflect Assert.assertEquals("", o.toString()); } + @Test(groups = { "utils" }) + public void testReflectedClass() + throws Exception + { + // Uses JNI class naming. + ReflectedClass cl = new ReflectedClass("java/lang/System"); + Assert.assertEquals("java.lang.System", cl.toString()); + ReflectedMethod m = cl.getStaticMethod("load", "(Ljava/lang/String;)V"); + Assert.assertNotNull(m); + } + }