1 package spoon.examples.distcalc.processing; 2 3 import java.util.ArrayList ; 4 5 import spoon.examples.distcalc.annotation.Node; 6 import spoon.examples.distcalc.template.ServicesImplTemplate; 7 import spoon.examples.distcalc.template._NodeImpl_; 8 import spoon.examples.distcalc.template._ReturnType_; 9 import spoon.processing.AbstractAnnotationProcessor; 10 import spoon.processing.Environment; 11 import spoon.processing.Severity; 12 import spoon.reflect.Factory; 13 import spoon.reflect.code.CtBlock; 14 import spoon.reflect.code.CtIf; 15 import spoon.reflect.code.CtInvocation; 16 import spoon.reflect.code.CtStatement; 17 import spoon.reflect.declaration.CtAnonymousExecutable; 18 import spoon.reflect.declaration.CtClass; 19 import spoon.reflect.declaration.CtInterface; 20 import spoon.reflect.declaration.CtMethod; 21 import spoon.reflect.declaration.CtPackage; 22 import spoon.reflect.declaration.CtParameter; 23 import spoon.reflect.declaration.ModifierKind; 24 import spoon.template.Substitution; 25 import spoon.template.TemplateParameter; 26 27 public class NodeProcessor extends 28 AbstractAnnotationProcessor<Node, CtMethod<?>> { 29 30 public static CtInterface<?> getNodeInterface(Factory f, _NodeImpl_ t, 31 CtPackage p, String funName) { 32 CtInterface<?> i = null; 33 i = f.Interface().get(p.getQualifiedName() + ".Node_" + funName); 34 if (i == null) { 35 i = f.Interface().create(p, "Node_" + funName); 36 i.getModifiers().add(ModifierKind.PUBLIC); 37 Substitution.insertAll(i, t); 38 } 39 return i; 40 } 41 42 public void process(Node node, CtMethod<?> method) { 43 Factory f = method.getFactory(); 44 Environment env = f.getEnvironment(); 45 CtPackage p = method.getParent(CtPackage.class); 46 CtClass<Object > c = f.Class().create(p, 47 "Node_" + method.getSimpleName()); 48 TemplateParameter<Boolean > pre = null; 49 CtBlock<_ReturnType_> b = f.Core().createBlock(); 50 CtMethod<?> fun = null; 51 for (CtStatement s : method.getBody().getStatements()) { 52 if (s.getLabel() != null) { 53 if (s.getLabel().equals("pre")) { 54 pre = ((CtIf) s).getCondition(); 55 } else if (s.getLabel().equals("fun")) { 56 fun = (CtMethod<?>) ((CtInvocation<?>) s).getExecutable() 57 .getDeclaration(); 58 } 59 } else { 60 b.getStatements().add(s); 61 } 62 } 63 if (pre == null) { 64 env.report(this, Severity.ERROR, method, 65 "undefined 'pre' clause in node"); 66 env.setProcessingStopped(true); 67 } else { 68 _NodeImpl_ t = new _NodeImpl_(null, method.getType() 69 .getActualClass(), pre, b, new ArrayList <CtParameter>( 70 method.getParameters())); 71 Substitution.insertAll(c, t); 72 CtInterface<?> i = getNodeInterface(f, t, p, fun.getSimpleName()); 73 c.getSuperInterfaces().add(f.Interface().createReference(i)); 74 CtClass<?> funsImpl = ServiceProcessor.getFunsImpl(f, 75 (CtInterface<?>) fun.getParent()); 76 c.setSuperclass(f.Type().createReference(funsImpl)); 77 78 ServicesImplTemplate ft = new ServicesImplTemplate(f.Type() 80 .createReference(i), f.Type().createReference(c), null, 81 Object .class, fun.getSimpleName(), null); 82 CtBlock<?> block = Substitution.substituteMethodBody(funsImpl, ft, 83 "initializer"); 84 CtAnonymousExecutable a = f.Executable().createAnonymous(funsImpl, 85 block); 86 a.getModifiers().add(ModifierKind.STATIC); 87 } 88 } 89 90 } 91 | Popular Tags |