001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.util;
018:
019: import java.lang.reflect.Field;
020: import java.lang.reflect.Method;
021: import java.util.Map;
022:
023: /**
024: * @version $Id: ReflectionUtils.java 433543 2006-08-22 06:22:54Z crossley $
025: *
026: */
027: public final class ReflectionUtils {
028:
029: public interface Matcher {
030: boolean matches(final String pName);
031: }
032:
033: public interface Indexer {
034: void put(final Map pMap, final String pKey, final Object pObject);
035: }
036:
037: private static DefaultIndexer defaultIndexer = new DefaultIndexer();
038: private static DefaultMatcher defaultMatcher = new DefaultMatcher();
039:
040: private static class DefaultMatcher implements Matcher {
041: public boolean matches(final String pName) {
042: return pName.startsWith("do");
043: }
044: }
045:
046: private static class DefaultIndexer implements Indexer {
047: public void put(final Map pMap, final String pKey,
048: final Object pObject) {
049:
050: // doAction -> action
051: final String name = Character.toLowerCase(pKey.charAt(2))
052: + pKey.substring(3);
053:
054: System.out.println("reflecting " + name);
055: pMap.put(name, pObject);
056: }
057: }
058:
059: public static Map discoverFields(final Class pClazz,
060: final Matcher pMatcher) {
061:
062: return discoverFields(pClazz, pMatcher, defaultIndexer);
063: }
064:
065: public static Map discoverFields(final Class pClazz) {
066:
067: return discoverFields(pClazz, defaultMatcher, defaultIndexer);
068: }
069:
070: public static Map discoverFields(final Class pClazz,
071: final Matcher pMatcher, final Indexer pIndexer) {
072:
073: System.out.println("discovering fields on " + pClazz.getName());
074:
075: final Map result = new HashMap();
076:
077: Class current = pClazz;
078: do {
079: final Field[] fields = current.getDeclaredFields();
080: for (int i = 0; i < fields.length; i++) {
081: final String fname = fields[i].getName();
082: if (pMatcher.matches(fname)) {
083: pIndexer.put(result, fname, fields[i]);
084: }
085: }
086: current = current.getSuperclass();
087: } while (current != null);
088:
089: return result;
090: }
091:
092: public static Map discoverMethods(final Class pClazz,
093: final Matcher pMatcher) {
094:
095: return discoverMethods(pClazz, pMatcher, defaultIndexer);
096: }
097:
098: public static Map discoverMethods(final Class pClazz) {
099:
100: return discoverMethods(pClazz, defaultMatcher, defaultIndexer);
101: }
102:
103: public static Map discoverMethods(final Class pClazz,
104: final Matcher pMatcher, final Indexer pIndexer) {
105:
106: System.out
107: .println("discovering methods on " + pClazz.getName());
108:
109: final Map result = new HashMap();
110:
111: Class current = pClazz;
112: do {
113: final Method[] methods = current.getDeclaredMethods();
114: for (int i = 0; i < methods.length; i++) {
115: final String mname = methods[i].getName();
116: if (pMatcher.matches(mname)) {
117: pIndexer.put(result, mname, methods[i]);
118: }
119: }
120: current = current.getSuperclass();
121: } while (current != null);
122:
123: return result;
124: }
125:
126: }
|