]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src/org/simantics/databoard/util/ReflectionUtils.java
Re-implement URIStringUtils escape and unescape using Unicode
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / util / ReflectionUtils.java
1 /*******************************************************************************\r
2  *  Copyright (c) 2010 Association for Decentralized Information Management in\r
3  *  Industry THTH ry.\r
4  *  All rights reserved. This program and the accompanying materials\r
5  *  are made available under the terms of the Eclipse Public License v1.0\r
6  *  which accompanies this distribution, and is available at\r
7  *  http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  *  Contributors:\r
10  *      VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.databoard.util;
13
14 import java.lang.reflect.Field;\r
15 import java.lang.reflect.Method;\r
16 import java.util.ArrayList;\r
17 import java.util.Collection;\r
18 import java.util.HashSet;\r
19 import java.util.LinkedList;\r
20 import java.util.Set;\r
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 }