View Javadoc

1   /*
2    * Copyright 2004-2010 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  
17  package org.seasar.cubby.internal.util;
18  
19  import java.lang.reflect.Field;
20  import java.util.ArrayList;
21  import java.util.Collection;
22  import java.util.Collections;
23  
24  /**
25   * リフレクションのためのユーティリティクラスです。
26   * 
27   * @author baba
28   */
29  public class ReflectionUtils {
30  
31  	/**
32  	 * 指定されたクラスとそのすべてのスーパークラスに定義されたフィールドを取得します。
33  	 * 
34  	 * @param clazz
35  	 *            フィールドを検索するクラス
36  	 * @return 検索したフィールドのコレクション
37  	 */
38  	public static Collection<Field> findAllDeclaredField(final Class<?> clazz) {
39  		final Collection<Field> fields = new ArrayList<Field>(50);
40  		appendFields(clazz, fields);
41  		return Collections.unmodifiableCollection(fields);
42  	}
43  
44  	/**
45  	 * 指定されたクラスとそのすべてのスーパークラスに定義されたフィールドを指定されたコレクションに追加します。
46  	 * 
47  	 * @param clazz
48  	 *            フィールドを検索するクラス
49  	 * @param fields
50  	 *            フィールドを追加するコレクション
51  	 */
52  	private static void appendFields(final Class<?> clazz,
53  			final Collection<Field> fields) {
54  		for (final Field field : clazz.getDeclaredFields()) {
55  			fields.add(field);
56  		}
57  		final Class<?> superClass = clazz.getSuperclass();
58  		if (!Object.class.equals(superClass)) {
59  			appendFields(superClass, fields);
60  		}
61  
62  	}
63  
64  }