beanutil.java
来自「RESIN 3.2 最新源码」· Java 代码 · 共 654 行 · 第 1/2 页
JAVA
654 行
/* * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved * * This file is part of Resin(R) Open Source * * Each copy or derived work must preserve the copyright notice and this * notice unmodified. * * Resin Open Source is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Resin Open Source is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty * of NON-INFRINGEMENT. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License * along with Resin Open Source; if not, write to the * Free SoftwareFoundation, Inc. * 59 Temple Place, Suite 330 * Boston, MA 02111-1307 USA * * @author Scott Ferguson */package com.caucho.util;import com.caucho.vfs.Path;import com.caucho.vfs.Vfs;import java.beans.BeanInfo;import java.beans.PropertyDescriptor;import java.lang.reflect.Constructor;import java.lang.reflect.Method;import java.lang.reflect.Modifier;import java.util.HashMap;import java.util.logging.Logger;/** * Bean utilities. */public class BeanUtil { static final Logger log = Log.open(BeanUtil.class); static L10N L = new L10N(BeanUtil.class); /** * Returns the bean property type. * * @param obj the bean object * @param name the property name */ public static Class getBeanPropertyClass(Object obj, String name) { Method method = getBeanPropertyMethod(obj, name); if (method == null) return null; Class []paramTypes = method.getParameterTypes(); if (paramTypes.length == 1) return paramTypes[0]; else return null; } /** * Returns the bean property type. * * @param obj the bean object * @param name the property name */ public static Method getBeanPropertyMethod(Object obj, String name) { name = configToBeanName(name); Class beanClass = obj.getClass(); Method method = getSetMethod(beanClass, name); if (method == null) method = getAddMethod(beanClass, name); return method; } public static void validateClass(Class cl, Class parent) throws RegistryException { if (parent.isAssignableFrom(cl)) { } else if (parent.isInterface()) throw new RegistryException(L.l("{0} must implement {1}", cl.getName(), parent.getName())); else throw new RegistryException(L.l("{0} must extend {1}", cl.getName(), parent.getName())); if (cl.isInterface()) throw new RegistryException(L.l("{0} must be a concrete class.", cl.getName())); if (Modifier.isAbstract(cl.getModifiers())) throw new RegistryException(L.l("{0} must not be abstract.", cl.getName())); if (! Modifier.isPublic(cl.getModifiers())) throw new RegistryException(L.l("{0} must be public.", cl.getName())); Constructor zero = null; try { zero = cl.getConstructor(new Class[0]); } catch (Throwable e) { } if (zero == null) throw new RegistryException(L.l("{0} must have a public zero-arg constructor.", cl.getName())); } /** * Returns the native path for a configured path name. The special cases * $app-dir and $resin-home specify the root directory. * * @param pathName the configuration path name. * @param varMap the map of path variables. * @param pwd the default path. * * @return a real path corresponding to the path name */ public static Path lookupPath(String pathName, HashMap varMap, Path pwd) { if (pwd == null) pwd = Vfs.lookup(); if (pathName.startsWith("$")) { int p = pathName.indexOf('/'); String prefix; String suffix; if (p > 0) { prefix = pathName.substring(1, p); suffix = pathName.substring(p + 1); } else { prefix = pathName.substring(1); suffix = null; } Object value = varMap != null ? varMap.get(prefix) : null; if (value instanceof Path) { pwd = (Path) value; pathName = suffix; } } if (pathName == null) return pwd; else if (pathName.indexOf('$') < 0) return pwd.lookup(pathName); CharBuffer cb = CharBuffer.allocate(); int head = 0; int tail = 0; while ((tail = pathName.indexOf('$', head)) >= 0) { cb.append(pathName.substring(head, tail)); if (tail + 1 == pathName.length()) { cb.append('$'); continue; } int ch = pathName.charAt(tail + 1); if (ch >= '0' && ch <= '9') { for (head = tail + 1; head < pathName.length(); head++) { ch = pathName.charAt(head); if (ch < '0' || ch > '9') break; } } else { for (head = tail + 1; head < pathName.length(); head++) { ch = pathName.charAt(head); if (ch == '/' || ch == '\\' || ch == '$' || ch == ' ') break; } } String key = pathName.substring(tail + 1, head); Object value = varMap != null ? varMap.get(key) : null; if (value == null) value = System.getProperty(key); if (value != null) cb.append(value); else cb.append(pathName.substring(tail, head)); } if (head > 0 && head < pathName.length()) cb.append(pathName.substring(head)); return pwd.lookupNative(cb.close()); } /** * Translates a configuration name to a bean name. * * <pre> * foo-bar maps to fooBar * </pre> */ private static String configToBeanName(String name) { CharBuffer cb = CharBuffer.allocate(); for (int i = 0; i < name.length(); i++) { char ch = name.charAt(i); if (ch == '-') cb.append(Character.toUpperCase(name.charAt(++i))); else cb.append(ch); } return cb.close(); } /** * Returns an add method matching the name. */ private static Method getAddMethod(Class cl, String name) { name = "add" + name; Method []methods = cl.getMethods(); for (int i = 0; i < methods.length; i++) { if (! Modifier.isPublic(methods[i].getModifiers())) continue; if (! name.equalsIgnoreCase(methods[i].getName())) continue; if (methods[i].getParameterTypes().length == 1) return methods[i]; } return null; } /** * Returns the method matching the name. */ static private Method getMethod(Method []methods, String name) { Method method = null; for (int i = 0; i < methods.length; i++) { method = methods[i]; if (! Modifier.isPublic(method.getModifiers())) continue; if (! Modifier.isPublic(method.getDeclaringClass().getModifiers())) continue; if (method.getName().equals(name)) return method; } return null; } /** * Returns the method matching the name. */ static private Method getMethod(Method []methods, String name, Class []params) { Method method = null; loop: for (int i = 0; i < methods.length; i++) { method = methods[i]; if (! Modifier.isPublic(method.getModifiers())) continue; if (! Modifier.isPublic(method.getDeclaringClass().getModifiers())) continue; if (! method.getName().equals(name)) continue; Class []actual = method.getParameterTypes(); if (actual.length != params.length) continue; for (int j = 0; j < actual.length; j++) { if (! actual[j].isAssignableFrom(params[j])) continue loop; } return method; } return null; } /** * Returns a set method matching the property name. */ public static Method getSetMethod(BeanInfo info, String propertyName) { // jsp, 184c, jsp/184z, bug #2634 Method method;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?