📄 xmlconfiguration.java
字号:
catch (IllegalArgumentException e) { Log.ignore(e); } catch (IllegalAccessException e) { Log.ignore(e); } catch (NoSuchMethodException e) { Log.ignore(e); } // Try a field try { Field field = oClass.getField(attr); if (Modifier.isPublic(field.getModifiers())) { field.set(obj, value); return; } } catch (NoSuchFieldException e) { Log.ignore(e); } // Search for a match by trying all the set methods Method[] sets = oClass.getMethods(); Method set = null; for (int s = 0; sets != null && s < sets.length; s++) { if (name.equals(sets[s].getName()) && sets[s].getParameterTypes().length == 1) { // lets try it try { set = sets[s]; sets[s].invoke(obj, arg); return; } catch (IllegalArgumentException e) { Log.ignore(e); } catch (IllegalAccessException e) { Log.ignore(e); } } } // Try converting the arg to the last set found. if (set != null) { try { Class sClass = set.getParameterTypes()[0]; if (sClass.isPrimitive()) { for (int t = 0; t < __primitives.length; t++) { if (sClass.equals(__primitives[t])) { sClass = __primitiveHolders[t]; break; } } } Constructor cons = sClass.getConstructor(vClass); arg[0] = cons.newInstance(arg); set.invoke(obj, arg); return; } catch (NoSuchMethodException e) { Log.ignore(e); } catch (IllegalAccessException e) { Log.ignore(e); } catch (InstantiationException e) { Log.ignore(e); } } // No Joy throw new NoSuchMethodException(oClass + "." + name + "(" + vClass[0] + ")"); } /* ------------------------------------------------------------ */ /* * Call a put method. * * @param obj @param node */ private void put(Object obj, XmlParser.Node node) throws Exception { if (!(obj instanceof Map)) throw new IllegalArgumentException("Object for put is not a Map: " + obj); Map map = (Map) obj; String name = node.getAttribute("name"); Object value = value(obj, node); map.put(name, value); if (Log.isDebugEnabled()) Log.debug("XML "+obj + ".put(" + name + "," + value + ")"); } /* ------------------------------------------------------------ */ /* * Call a get method. Any object returned from the call is passed to the configure method to * consume the remaining elements. @param obj @param node @return @exception Exception */ private Object get(Object obj, XmlParser.Node node) throws Exception { Class oClass = nodeClass(node); if (oClass != null) obj = null; else oClass = obj.getClass(); String name = node.getAttribute("name"); String id = node.getAttribute("id"); if (Log.isDebugEnabled()) Log.debug("XML get " + name); try { // try calling a getXxx method. Method method = oClass.getMethod("get" + name.substring(0, 1).toUpperCase() + name.substring(1), (java.lang.Class[]) null); obj = method.invoke(obj, (java.lang.Object[]) null); configure(obj, node, 0); } catch (NoSuchMethodException nsme) { try { Field field = oClass.getField(name); obj = field.get(obj); configure(obj, node, 0); } catch (NoSuchFieldException nsfe) { throw nsme; } } if (id != null) _idMap.put(id, obj); return obj; } /* ------------------------------------------------------------ */ /* * Call a method. A method is selected by trying all methods with matching names and number of * arguments. Any object returned from the call is passed to the configure method to consume the * remaining elements. Note that if this is a static call we consider only methods declared * directly in the given class. i.e. we ignore any static methods in superclasses. @param obj * @param node @return @exception Exception */ private Object call(Object obj, XmlParser.Node node) throws Exception { String id = node.getAttribute("id"); Class oClass = nodeClass(node); if (oClass != null) obj = null; else if (obj != null) oClass = obj.getClass(); if (oClass == null) throw new IllegalArgumentException(node.toString()); int size = 0; int argi = node.size(); for (int i = 0; i < node.size(); i++) { Object o = node.get(i); if (o instanceof String) continue; if (!((XmlParser.Node) o).getTag().equals("Arg")) { argi = i; break; } size++; } Object[] arg = new Object[size]; for (int i = 0, j = 0; j < size; i++) { Object o = node.get(i); if (o instanceof String) continue; arg[j++] = value(obj, (XmlParser.Node) o); } String method = node.getAttribute("name"); if (Log.isDebugEnabled()) Log.debug("XML call " + method); // Lets just try all methods for now Method[] methods = oClass.getMethods(); for (int c = 0; methods != null && c < methods.length; c++) { if (!methods[c].getName().equals(method)) continue; if (methods[c].getParameterTypes().length != size) continue; if (Modifier.isStatic(methods[c].getModifiers()) != (obj == null)) continue; if ((obj == null) && methods[c].getDeclaringClass() != oClass) continue; Object n = null; boolean called = false; try { n = methods[c].invoke(obj, arg); called = true; } catch (IllegalAccessException e) { Log.ignore(e); } catch (IllegalArgumentException e) { Log.ignore(e); } if (called) { if (id != null) _idMap.put(id, n); configure(n, node, argi); return n; } } throw new IllegalStateException("No Method: " + node + " on " + oClass); } /* ------------------------------------------------------------ */ /* * Create a new value object. * * @param obj @param node @return @exception Exception */ private Object newObj(Object obj, XmlParser.Node node) throws Exception { Class oClass = nodeClass(node); String id = node.getAttribute("id"); int size = 0; int argi = node.size(); for (int i = 0; i < node.size(); i++) { Object o = node.get(i); if (o instanceof String) continue; if (!((XmlParser.Node) o).getTag().equals("Arg")) { argi = i; break; } size++; } Object[] arg = new Object[size]; for (int i = 0, j = 0; j < size; i++) { Object o = node.get(i); if (o instanceof String) continue; arg[j++] = value(obj, (XmlParser.Node) o); } if (Log.isDebugEnabled()) Log.debug("XML new " + oClass); // Lets just try all constructors for now Constructor[] constructors = oClass.getConstructors(); for (int c = 0; constructors != null && c < constructors.length; c++) { if (constructors[c].getParameterTypes().length != size) continue; Object n = null; boolean called = false; try { n = constructors[c].newInstance(arg); called = true; } catch (IllegalAccessException e) { Log.ignore(e); } catch (InstantiationException e) { Log.ignore(e); } catch (IllegalArgumentException e) { Log.ignore(e); } if (called) { if (id != null) _idMap.put(id, n); configure(n, node, argi); return n; } } throw new IllegalStateException("No Constructor: " + node + " on " + obj); } /* ------------------------------------------------------------ */ /* * Reference an id value object. * * @param obj @param node @return @exception NoSuchMethodException @exception * ClassNotFoundException @exception InvocationTargetException */ private Object refObj(Object obj, XmlParser.Node node) throws Exception { String id = node.getAttribute("id"); obj = _idMap.get(id); if (obj == null) throw new IllegalStateException("No object for id=" + id); configure(obj, node, 0); return obj; } /* ------------------------------------------------------------ */ /* * Create a new array object. * */ private Object newArray(Object obj, XmlParser.Node node) throws Exception { // Get the type Class aClass = java.lang.Object.class; String type = node.getAttribute("type"); final String id = node.getAttribute("id"); if (type != null) { aClass = TypeUtil.fromName(type); if (aClass == null)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -