For now don't use the generated field accessors unless we're running in a

sandbox. They should work exactly the same but there seem to be remaining
niggles, so we'll iron those out without impacting other projects.


git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@4706 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Michael Bayne
2007-05-09 21:52:47 +00:00
parent 84855f8fe0
commit 96dd2deba3
+38 -18
View File
@@ -23,6 +23,7 @@ package com.threerings.io;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.lang.reflect.ReflectPermission;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
@@ -57,23 +58,25 @@ public abstract class FieldMarshaller
createMarshallers(); createMarshallers();
} }
// first look to see if this field has custom reader/writer methods // if necessary (we're running in a sandbox), look for custom field accessors
Method reader = null, writer = null; if (useFieldAccessors()) {
try { Method reader = null, writer = null;
reader = field.getDeclaringClass().getMethod( try {
getReaderMethodName(field.getName()), READER_ARGS); reader = field.getDeclaringClass().getMethod(
writer = field.getDeclaringClass().getMethod( getReaderMethodName(field.getName()), READER_ARGS);
getWriterMethodName(field.getName()), WRITER_ARGS); writer = field.getDeclaringClass().getMethod(
return new MethodFieldMarshaller(reader, writer); getWriterMethodName(field.getName()), WRITER_ARGS);
} catch (NoSuchMethodException nsme) { return new MethodFieldMarshaller(reader, writer);
// no problem } catch (NoSuchMethodException nsme) {
} // no problem
if ((reader != null || writer != null) && (reader == null || writer == null)) { }
log.warning("Class contains one but not both custom field reader and writer " + if ((reader != null || writer != null) && (reader == null || writer == null)) {
"[class=" + field.getDeclaringClass().getName() + log.warning("Class contains one but not both custom field reader and writer " +
", field=" + field.getName() + ", reader=" + reader + "[class=" + field.getDeclaringClass().getName() +
", writer=" + writer + "]."); ", field=" + field.getName() + ", reader=" + reader +
// fall through to using reflection on the fields... ", writer=" + writer + "].");
// fall through to using reflection on the fields...
}
} }
Class ftype = field.getType(); Class ftype = field.getType();
@@ -111,6 +114,23 @@ public abstract class FieldMarshaller
return "writeField_" + field; return "writeField_" + field;
} }
/**
* Returns true if we should use the generated field marshaller methods that allow us to work
* around our inability to read and write protected and private fields of a {@link Streamable}.
*/
protected static boolean useFieldAccessors ()
{
try {
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkPermission(new ReflectPermission("suppressAccessChecks"));
}
return false;
} catch (SecurityException se) {
return true;
}
}
/** /**
* Used to marshall and unmarshall classes for which we have a basic {@link Streamer}. * Used to marshall and unmarshall classes for which we have a basic {@link Streamer}.
*/ */
@@ -157,7 +177,7 @@ public abstract class FieldMarshaller
} }
/** /**
* Uses custom reader and writer methods to read and write a field. * Uses custom accessor methods to read and write a field.
*/ */
protected static class MethodFieldMarshaller extends FieldMarshaller protected static class MethodFieldMarshaller extends FieldMarshaller
{ {