This is the mail archive of the
kawa@sources.redhat.com
mailing list for the Kawa project.
Re: bug in invoke + reflection patch/improvement
- To: kawa at sourceware dot cygnus dot com
- Subject: Re: bug in invoke + reflection patch/improvement
- From: Martin Atzmueller <martin dot atzmueller at gmx dot net>
- Date: Fri, 25 Aug 2000 15:21:46 +0200 (MEST)
- References: <30827.967209519@www2.gmx.net>
>
> A bug in your ReflectionHelper class:
[ snip ]
> It seems to me that isJava2 = true is executed in any case. This
> statement
> should go just after the forName call.
>
> BTW, the javadoc comment should have </code> and not </true> ! :-)
yes, absolutely right, thank you very much.
I've attached the new ReflectionHelper class.
(BTW, I couldn't compile Kawa with JDK1.1, due to Swing, so this issue is
not that relevant, ...)
Cheers,
Martin Atzmueller
--
Homepage: http://www.atzmueller.net/
Email: martin@atzmueller.net
Sent through GMX FreeMail - http://www.gmx.net
--
Homepage: http://www.atzmueller.net/
Email: martin@atzmueller.net
Sent through GMX FreeMail - http://www.gmx.net
package gnu.kawa.reflect;
import java.lang.reflect.*;
import java.util.Vector;
import java.util.Hashtable;
/** This Class improves java-reflection access to both methods and
fields.
* In versions of Java prior to Java 2, only public classes and members
* can be accessed reflectively. In Java 2, nonpublic classes and members
* can be accessed if the security manager allows it, and this feature is
* explicitly requested.
*
*
* Using this helper class, especially the 'enableAccess'-method,
non-public slots
* methods are made accessible.
* The private-access features are only available in the
"Runtime"-Kawa-Environment,
* e.g. when working interactively.
* It does not work in compiled code.
*/
public final class ReflectionHelper {
/** True if the JDK implements Java 2 or above. If this is
<code>true</code>, then non-public classes and members can be accessed.
*/
public static boolean isJava2;
static {
try {
Class.forName("java.lang.reflect.AccessibleObject");
isJava2 = true;
} catch (ClassNotFoundException ex) {
isJava2 = false;
}
}
/** Override Java-Access-control, if possible.*/
public static void enableAccess( Object obj ) {
setAccessible( obj, true );
}
// going thru reflection so this can be compiled using JDK1.1.
static Method setAccessibleMethod = null;
private static void setAccessible(Object obj, boolean accessible) {
try {
if (setAccessibleMethod == null) {
Class aclass =
Class.forName("java.lang.reflect.AccessibleObject");
setAccessibleMethod =
aclass.getMethod("setAccessible", new Class[]{
Boolean.TYPE });
}
setAccessibleMethod.invoke(obj, new Object[]{ Boolean.TRUE
});
}
catch (Throwable e) {
System.out.println("Error trying to set accessibility for " +
obj);
}
}
public static java.lang.reflect.Field getField( Class clas, String
fname ) {
java.lang.reflect.Field field;
try {
field = clas.getField(fname);
}
catch ( NoSuchFieldException ex ) {
if ( isJava2 ) {
// try harder, using all declared fields.
java.lang.reflect.Field[] localFields = clas.getDeclaredFields();
for (int i = 0; i != localFields.length; i++) {
if (fname.equals(localFields[i].getName())) {
field = localFields[i];
ReflectionHelper.enableAccess( field );
return field;
}
}
// if not found, go up to superclass.
Class superClass = clas.getSuperclass();
if (superClass == null)
field = null;
else
return getField(superClass, fname);
}
else
field = null;
}
catch (Exception ex)
{
field = null;
}
return field;
}
}