1 package spoon.support.util; 2 3 import java.lang.reflect.Field ; 4 import java.lang.reflect.InvocationTargetException ; 5 import java.lang.reflect.Method ; 6 import java.lang.reflect.Modifier ; 7 import java.util.ArrayList ; 8 import java.util.List ; 9 import java.util.Set ; 10 import java.util.TreeSet ; 11 12 import spoon.reflect.code.CtExpression; 13 import spoon.reflect.code.CtInvocation; 14 import spoon.reflect.code.CtLiteral; 15 import spoon.reflect.declaration.ModifierKind; 16 import spoon.reflect.reference.CtTypeReference; 17 18 21 public abstract class RtHelper { 22 23 private RtHelper() { 24 } 25 26 30 public static Field [] getAllFields(Class <?> c) { 31 List <Field > fields = new ArrayList <Field >(); 32 while (c != null && c != Object .class) { 33 for (Field f : c.getDeclaredFields()) { 34 fields.add(f); 35 } 36 c = c.getSuperclass(); 38 } 39 Field [] result = new Field [fields.size()]; 40 return fields.toArray(result); 41 } 42 43 47 public static Method [] getAllMethods(Class <?> c) { 48 List <Method > methods = new ArrayList <Method >(); 49 if (c.isInterface()) { 50 getAllIMethods(c, methods); 51 } else { 52 while (c != null && c != Object .class) { 53 for (Method m : c.getDeclaredMethods()) 54 methods.add(m); 55 c = c.getSuperclass(); 57 } 58 } 59 Method [] result = new Method [methods.size()]; 60 return methods.toArray(result); 61 } 62 63 private static void getAllIMethods(Class <?> c, List <Method > methods) { 64 for (Method m : c.getDeclaredMethods()) 65 methods.add(m); 66 for (Class i : c.getInterfaces()) { 67 getAllIMethods(i, methods); 68 } 69 } 70 71 75 @SuppressWarnings ("unchecked") 76 public static <T> T invoke(CtInvocation<T> i) throws NoSuchMethodException , 77 IllegalAccessException , InvocationTargetException { 78 Object target = i.getTarget() == null ? null : ((CtLiteral<?>) i 79 .getTarget()).getValue(); 80 List <Object > args = new ArrayList <Object >(); 81 for (CtExpression e : i.getArguments()) { 82 args.add(((CtLiteral<?>) e).getValue()); 83 } 84 Class <?> c = i.getExecutable().getDeclaringType().getActualClass(); 85 ArrayList <Class <?>> argTypes = new ArrayList <Class <?>>(); 86 for (CtTypeReference<?> type : i.getExecutable().getParameterTypes()) { 87 argTypes.add(type.getActualClass()); 88 } 89 return (T) c.getMethod(i.getExecutable().getSimpleName(), 90 argTypes.toArray(new Class [argTypes.size()])).invoke(target, 91 args.toArray()); 92 } 93 94 98 public static Set <ModifierKind> getModifiers(int mod) { 99 Set <ModifierKind> set = new TreeSet <ModifierKind>(); 100 if (Modifier.isAbstract(mod)) { 101 set.add(ModifierKind.ABSTRACT); 102 } 103 if (Modifier.isFinal(mod)) { 104 set.add(ModifierKind.FINAL); 105 } 106 if (Modifier.isNative(mod)) { 107 set.add(ModifierKind.NATIVE); 108 } 109 if (Modifier.isPrivate(mod)) { 110 set.add(ModifierKind.PRIVATE); 111 } 112 if (Modifier.isProtected(mod)) { 113 set.add(ModifierKind.PROTECTED); 114 } 115 if (Modifier.isPublic(mod)) { 116 set.add(ModifierKind.PUBLIC); 117 } 118 if (Modifier.isStatic(mod)) { 119 set.add(ModifierKind.STATIC); 120 } 121 if (Modifier.isStrict(mod)) { 122 set.add(ModifierKind.STRICTFP); 123 } 124 if (Modifier.isSynchronized(mod)) { 125 set.add(ModifierKind.SYNCHRONIZED); 126 } 127 if (Modifier.isTransient(mod)) { 128 set.add(ModifierKind.TRANSIENT); 129 } 130 if (Modifier.isVolatile(mod)) { 131 set.add(ModifierKind.VOLATILE); 132 } 133 return set; 134 } 135 136 } 137 | Popular Tags |