beantype.java
来自「RESIN 3.2 最新源码」· Java 代码 · 共 678 行 · 第 1/2 页
JAVA
678 行
// /** * Introspect the bean for configuration */ @Override public void introspect() { synchronized (_introspectLock) { if (_isIntrospecting) return; _isIntrospecting = true; try { // ioc/20h4 - after to deal with recursion introspectParent(); //Method []methods = _beanClass.getMethods(); if (! _isIntrospected) { _isIntrospected = true; Method []methods = _beanClass.getDeclaredMethods(); introspectMethods(methods); InjectIntrospector.introspectInject(_injectList, _beanClass); InjectIntrospector.introspectInit(_initList, _beanClass); } } finally { _isIntrospecting = false; } } introspectComplete(); } private void introspectComplete() { ArrayList<BeanType> childList = new ArrayList<BeanType>(_pendingChildList); // ioc/20h4 for (BeanType child : childList) { child.introspectParent(); child.introspectComplete(); } } private boolean isIntrospecting() { if (_isIntrospecting) return true; Class parentClass = _beanClass.getSuperclass(); if (parentClass != null) { ConfigType parentType = TypeFactory.getType(parentClass); if (parentType instanceof BeanType) { BeanType parentBean = (BeanType) parentType; return parentBean.isIntrospecting(); } } return false; } private void introspectParent() { Class parentClass = _beanClass.getSuperclass(); if (parentClass != null) { ConfigType parentType = TypeFactory.getType(parentClass); if (parentType instanceof BeanType) { BeanType parentBean = (BeanType) parentType; if (! parentBean._isIntrospected) parentBean.introspect(); // ioc/20h4 if (parentBean.isIntrospecting()) { if (! parentBean._pendingChildList.contains(this)) parentBean._pendingChildList.add(this); return; } if (_setParent == null) _setParent = parentBean._setParent; if (_replaceObject == null) _replaceObject = parentBean._replaceObject; if (_setConfigLocation == null) _setConfigLocation = parentBean._setConfigLocation; if (_addText == null) _addText = parentBean._addText; if (_addProgram == null) _addProgram = parentBean._addProgram; if (_addContentProgram == null) _addContentProgram = parentBean._addContentProgram; if (_setProperty == null) _setProperty = parentBean._setProperty; for (Map.Entry<QName,Attribute> entry : parentBean._nsAttributeMap.entrySet()) { if (_nsAttributeMap.get(entry.getKey()) == null) _nsAttributeMap.put(entry.getKey(), entry.getValue()); } for (Map.Entry<String,Attribute> entry : parentBean._attributeMap.entrySet()) { if (_attributeMap.get(entry.getKey()) == null) _attributeMap.put(entry.getKey(), entry.getValue()); } } } } /** * Introspect the beans methods for setters */ public void introspectMethods(Method []methods) { try { _stringConstructor = _beanClass.getConstructor(new Class[] { String.class } ); } catch (NoSuchMethodException e) { } HashMap<String,Method> createMap = new HashMap<String,Method>(); fillCreateMap(createMap, methods); HashMap<String,Method> setterMap = new HashMap<String,Method>(); fillSetterMap(setterMap, methods); for (Method method : methods) { Class []paramTypes = method.getParameterTypes(); String name = method.getName(); if ("replaceObject".equals(name) && paramTypes.length == 0) { _replaceObject = method; continue; } if ("valueOf".equals(name) && paramTypes.length == 1 && String.class.equals(paramTypes[0]) && Modifier.isStatic(method.getModifiers())) { _valueOf = method; continue; } if (Modifier.isStatic(method.getModifiers())) continue; if (! Modifier.isPublic(method.getModifiers())) continue; if ((name.equals("addBuilderProgram") || name.equals("addProgram")) && paramTypes.length == 1 && paramTypes[0].equals(ConfigProgram.class)) { ConfigType type = TypeFactory.getType(paramTypes[0]); _addProgram = new ProgramAttribute(method, type); } else if (name.equals("addContentProgram") && paramTypes.length == 1 && paramTypes[0].equals(ConfigProgram.class)) { ConfigType type = TypeFactory.getType(paramTypes[0]); _addContentProgram = new ProgramAttribute(method, type); } else if ((name.equals("setConfigLocation") && paramTypes.length == 2 && paramTypes[0].equals(String.class) && paramTypes[1].equals(int.class))) { _setConfigLocation = method; } else if (name.equals("setProperty") && paramTypes.length == 2 && paramTypes[0].equals(String.class)) { ConfigType type = TypeFactory.getType(paramTypes[1]); PropertyAttribute attr = new PropertyAttribute(method, type); _setProperty = attr; } else if (name.equals("setParent") && paramTypes.length == 1) { // XXX: use annotation _setParent = method; } else if ((name.startsWith("set") || name.startsWith("add")) && paramTypes.length == 1 && createMap.get(name.substring(3)) == null) { ConfigType type = TypeFactory.getType(paramTypes[0]); String propName = toXmlName(name.substring(3)); Attribute attr; if (propName.equals("text") && (paramTypes[0].equals(String.class) || paramTypes[0].equals(RawString.class))) { attr = new TextAttribute(method, type); _addText = attr; _attributeMap.put("#text", attr); } else attr = new SetterAttribute(method, type); _attributeMap.put(propName, attr); if (propName.equals("value")) { _attributeMap.put("#text", attr); // server/12aa if (_addText == null) _addText = attr; } propName = toCamelName(name.substring(3)); _attributeMap.put(propName, attr); } else if ((name.startsWith("create") && paramTypes.length == 0 && ! void.class.equals(method.getReturnType()))) { ConfigType type = TypeFactory.getType(method.getReturnType()); Method setter = setterMap.get(name.substring(6)); CreateAttribute attr = new CreateAttribute(method, type, setter); String propName = toXmlName(name.substring(6)); _attributeMap.put(propName, attr); } } } private void fillCreateMap(HashMap<String,Method> createMap, Method []methods) { for (Method method : methods) { String name = method.getName(); if (name.startsWith("create") && ! name.equals("create") && method.getParameterTypes().length == 0) { createMap.put(name.substring("create".length()), method); } } } private void fillSetterMap(HashMap<String,Method> setterMap, Method []methods) { for (Method method : methods) { String name = method.getName(); if (name.length() > 3 && (name.startsWith("add") || name.startsWith("set")) && method.getParameterTypes().length == 1) { setterMap.put(name.substring("set".length()), method); } } } private Method findCreate(Method []methods, String name) { String createName = "create" + name; for (Method method : methods) { if (method.getParameterTypes().length != 0) continue; if (method.getName().equals(createName)) return method; } return null; } private Method findSetter(String name) { String addName = "add" + name; String setName = "set" + name; for (Method method : _beanClass.getMethods()) { if (method.getParameterTypes().length != 1) continue; if (method.getName().equals(addName) || method.getName().equals(setName)) return method; } return null; } private String toXmlName(String name) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < name.length(); i++) { char ch = name.charAt(i); if (Character.isUpperCase(ch) && i > 0 && (Character.isLowerCase(name.charAt(i - 1)) || (i + 1 < name.length() && Character.isLowerCase(name.charAt(i + 1))))) { sb.append('-'); } sb.append(Character.toLowerCase(ch)); } return sb.toString(); } private String toCamelName(String name) { return Introspector.decapitalize(name); } @Override public String toString() { return getClass().getSimpleName() + "[" + _beanClass.getName() + "]"; }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?