< prev index next >

modules/fxml/src/main/java/javafx/fxml/FXMLLoader.java

Print this page
rev 9717 : 8151320: Remove unnecessary addReads from JavaFX


  60 import javafx.event.Event;
  61 import javafx.event.EventHandler;
  62 import javafx.util.Builder;
  63 import javafx.util.BuilderFactory;
  64 import javafx.util.Callback;
  65 
  66 import javax.script.Bindings;
  67 import javax.script.ScriptContext;
  68 import javax.script.ScriptEngine;
  69 import javax.script.ScriptEngineManager;
  70 import javax.script.ScriptException;
  71 import javax.script.SimpleBindings;
  72 import javax.xml.stream.XMLInputFactory;
  73 import javax.xml.stream.XMLStreamConstants;
  74 import javax.xml.stream.XMLStreamException;
  75 import javax.xml.stream.XMLStreamReader;
  76 import javax.xml.stream.util.StreamReaderDelegate;
  77 
  78 import com.sun.javafx.beans.IDProperty;
  79 import com.sun.javafx.fxml.BeanAdapter;
  80 import com.sun.javafx.fxml.ModuleHelper;
  81 import com.sun.javafx.fxml.ParseTraceElement;
  82 import com.sun.javafx.fxml.PropertyNotFoundException;
  83 import com.sun.javafx.fxml.expression.Expression;
  84 import com.sun.javafx.fxml.expression.ExpressionValue;
  85 import com.sun.javafx.fxml.expression.KeyPath;
  86 import java.net.MalformedURLException;
  87 import java.security.AccessController;
  88 import java.security.PrivilegedAction;
  89 import java.util.EnumMap;
  90 import java.util.Locale;
  91 import java.util.StringTokenizer;
  92 import sun.reflect.CallerSensitive;
  93 import sun.reflect.Reflection;
  94 import sun.reflect.misc.ConstructorUtil;
  95 import sun.reflect.misc.MethodUtil;
  96 import sun.reflect.misc.ReflectUtil;
  97 
  98 /**
  99  * Loads an object hierarchy from an XML document.
 100  * @since JavaFX 2.0


 908                     if (current.parent != null) {
 909                         throw constructLoadException(FX_NAMESPACE_PREFIX + ":" + FX_CONTROLLER_ATTRIBUTE
 910                             + " can only be applied to root element.");
 911                     }
 912 
 913                     if (controller != null) {
 914                         throw constructLoadException("Controller value already specified.");
 915                     }
 916 
 917                     if (!staticLoad) {
 918                         Class<?> type;
 919                         try {
 920                             type = getClassLoader().loadClass(value);
 921                         } catch (ClassNotFoundException exception) {
 922                             throw constructLoadException(exception);
 923                         }
 924 
 925                         try {
 926                             if (controllerFactory == null) {
 927                                 ReflectUtil.checkPackageAccess(type);
 928                                 Object thisModule = ModuleHelper.getModule(this.getClass());
 929                                 ModuleHelper.addReads(thisModule, ModuleHelper.getModule(type));
 930                                 setController(type.newInstance());
 931                             } else {
 932                                 setController(controllerFactory.call(type));
 933                             }
 934                         } catch (InstantiationException exception) {
 935                             throw constructLoadException(exception);
 936                         } catch (IllegalAccessException exception) {
 937                             throw constructLoadException(exception);
 938                         }
 939                     }
 940                 } else {
 941                     throw constructLoadException("Invalid attribute.");
 942                 }
 943             } else {
 944                 super.processAttribute(prefix, localName, value);
 945             }
 946         }
 947 
 948         public abstract Object constructValue() throws IOException;
 949     }


 993                     throw constructLoadException(exception);
 994                 }
 995 
 996                 try {
 997                     value = MethodUtil.invoke(factoryMethod, null, new Object [] {});
 998                 } catch (IllegalAccessException exception) {
 999                     throw constructLoadException(exception);
1000                 } catch (InvocationTargetException exception) {
1001                     throw constructLoadException(exception);
1002                 }
1003             } else {
1004                 value = (builderFactory == null) ? null : builderFactory.getBuilder(type);
1005 
1006                 if (value == null) {
1007                     value = DEFAULT_BUILDER_FACTORY.getBuilder(type);
1008                 }
1009 
1010                 if (value == null) {
1011                     try {
1012                         ReflectUtil.checkPackageAccess(type);
1013                         Object thisModule = ModuleHelper.getModule(this.getClass());
1014                         ModuleHelper.addReads(thisModule, ModuleHelper.getModule(type));
1015                         value = type.newInstance();
1016                     } catch (InstantiationException exception) {
1017                         throw constructLoadException(exception);
1018                     } catch (IllegalAccessException exception) {
1019                         throw constructLoadException(exception);
1020                     }
1021                 }
1022             }
1023 
1024             return value;
1025         }
1026     }
1027 
1028     // Element representing an unknown type
1029     private class UnknownTypeElement extends ValueElement {
1030         // Map type representing an unknown value
1031         @DefaultProperty("items")
1032         public class UnknownValueMap extends AbstractMap<String, Object> {
1033             private ArrayList<?> items = new ArrayList<Object>();
1034             private HashMap<String, Object> values = new HashMap<String, Object>();


1243 
1244             KeyPath path = KeyPath.parse(source);
1245             if (!Expression.isDefined(namespace, path)) {
1246                 throw constructLoadException("Value \"" + source + "\" does not exist.");
1247             }
1248 
1249             Object sourceValue = Expression.get(namespace, path);
1250             Class<?> sourceValueType = sourceValue.getClass();
1251 
1252             Constructor<?> constructor = null;
1253             try {
1254                 constructor = ConstructorUtil.getConstructor(sourceValueType, new Class[] { sourceValueType });
1255             } catch (NoSuchMethodException exception) {
1256                 // No-op
1257             }
1258 
1259             Object value;
1260             if (constructor != null) {
1261                 try {
1262                     ReflectUtil.checkPackageAccess(sourceValueType);
1263                     Object thisModule = ModuleHelper.getModule(this.getClass());
1264                     ModuleHelper.addReads(thisModule, ModuleHelper.getModule(sourceValueType));
1265                     value = constructor.newInstance(sourceValue);
1266                 } catch (InstantiationException exception) {
1267                     throw constructLoadException(exception);
1268                 } catch (IllegalAccessException exception) {
1269                     throw constructLoadException(exception);
1270                 } catch (InvocationTargetException exception) {
1271                     throw constructLoadException(exception);
1272                 }
1273             } else {
1274                 throw constructLoadException("Can't copy value " + sourceValue + ".");
1275             }
1276 
1277             return value;
1278         }
1279     }
1280 
1281     // Element representing a predefined root value
1282     private class RootElement extends ValueElement {
1283         public String type = null;
1284 


1307             }
1308 
1309             Class<?> type = getType(this.type);
1310 
1311             if (type == null) {
1312                 throw constructLoadException(this.type + " is not a valid type.");
1313             }
1314 
1315             Object value;
1316             if (root == null) {
1317                 if (staticLoad) {
1318                     value = (builderFactory == null) ? null : builderFactory.getBuilder(type);
1319 
1320                     if (value == null) {
1321                         value = DEFAULT_BUILDER_FACTORY.getBuilder(type);
1322                     }
1323 
1324                     if (value == null) {
1325                         try {
1326                             ReflectUtil.checkPackageAccess(type);
1327                             Object thisModule = ModuleHelper.getModule(this.getClass());
1328                             ModuleHelper.addReads(thisModule, ModuleHelper.getModule(type));
1329                             value = type.newInstance();
1330                         } catch (InstantiationException exception) {
1331                             throw constructLoadException(exception);
1332                         } catch (IllegalAccessException exception) {
1333                             throw constructLoadException(exception);
1334                         }
1335                     }
1336                     root = value;
1337                 } else {
1338                     throw constructLoadException("Root hasn't been set. Use method setRoot() before load.");
1339                 }
1340             } else {
1341                 if (!type.isAssignableFrom(root.getClass())) {
1342                     throw constructLoadException("Root is not an instance of "
1343                         + type.getName() + ".");
1344                 }
1345 
1346                 value = root;
1347             }
1348 


3398             if (type == Object.class) {
3399                 return;
3400             }
3401 
3402             int allowedClassAccess = prevAllowedClassAccess;
3403             int allowedMemberAccess = prevAllowedMemberAccess;
3404             if ((callerClassLoader != null)
3405                     && (type.getClassLoader() != callerClassLoader)) {
3406                 // restrict further access
3407                 allowedClassAccess &= PUBLIC;
3408                 allowedMemberAccess &= PUBLIC;
3409             }
3410 
3411             final int classAccess = getAccess(type.getModifiers());
3412             if ((classAccess & allowedClassAccess) == 0) {
3413                 // we are done
3414                 return;
3415             }
3416 
3417             ReflectUtil.checkPackageAccess(type);
3418             Object thisModule = ModuleHelper.getModule(this.getClass());
3419             ModuleHelper.addReads(thisModule, ModuleHelper.getModule(type));
3420 
3421             addAccessibleMembers(type.getSuperclass(),
3422                                  allowedClassAccess,
3423                                  allowedMemberAccess,
3424                                  membersType);
3425 
3426             final int finalAllowedMemberAccess = allowedMemberAccess;
3427             AccessController.doPrivileged(
3428                     new PrivilegedAction<Void>() {
3429                         @Override
3430                         public Void run() {
3431                             if (membersType == FIELDS) {
3432                                 addAccessibleFields(type,
3433                                                     finalAllowedMemberAccess);
3434                             } else {
3435                                 addAccessibleMethods(type,
3436                                                      finalAllowedMemberAccess);
3437                             }
3438 
3439                             return null;




  60 import javafx.event.Event;
  61 import javafx.event.EventHandler;
  62 import javafx.util.Builder;
  63 import javafx.util.BuilderFactory;
  64 import javafx.util.Callback;
  65 
  66 import javax.script.Bindings;
  67 import javax.script.ScriptContext;
  68 import javax.script.ScriptEngine;
  69 import javax.script.ScriptEngineManager;
  70 import javax.script.ScriptException;
  71 import javax.script.SimpleBindings;
  72 import javax.xml.stream.XMLInputFactory;
  73 import javax.xml.stream.XMLStreamConstants;
  74 import javax.xml.stream.XMLStreamException;
  75 import javax.xml.stream.XMLStreamReader;
  76 import javax.xml.stream.util.StreamReaderDelegate;
  77 
  78 import com.sun.javafx.beans.IDProperty;
  79 import com.sun.javafx.fxml.BeanAdapter;

  80 import com.sun.javafx.fxml.ParseTraceElement;
  81 import com.sun.javafx.fxml.PropertyNotFoundException;
  82 import com.sun.javafx.fxml.expression.Expression;
  83 import com.sun.javafx.fxml.expression.ExpressionValue;
  84 import com.sun.javafx.fxml.expression.KeyPath;
  85 import java.net.MalformedURLException;
  86 import java.security.AccessController;
  87 import java.security.PrivilegedAction;
  88 import java.util.EnumMap;
  89 import java.util.Locale;
  90 import java.util.StringTokenizer;
  91 import sun.reflect.CallerSensitive;
  92 import sun.reflect.Reflection;
  93 import sun.reflect.misc.ConstructorUtil;
  94 import sun.reflect.misc.MethodUtil;
  95 import sun.reflect.misc.ReflectUtil;
  96 
  97 /**
  98  * Loads an object hierarchy from an XML document.
  99  * @since JavaFX 2.0


 907                     if (current.parent != null) {
 908                         throw constructLoadException(FX_NAMESPACE_PREFIX + ":" + FX_CONTROLLER_ATTRIBUTE
 909                             + " can only be applied to root element.");
 910                     }
 911 
 912                     if (controller != null) {
 913                         throw constructLoadException("Controller value already specified.");
 914                     }
 915 
 916                     if (!staticLoad) {
 917                         Class<?> type;
 918                         try {
 919                             type = getClassLoader().loadClass(value);
 920                         } catch (ClassNotFoundException exception) {
 921                             throw constructLoadException(exception);
 922                         }
 923 
 924                         try {
 925                             if (controllerFactory == null) {
 926                                 ReflectUtil.checkPackageAccess(type);


 927                                 setController(type.newInstance());
 928                             } else {
 929                                 setController(controllerFactory.call(type));
 930                             }
 931                         } catch (InstantiationException exception) {
 932                             throw constructLoadException(exception);
 933                         } catch (IllegalAccessException exception) {
 934                             throw constructLoadException(exception);
 935                         }
 936                     }
 937                 } else {
 938                     throw constructLoadException("Invalid attribute.");
 939                 }
 940             } else {
 941                 super.processAttribute(prefix, localName, value);
 942             }
 943         }
 944 
 945         public abstract Object constructValue() throws IOException;
 946     }


 990                     throw constructLoadException(exception);
 991                 }
 992 
 993                 try {
 994                     value = MethodUtil.invoke(factoryMethod, null, new Object [] {});
 995                 } catch (IllegalAccessException exception) {
 996                     throw constructLoadException(exception);
 997                 } catch (InvocationTargetException exception) {
 998                     throw constructLoadException(exception);
 999                 }
1000             } else {
1001                 value = (builderFactory == null) ? null : builderFactory.getBuilder(type);
1002 
1003                 if (value == null) {
1004                     value = DEFAULT_BUILDER_FACTORY.getBuilder(type);
1005                 }
1006 
1007                 if (value == null) {
1008                     try {
1009                         ReflectUtil.checkPackageAccess(type);


1010                         value = type.newInstance();
1011                     } catch (InstantiationException exception) {
1012                         throw constructLoadException(exception);
1013                     } catch (IllegalAccessException exception) {
1014                         throw constructLoadException(exception);
1015                     }
1016                 }
1017             }
1018 
1019             return value;
1020         }
1021     }
1022 
1023     // Element representing an unknown type
1024     private class UnknownTypeElement extends ValueElement {
1025         // Map type representing an unknown value
1026         @DefaultProperty("items")
1027         public class UnknownValueMap extends AbstractMap<String, Object> {
1028             private ArrayList<?> items = new ArrayList<Object>();
1029             private HashMap<String, Object> values = new HashMap<String, Object>();


1238 
1239             KeyPath path = KeyPath.parse(source);
1240             if (!Expression.isDefined(namespace, path)) {
1241                 throw constructLoadException("Value \"" + source + "\" does not exist.");
1242             }
1243 
1244             Object sourceValue = Expression.get(namespace, path);
1245             Class<?> sourceValueType = sourceValue.getClass();
1246 
1247             Constructor<?> constructor = null;
1248             try {
1249                 constructor = ConstructorUtil.getConstructor(sourceValueType, new Class[] { sourceValueType });
1250             } catch (NoSuchMethodException exception) {
1251                 // No-op
1252             }
1253 
1254             Object value;
1255             if (constructor != null) {
1256                 try {
1257                     ReflectUtil.checkPackageAccess(sourceValueType);


1258                     value = constructor.newInstance(sourceValue);
1259                 } catch (InstantiationException exception) {
1260                     throw constructLoadException(exception);
1261                 } catch (IllegalAccessException exception) {
1262                     throw constructLoadException(exception);
1263                 } catch (InvocationTargetException exception) {
1264                     throw constructLoadException(exception);
1265                 }
1266             } else {
1267                 throw constructLoadException("Can't copy value " + sourceValue + ".");
1268             }
1269 
1270             return value;
1271         }
1272     }
1273 
1274     // Element representing a predefined root value
1275     private class RootElement extends ValueElement {
1276         public String type = null;
1277 


1300             }
1301 
1302             Class<?> type = getType(this.type);
1303 
1304             if (type == null) {
1305                 throw constructLoadException(this.type + " is not a valid type.");
1306             }
1307 
1308             Object value;
1309             if (root == null) {
1310                 if (staticLoad) {
1311                     value = (builderFactory == null) ? null : builderFactory.getBuilder(type);
1312 
1313                     if (value == null) {
1314                         value = DEFAULT_BUILDER_FACTORY.getBuilder(type);
1315                     }
1316 
1317                     if (value == null) {
1318                         try {
1319                             ReflectUtil.checkPackageAccess(type);


1320                             value = type.newInstance();
1321                         } catch (InstantiationException exception) {
1322                             throw constructLoadException(exception);
1323                         } catch (IllegalAccessException exception) {
1324                             throw constructLoadException(exception);
1325                         }
1326                     }
1327                     root = value;
1328                 } else {
1329                     throw constructLoadException("Root hasn't been set. Use method setRoot() before load.");
1330                 }
1331             } else {
1332                 if (!type.isAssignableFrom(root.getClass())) {
1333                     throw constructLoadException("Root is not an instance of "
1334                         + type.getName() + ".");
1335                 }
1336 
1337                 value = root;
1338             }
1339 


3389             if (type == Object.class) {
3390                 return;
3391             }
3392 
3393             int allowedClassAccess = prevAllowedClassAccess;
3394             int allowedMemberAccess = prevAllowedMemberAccess;
3395             if ((callerClassLoader != null)
3396                     && (type.getClassLoader() != callerClassLoader)) {
3397                 // restrict further access
3398                 allowedClassAccess &= PUBLIC;
3399                 allowedMemberAccess &= PUBLIC;
3400             }
3401 
3402             final int classAccess = getAccess(type.getModifiers());
3403             if ((classAccess & allowedClassAccess) == 0) {
3404                 // we are done
3405                 return;
3406             }
3407 
3408             ReflectUtil.checkPackageAccess(type);


3409 
3410             addAccessibleMembers(type.getSuperclass(),
3411                                  allowedClassAccess,
3412                                  allowedMemberAccess,
3413                                  membersType);
3414 
3415             final int finalAllowedMemberAccess = allowedMemberAccess;
3416             AccessController.doPrivileged(
3417                     new PrivilegedAction<Void>() {
3418                         @Override
3419                         public Void run() {
3420                             if (membersType == FIELDS) {
3421                                 addAccessibleFields(type,
3422                                                     finalAllowedMemberAccess);
3423                             } else {
3424                                 addAccessibleMethods(type,
3425                                                      finalAllowedMemberAccess);
3426                             }
3427 
3428                             return null;


< prev index next >