use of org.eclipse.ceylon.langtools.tools.javac.tree.JCTree in project ceylon by eclipse.
the class JavacElements method getTreeAndTopLevel.
/**
* Returns the best approximation for the tree node and compilation unit
* corresponding to the given element, annotation and value.
* If the element is null, null is returned.
* If the annotation is null or cannot be found, the tree node and
* compilation unit for the element is returned.
* If the annotation value is null or cannot be found, the tree node and
* compilation unit for the annotation is returned.
*/
public Pair<JCTree, JCCompilationUnit> getTreeAndTopLevel(Element e, AnnotationMirror a, AnnotationValue v) {
if (e == null)
return null;
Pair<JCTree, JCCompilationUnit> elemTreeTop = getTreeAndTopLevel(e);
if (elemTreeTop == null)
return null;
if (a == null)
return elemTreeTop;
JCTree annoTree = matchAnnoToTree(a, e, elemTreeTop.fst);
if (annoTree == null)
return elemTreeTop;
// the annotation.
return new Pair<JCTree, JCCompilationUnit>(annoTree, elemTreeTop.snd);
}
use of org.eclipse.ceylon.langtools.tools.javac.tree.JCTree in project ceylon by eclipse.
the class JavacElements method getSourcePosition.
public JavacSourcePosition getSourcePosition(Element e) {
Pair<JCTree, JCCompilationUnit> treeTop = getTreeAndTopLevel(e);
if (treeTop == null)
return null;
JCTree tree = treeTop.fst;
JCCompilationUnit toplevel = treeTop.snd;
JavaFileObject sourcefile = toplevel.sourcefile;
if (sourcefile == null)
return null;
return new JavacSourcePosition(sourcefile, tree.pos, toplevel.lineMap);
}
use of org.eclipse.ceylon.langtools.tools.javac.tree.JCTree in project ceylon by eclipse.
the class JavacElements method matchAnnoToTree.
/**
* Returns the tree for an annotation given an Attribute to
* search (recursively) and its corresponding tree.
* Returns null if the tree cannot be found.
*/
private JCTree matchAnnoToTree(final Attribute.Compound findme, final Attribute attr, final JCTree tree) {
if (attr == findme)
return (tree.type.tsym == findme.type.tsym) ? tree : null;
class Vis implements Attribute.Visitor {
JCTree result = null;
public void visitConstant(Attribute.Constant value) {
}
public void visitClass(Attribute.Class clazz) {
}
public void visitCompound(Attribute.Compound anno) {
for (Pair<MethodSymbol, Attribute> pair : anno.values) {
JCExpression expr = scanForAssign(pair.fst, tree);
if (expr != null) {
JCTree match = matchAnnoToTree(findme, pair.snd, expr);
if (match != null) {
result = match;
return;
}
}
}
}
public void visitArray(Attribute.Array array) {
if (tree.hasTag(NEWARRAY) && types.elemtype(array.type).tsym == findme.type.tsym) {
List<JCExpression> elems = ((JCNewArray) tree).elems;
for (Attribute value : array.values) {
if (value == findme) {
result = elems.head;
return;
}
elems = elems.tail;
}
}
}
public void visitEnum(Attribute.Enum e) {
}
public void visitError(Attribute.Error e) {
}
}
Vis vis = new Vis();
attr.accept(vis);
return vis.result;
}
use of org.eclipse.ceylon.langtools.tools.javac.tree.JCTree in project ceylon by eclipse.
the class JavacParser method annotation.
/**
* Annotation = "@" Qualident [ "(" AnnotationFieldValues ")" ]
*
* @param pos position of "@" token
* @param kind Whether to parse an ANNOTATION or TYPE_ANNOTATION
*/
JCAnnotation annotation(int pos, Tag kind) {
// accept(AT); // AT consumed by caller
checkAnnotations();
if (kind == Tag.TYPE_ANNOTATION) {
checkTypeAnnotations();
}
JCTree ident = qualident(false);
List<JCExpression> fieldValues = annotationFieldValuesOpt();
JCAnnotation ann;
if (kind == Tag.ANNOTATION) {
ann = F.at(pos).Annotation(ident, fieldValues);
} else if (kind == Tag.TYPE_ANNOTATION) {
ann = F.at(pos).TypeAnnotation(ident, fieldValues);
} else {
throw new AssertionError("Unhandled annotation kind: " + kind);
}
storeEnd(ann, S.prevToken().endPos);
return ann;
}
use of org.eclipse.ceylon.langtools.tools.javac.tree.JCTree in project ceylon by eclipse.
the class JavacParser method enumeratorDeclaration.
/**
* EnumeratorDeclaration = AnnotationsOpt [TypeArguments] IDENTIFIER [ Arguments ] [ "{" ClassBody "}" ]
*/
JCTree enumeratorDeclaration(Name enumName) {
Comment dc = token.comment(CommentStyle.JAVADOC);
int flags = Flags.PUBLIC | Flags.STATIC | Flags.FINAL | Flags.ENUM;
if (token.deprecatedFlag()) {
flags |= Flags.DEPRECATED;
}
int pos = token.pos;
List<JCAnnotation> annotations = annotationsOpt(Tag.ANNOTATION);
JCModifiers mods = F.at(annotations.isEmpty() ? Position.NOPOS : pos).Modifiers(flags, annotations);
List<JCExpression> typeArgs = typeArgumentsOpt();
int identPos = token.pos;
Name name = ident();
int createPos = token.pos;
List<JCExpression> args = (token.kind == LPAREN) ? arguments() : List.<JCExpression>nil();
JCClassDecl body = null;
if (token.kind == LBRACE) {
JCModifiers mods1 = F.at(Position.NOPOS).Modifiers(Flags.ENUM | Flags.STATIC);
List<JCTree> defs = classOrInterfaceBody(names.empty, false);
body = toP(F.at(identPos).AnonymousClassDef(mods1, defs));
}
if (args.isEmpty() && body == null)
createPos = identPos;
JCIdent ident = F.at(identPos).Ident(enumName);
JCNewClass create = F.at(createPos).NewClass(null, typeArgs, ident, args, body);
if (createPos != identPos)
storeEnd(create, S.prevToken().endPos);
ident = F.at(identPos).Ident(enumName);
JCTree result = toP(F.at(pos).VarDef(mods, name, ident, create));
attach(result, dc);
return result;
}
Aggregations