You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by tc...@apache.org on 2004/11/06 02:56:59 UTC
svn commit: rev 56723 - cocoon/trunk/src/java/org/apache/cocoon/util
Author: tcurdt
Date: Fri Nov 5 17:56:59 2004
New Revision: 56723
Added:
cocoon/trunk/src/java/org/apache/cocoon/util/ReflectionUtils.java
Log:
forgot to add the utils
Added: cocoon/trunk/src/java/org/apache/cocoon/util/ReflectionUtils.java
==============================================================================
--- (empty file)
+++ cocoon/trunk/src/java/org/apache/cocoon/util/ReflectionUtils.java Fri Nov 5 17:56:59 2004
@@ -0,0 +1,137 @@
+/*
+ * Copyright 1999-2004 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.cocoon.util;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.util.Map;
+
+/**
+ * @author tcurdt
+ *
+ */
+public final class ReflectionUtils {
+
+ public interface Matcher {
+ boolean matches(final String pName);
+ }
+
+ public interface Indexer {
+ void put(final Map pMap, final String pKey, final Object pObject);
+ };
+
+ private static DefaultIndexer defaultIndexer = new DefaultIndexer();
+ private static DefaultMatcher defaultMatcher = new DefaultMatcher();
+
+ private static class DefaultMatcher implements Matcher {
+ public boolean matches(final String pName) {
+ return pName.startsWith("do");
+ }
+ }
+
+ private static class DefaultIndexer implements Indexer {
+ public void put(final Map pMap, final String pKey, final Object pObject) {
+
+ // doAction -> action
+ final String name = Character.toLowerCase(pKey.charAt(2)) + pKey.substring(3);
+
+ System.out.println("reflecting " + name);
+ pMap.put(name, pObject);
+ }
+ };
+
+ public static Map discoverFields(
+ final Class pClazz,
+ final Matcher pMatcher
+ ) {
+
+ return discoverFields(pClazz, pMatcher, defaultIndexer);
+ }
+
+ public static Map discoverFields(
+ final Class pClazz
+ ) {
+
+ return discoverFields(pClazz, defaultMatcher, defaultIndexer);
+ }
+
+ public static Map discoverFields(
+ final Class pClazz,
+ final Matcher pMatcher,
+ final Indexer pIndexer
+ ) {
+
+ System.out.println("discovering fields on " + pClazz.getName());
+
+ final Map result = new HashMap();
+
+ Class current = pClazz;
+ do {
+ final Field[] fields = current.getDeclaredFields();
+ for (int i = 0; i < fields.length; i++) {
+ final String fname = fields[i].getName();
+ if (pMatcher.matches(fname)) {
+ pIndexer.put(result, fname, fields[i]);
+ }
+ }
+ current = current.getSuperclass();
+ } while(current != null);
+
+ return result;
+ }
+
+
+ public static Map discoverMethods(
+ final Class pClazz,
+ final Matcher pMatcher
+ ) {
+
+ return discoverMethods(pClazz, pMatcher, defaultIndexer);
+ }
+
+ public static Map discoverMethods(
+ final Class pClazz
+ ) {
+
+ return discoverMethods(pClazz, defaultMatcher, defaultIndexer);
+ }
+
+ public static Map discoverMethods(
+ final Class pClazz,
+ final Matcher pMatcher,
+ final Indexer pIndexer
+ ) {
+
+ System.out.println("discovering methods on " + pClazz.getName());
+
+ final Map result = new HashMap();
+
+ Class current = pClazz;
+ do {
+ final Method[] methods = current.getDeclaredMethods();
+ for (int i = 0; i < methods.length; i++) {
+ final String mname = methods[i].getName();
+ if (pMatcher.matches(mname)) {
+ pIndexer.put(result, mname, methods[i]);
+ }
+ }
+ current = current.getSuperclass();
+ } while(current != null);
+
+ return result;
+ }
+
+}