001: /**
002: * MVEL (The MVFLEX Expression Language)
003: *
004: * Copyright (C) 2007 Christopher Brock, MVFLEX/Valhalla Project and the Codehaus
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: *
018: */package org.mvel.util;
019:
020: import static java.lang.System.arraycopy;
021: import java.lang.reflect.Field;
022: import java.util.HashSet;
023: import java.util.Set;
024:
025: /**
026: * Utilities for working with reflection.
027: */
028: public class ReflectionUtil {
029: /**
030: * This new method 'slightly' outperforms the old method, it was
031: * essentially a perfect example of me wasting my time and a
032: * premature optimization. But what the hell...
033: *
034: * @param s
035: * @return String
036: */
037: public static String getSetter(String s) {
038: char[] chars = new char[s.length() + 3];
039:
040: chars[0] = 's';
041: chars[1] = 'e';
042: chars[2] = 't';
043:
044: if (s.charAt(0) > 'Z') {
045: chars[3] = (char) (s.charAt(0) - ('z' - 'Z'));
046: } else {
047: chars[3] = s.charAt(0);
048: }
049:
050: for (int i = s.length() - 1; i != 0; i--) {
051: chars[i + 3] = s.charAt(i);
052: }
053:
054: return new String(chars);
055: }
056:
057: public static String getGetter(String s) {
058: char[] c = s.toCharArray();
059: char[] chars = new char[c.length + 3];
060:
061: chars[0] = 'g';
062: chars[1] = 'e';
063: chars[2] = 't';
064:
065: if (c[0] > 'Z') {
066: chars[3] = (char) (c[0] - ('z' - 'Z'));
067: } else {
068: chars[3] = (c[0]);
069: }
070:
071: arraycopy(c, 1, chars, 4, c.length - 1);
072:
073: return new String(chars);
074: }
075:
076: public static String getIsGetter(String s) {
077: char[] c = s.toCharArray();
078: char[] chars = new char[c.length + 2];
079:
080: chars[0] = 'i';
081: chars[1] = 's';
082:
083: if (s.charAt(0) > 'Z') {
084: chars[2] = (char) (c[0] - ('z' - 'Z'));
085: } else {
086: chars[2] = c[0];
087: }
088:
089: arraycopy(c, 1, chars, 3, c.length - 1);
090:
091: return new String(chars);
092: }
093:
094: private static String parameterizeClassName(String s) {
095: char[] chars = s.toCharArray();
096: if (s.charAt(0) < 'a') {
097: chars[0] = (char) (chars[0] + ('z' - 'Z'));
098: }
099:
100: return new String(chars);
101: }
102:
103: public static String parameterizeClassName(Class c) {
104: return parameterizeClassName(c.getName().substring(
105: c.getName().lastIndexOf('.') + 1));
106: }
107:
108: public static String getParameterFromAccessor(String s) {
109: if (s.charAt(0) == 'i') {
110: char[] chars = new char[s.length() - 2];
111:
112: for (int i = chars.length; i > 0; i--) {
113: chars[i - 1] = s.charAt(i + 1);
114: }
115:
116: if (s.charAt(2) < 'a') {
117: chars[0] = (char) (s.charAt(2) + ('z' - 'Z'));
118: }
119:
120: return new String(chars);
121: } else {
122: char[] chars = new char[s.length() - 3];
123:
124: for (int i = chars.length; i > 0; i--) {
125: chars[i - 1] = s.charAt(i + 2);
126: }
127:
128: if (s.charAt(3) < 'a') {
129: chars[0] = (char) (s.charAt(3) + ('z' - 'Z'));
130: }
131:
132: return new String(chars);
133: }
134: }
135:
136: public static boolean isAssignable(Object instance,
137: Class interfaceClass) {
138: if (instance == null)
139: return false;
140: Class ref = instance.getClass();
141:
142: while (ref != Object.class) {
143: if (interfaceClass.isAssignableFrom(ref))
144: return true;
145: ref = ref.getSuperclass();
146: }
147:
148: return false;
149: }
150:
151: public static Set<Field> getAllFields(Class cls) {
152: Set<Field> allFields = new HashSet<Field>(
153: cls.getFields().length, 1.0f);
154: for (Field fld : cls.getFields())
155: allFields.add(fld);
156: return allFields;
157: }
158: }
|