]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src/org/simantics/databoard/util/ReflectionUtils.java
Fixing several binding-related bugs
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / util / ReflectionUtils.java
1 /*******************************************************************************
2  *  Copyright (c) 2010 Association for Decentralized Information Management in
3  *  Industry THTH ry.
4  *  All rights reserved. This program and the accompanying materials
5  *  are made available under the terms of the Eclipse Public License v1.0
6  *  which accompanies this distribution, and is available at
7  *  http://www.eclipse.org/legal/epl-v10.html
8  *
9  *  Contributors:
10  *      VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.databoard.util;
13
14 import java.lang.reflect.Field;
15 import java.lang.reflect.Method;
16 import java.util.ArrayList;
17 import java.util.Collection;
18 import java.util.HashSet;
19 import java.util.LinkedList;
20 import java.util.Set;
21
22 /**
23  * Reflection Utils
24  * 
25  * @author Toni Kalajainen (toni.kalajainen@vtt.fi)
26  */
27 public class ReflectionUtils {
28
29         /**
30          * Returns all methods public, protected and private
31          * 
32          * @param clazz
33          * @return all methods
34          */
35         public static Method[] getAllMethods(Class<?> clazz)
36         {
37                 Set<Method> result = new HashSet<Method>();                             
38                 _getAllMethods(clazz, result);          
39                 return result.toArray(new Method[result.size()]);
40         }
41
42         public static Field[] getAllFields(Class<?> clazz)
43         {
44                 LinkedList<Class<?>> classes = new LinkedList<Class<?>>();
45                 while (clazz!=null) {
46                         classes.addFirst(clazz);
47                         clazz = clazz.getSuperclass();
48                 }
49                 
50                 ArrayList<Field> result = new ArrayList<Field>();
51                 for (Class<?> _class : classes) {
52                         _getAllFields(_class, result);
53                 }
54                 
55                 return result.toArray(new Field[result.size()]);
56         }
57         
58         private static void _getAllFields(Class<?> clazz, Collection<Field> result)
59         {
60                 for (Field m : clazz.getDeclaredFields())
61                         result.add(m);
62         }       
63         
64         private static void _getAllMethods(Class<?> clazz, Collection<Method> result)
65         {
66                 for (Method m : clazz.getDeclaredMethods())
67                         result.add(m);
68         }
69         
70 }