Commits

Artem Melentyev  committed f9d2add

refactor javac getAnnotations. thanks to Michail Ponkin.

  • Participants
  • Parent commits 10daad2

Comments (0)

Files changed (4)

File javac/src/main/java/juast/javac/JCUtils.java

 
 package juast.javac;
 
+import java.util.Collections;
 import java.util.List;
 
+import javax.lang.model.element.AnnotationMirror;
 import javax.lang.model.type.TypeMirror;
 
 import juast.Annotation;
 import com.sun.source.tree.UnaryTree;
 import com.sun.source.tree.VariableTree;
 import com.sun.source.tree.WhileLoopTree;
+import com.sun.tools.javac.code.Symbol;
 import com.sun.tools.javac.tree.JCTree;
 import com.sun.tools.javac.tree.JCTree.JCAnnotation;
 import com.sun.tools.javac.tree.JCTree.JCArrayAccess;
 			}
 		});
 	}
+	public static List<Annotation> getAnnotationsFromSymbol(Symbol sym, final Node p) {
+		if (sym == null) return Collections.emptyList();
+		return BaseUtils.transform(sym.attributes_field, new fj.F<AnnotationMirror, Annotation>() {
+			public Annotation f(AnnotationMirror a) {
+				return new JWAnnotation.JWAnnotationMirror(a, p);
+			}
+		});
+		// return transformA(tm.Annotations(sym.attributes_field), p); // TODO: treemaker bug: "@some.annotation" transforms "@.some.annotation"
+	}
 
 	public static Expr wrap(ExpressionTree node, Node p) {
 		if (node == null)

File javac/src/main/java/juast/javac/JWIdExpr.java

 
 package juast.javac;
 
-import java.util.Collections;
 import java.util.List;
 
-import javax.lang.model.element.AnnotationMirror;
-
 import juast.Annotation;
 import juast.IdExpr;
 import juast.Node;
-import juast.common.BaseUtils;
 
 import com.sun.tools.javac.tree.JCTree.JCIdent;
 
 	}
 
 	public List<Annotation> getAnnotations() {
-		if (getNested().sym == null)
-			return Collections.emptyList();
-		return BaseUtils.transform(getNested().sym.attributes_field, new fj.F<AnnotationMirror, Annotation>() {
-			public Annotation f(AnnotationMirror a) {
-				return new JWAnnotation.JWAnnotationMirror(a, JWIdExpr.this);
-			}
-		});
+		return JCUtils.getAnnotationsFromSymbol(getNested().sym, JWIdExpr.this);
 	}
 }

File javac/src/main/java/juast/javac/JWMethodInvExpr.java

 import java.util.Collections;
 import java.util.List;
 
-import javax.lang.model.element.AnnotationMirror;
 import javax.lang.model.type.TypeMirror;
 
 import juast.Annotation;
 import juast.Expr;
 import juast.MethodInvExpr;
 import juast.Node;
-import juast.common.BaseUtils;
 import juast.common.NotImplementedException;
 
 import com.sun.tools.javac.code.Symbol.MethodSymbol;
 import com.sun.tools.javac.tree.JCTree.JCFieldAccess;
 import com.sun.tools.javac.tree.JCTree.JCIdent;
 import com.sun.tools.javac.tree.JCTree.JCMethodInvocation;
+import com.sun.tools.javac.tree.TreeInfo;
 
 public class JWMethodInvExpr extends AJWExpr<JCMethodInvocation> implements MethodInvExpr {
 	public JWMethodInvExpr(JCMethodInvocation nested, Node p) {
 	}
 
 	private MethodSymbol getSymbol() {
-		if (getNested().meth instanceof JCFieldAccess)
-			return (MethodSymbol) ((JCFieldAccess)getNested().meth).sym;
-		else if (getNested().meth instanceof JCIdent)
-			return (MethodSymbol) ((JCIdent)getNested().meth).sym;
-		return null;
+		return (MethodSymbol) TreeInfo.symbol(getNested().meth);
 	}
 
 	public List<Annotation> getAnnotations() {
-		MethodSymbol sym = getSymbol();
-		if (sym == null)
-			return Collections.emptyList();
-		return BaseUtils.transform(sym.attributes_field, new fj.F<AnnotationMirror, Annotation>() {
-			public Annotation f(AnnotationMirror a) {
-				return new JWAnnotation.JWAnnotationMirror(a, JWMethodInvExpr.this);
-			}
-		});
+		return JCUtils.getAnnotationsFromSymbol(getSymbol(), JWMethodInvExpr.this);
 	}
 
 	@Override
 		if (sym == null)
 			return Collections.emptyList();
 		List<List<Annotation>> res = new ArrayList<List<Annotation>>();
-		for (VarSymbol p : sym.params()) {
-			res.add( BaseUtils.transform(p.attributes_field, new fj.F<AnnotationMirror, Annotation>() {
-				public Annotation f(AnnotationMirror a) {
-					return new JWAnnotation.JWAnnotationMirror(a, JWMethodInvExpr.this);
-				}
-			}) );
-		}
+		for (VarSymbol p : sym.params())
+			res.add( JCUtils.getAnnotationsFromSymbol(p, JWMethodInvExpr.this) );
 		int argsSize = getArgs().size();
 		while (res.size() < argsSize) // duplicate annotation for varArgs params
 			res.add(res.get(res.size()-1));

File javac/src/main/java/juast/javac/JavacNode.java

 
 import com.sun.source.tree.Tree;
 import com.sun.tools.javac.tree.JCTree;
+import com.sun.tools.javac.tree.TreeMaker;
 
 public class JavacNode<T extends Tree> extends NestedNode<T> implements NodeW {
 	public JavacNode(T nested, Node parent) {
 			throw new RuntimeException(e);
 		}
 	}
+
+	protected TreeMaker getTreeMaker() {
+		return getContext().getInjector().getInstance(TreeMaker.class);
+	}
 }