1 package spoon.reflect.factory; 2 3 import java.lang.reflect.Method ; 4 import java.util.ArrayList ; 5 import java.util.Arrays ; 6 import java.util.Collection ; 7 import java.util.List ; 8 import java.util.Set ; 9 10 import spoon.reflect.Factory; 11 import spoon.reflect.code.CtBlock; 12 import spoon.reflect.declaration.CtClass; 13 import spoon.reflect.declaration.CtMethod; 14 import spoon.reflect.declaration.CtParameter; 15 import spoon.reflect.declaration.CtSimpleType; 16 import spoon.reflect.declaration.CtType; 17 import spoon.reflect.declaration.ModifierKind; 18 import spoon.reflect.reference.CtExecutableReference; 19 import spoon.reflect.reference.CtTypeReference; 20 import spoon.template.Substitution; 21 22 25 public class MethodFactory extends ExecutableFactory { 26 27 private static final long serialVersionUID = 1L; 28 29 35 public MethodFactory(Factory factory) { 36 super(factory); 37 } 38 39 57 public <T> CtMethod<T> create(CtClass<?> target, 58 Set <ModifierKind> modifiers, CtTypeReference<T> returnType, 59 String name, List <CtParameter<?>> parameters, 60 Set <CtTypeReference<? extends Throwable >> thrownTypes, 61 CtBlock<T> body) { 62 CtMethod<T> method = create(target, modifiers, returnType, name, 63 parameters, thrownTypes); 64 method.setBody(body); 65 body.setParent(method); 66 return method; 67 } 68 69 84 public <T> CtMethod<T> create(CtType<?> target, CtMethod<T> source, 85 boolean redirectReferences) { 86 CtMethod<T> newMethod = factory.Core().clone(source); 87 if (redirectReferences && source.getDeclaringType() != null) { 88 Substitution.redirectTypeReferences(newMethod, source 89 .getDeclaringType().getReference(), target.getReference()); 90 } 91 target.getMethods().add(newMethod); 92 newMethod.setParent(target); 93 return newMethod; 94 } 95 96 112 public <T> CtMethod<T> create(CtType<?> target, 113 Set <ModifierKind> modifiers, CtTypeReference<T> returnType, 114 String name, List <CtParameter<?>> parameters, 115 Set <CtTypeReference<? extends Throwable >> thrownTypes) { 116 CtMethod<T> method = factory.Core().createMethod(); 117 target.getMethods().add(method); 118 method.setParent(target); 119 if (modifiers != null) 120 method.setModifiers(modifiers); 121 method.setType(returnType); 122 method.setSimpleName(name); 123 if (parameters != null) 124 method.setParameters(parameters); 125 setParent(method, parameters); 126 if (thrownTypes != null) 127 method.setThrownTypes(thrownTypes); 128 return method; 129 } 130 131 134 public <T> CtExecutableReference<T> createReference(CtMethod<T> m) { 135 return factory.Executable().createReference(m); 136 } 137 138 141 @SuppressWarnings ("unchecked") 142 public <T> CtExecutableReference<T> createReference(Method method) { 143 return createReference(factory.Type().createReference( 144 method.getDeclaringClass()), (CtTypeReference<T>) factory 145 .Type().createReference(method.getReturnType()), method 146 .getName(), factory.Type().createReferences( 147 (List <Class <?>>) (List ) Arrays.asList(method 148 .getParameterTypes())).toArray( 149 new CtTypeReference<?>[0])); 150 } 151 152 155 public Collection <CtMethod> getMainMethods() { 156 Collection <CtMethod> methods = new ArrayList <CtMethod>(); 157 for (CtSimpleType<?> t : factory.Type().getAll()) { 158 if (t instanceof CtClass) { 159 CtMethod<?> m = ((CtClass<?>) t).getMethod("main", factory 160 .Type().createArrayReference( 161 factory.Type().createReference(String .class))); 162 if (m != null && m.getModifiers().contains(ModifierKind.STATIC)) { 163 methods.add(m); 164 } 165 } 166 } 167 return methods; 168 } 169 170 } 171 | Popular Tags |