use of org.eclipse.ceylon.langtools.tools.javac.code.Symbol in project ceylon by eclipse.
the class JavacClass method getDirectMethods.
@Override
public List<MethodMirror> getDirectMethods() {
if (methods == null) {
List<MethodMirror> ret = new LinkedList<MethodMirror>();
for (Symbol sym : classSymbol.getEnclosedElements()) {
if (sym instanceof MethodSymbol && (sym.flags() & Flags.PRIVATE) == 0) {
ret.add(new JavacMethod(this, (MethodSymbol) sym));
}
}
methods = Collections.unmodifiableList(ret);
}
return methods;
}
use of org.eclipse.ceylon.langtools.tools.javac.code.Symbol in project ceylon by eclipse.
the class JavacClass method getEnclosingMethod.
@Override
public MethodMirror getEnclosingMethod() {
if (!enclosingMethodSet) {
Symbol encl = classSymbol.getEnclosingElement();
if (encl != null && encl instanceof MethodSymbol) {
// it's a method, it must be in a Class
ClassSymbol enclosingClass = (ClassSymbol) encl.getEnclosingElement();
JavacClass enclosingClassMirror = new JavacClass(enclosingClass);
enclosingMethod = new JavacMethod(enclosingClassMirror, (MethodSymbol) encl);
}
enclosingMethodSet = true;
}
return enclosingMethod;
}
use of org.eclipse.ceylon.langtools.tools.javac.code.Symbol in project ceylon by eclipse.
the class Lower method visitIterableForeachLoop.
/**
* A statement of the form
*
* <pre>
* for ( T v : coll ) stmt ;
* </pre>
*
* (where coll implements {@code Iterable<? extends T>}) gets translated to
*
* <pre>{@code
* for ( Iterator<? extends T> #i = coll.iterator(); #i.hasNext(); ) {
* T v = (T) #i.next();
* stmt;
* }
* }</pre>
*
* where #i is a freshly named synthetic local variable.
*/
private void visitIterableForeachLoop(JCEnhancedForLoop tree) {
make_at(tree.expr.pos());
Type iteratorTarget = syms.objectType;
Type iterableType = types.asSuper(types.cvarUpperBound(tree.expr.type), syms.iterableType.tsym);
if (iterableType.getTypeArguments().nonEmpty())
iteratorTarget = types.erasure(iterableType.getTypeArguments().head);
Type eType = tree.expr.type;
while (eType.hasTag(TYPEVAR)) {
eType = eType.getUpperBound();
}
tree.expr.type = types.erasure(eType);
if (eType.isCompound())
tree.expr = make.TypeCast(types.erasure(iterableType), tree.expr);
Symbol iterator = lookupMethod(tree.expr.pos(), names.iterator, eType, List.<Type>nil());
VarSymbol itvar = new VarSymbol(SYNTHETIC, names.fromString("i" + target.syntheticNameChar()), types.erasure(types.asSuper(iterator.type.getReturnType(), syms.iteratorType.tsym)), currentMethodSym);
JCStatement init = make.VarDef(itvar, make.App(make.Select(tree.expr, iterator).setType(types.erasure(iterator.type))));
Symbol hasNext = lookupMethod(tree.expr.pos(), names.hasNext, itvar.type, List.<Type>nil());
JCMethodInvocation cond = make.App(make.Select(make.Ident(itvar), hasNext));
Symbol next = lookupMethod(tree.expr.pos(), names.next, itvar.type, List.<Type>nil());
JCExpression vardefinit = make.App(make.Select(make.Ident(itvar), next));
if (tree.var.type.isPrimitive())
vardefinit = make.TypeCast(types.cvarUpperBound(iteratorTarget), vardefinit);
else
vardefinit = make.TypeCast(tree.var.type, vardefinit);
JCVariableDecl indexDef = (JCVariableDecl) make.VarDef(tree.var.mods, tree.var.name, tree.var.vartype, vardefinit).setType(tree.var.type);
indexDef.sym = tree.var.sym;
JCBlock body = make.Block(0, List.of(indexDef, tree.body));
body.endpos = TreeInfo.endPos(tree.body);
result = translate(make.ForLoop(List.of(init), cond, List.<JCExpressionStatement>nil(), body));
patchTargets(body, tree, result);
}
use of org.eclipse.ceylon.langtools.tools.javac.code.Symbol in project ceylon by eclipse.
the class Lower method accessClass.
/**
* The class in which an access method for given symbol goes.
* @param sym The access symbol
* @param protAccess Is access to a protected symbol in another
* package?
*/
ClassSymbol accessClass(Symbol sym, boolean protAccess, JCTree tree) {
if (protAccess) {
Symbol qualifier = null;
ClassSymbol c = currentClass;
if (tree.hasTag(SELECT) && (sym.flags() & STATIC) == 0) {
qualifier = ((JCFieldAccess) tree).selected.type.tsym;
while (!qualifier.isSubClass(c, types)) {
c = c.owner.enclClass();
}
return c;
} else {
while (!c.isSubClass(sym.owner, types)) {
c = c.owner.enclClass();
}
}
return c;
} else {
// the symbol is private
return sym.owner.enclClass();
}
}
use of org.eclipse.ceylon.langtools.tools.javac.code.Symbol in project ceylon by eclipse.
the class Lower method classDollarSymBody.
/**
* Generate code for class$(String name).
*/
JCBlock classDollarSymBody(DiagnosticPosition pos, JCMethodDecl md) {
MethodSymbol classDollarSym = md.sym;
ClassSymbol outerCacheClass = (ClassSymbol) classDollarSym.owner;
JCBlock returnResult;
// which requires we cache the current loader in cl$
if (target.classLiteralsNoInit()) {
// clsym = "private static ClassLoader cl$"
VarSymbol clsym = new VarSymbol(STATIC | SYNTHETIC, names.fromString("cl" + target.syntheticNameChar()), syms.classLoaderType, outerCacheClass);
enterSynthetic(pos, clsym, outerCacheClass.members());
// emit "private static ClassLoader cl$;"
JCVariableDecl cldef = make.VarDef(clsym, null);
JCClassDecl outerCacheClassDef = classDef(outerCacheClass);
outerCacheClassDef.defs = outerCacheClassDef.defs.prepend(cldef);
// newcache := "new cache$1[0]"
JCNewArray newcache = make.NewArray(make.Type(outerCacheClass.type), List.<JCExpression>of(make.Literal(INT, 0).setType(syms.intType)), null);
newcache.type = new ArrayType(types.erasure(outerCacheClass.type), syms.arrayClass);
// forNameSym := java.lang.Class.forName(
// String s,boolean init,ClassLoader loader)
Symbol forNameSym = lookupMethod(make_pos, names.forName, types.erasure(syms.classType), List.of(syms.stringType, syms.booleanType, syms.classLoaderType));
// clvalue := "(cl$ == null) ?
// $newcache.getClass().getComponentType().getClassLoader() : cl$"
JCExpression clvalue = make.Conditional(makeBinary(EQ, make.Ident(clsym), makeNull()), make.Assign(make.Ident(clsym), makeCall(makeCall(makeCall(newcache, names.getClass, List.<JCExpression>nil()), names.getComponentType, List.<JCExpression>nil()), names.getClassLoader, List.<JCExpression>nil())).setType(syms.classLoaderType), make.Ident(clsym)).setType(syms.classLoaderType);
// returnResult := "{ return Class.forName(param1, false, cl$); }"
List<JCExpression> args = List.of(make.Ident(md.params.head.sym), makeLit(syms.booleanType, 0), clvalue);
returnResult = make.Block(0, List.<JCStatement>of(make.Call(// return
make.App(make.Ident(forNameSym), args))));
} else {
// forNameSym := java.lang.Class.forName(String s)
Symbol forNameSym = lookupMethod(make_pos, names.forName, types.erasure(syms.classType), List.of(syms.stringType));
// returnResult := "{ return Class.forName(param1); }"
returnResult = make.Block(0, List.of(make.Call(// return
make.App(make.QualIdent(forNameSym), List.<JCExpression>of(make.Ident(md.params.head.sym))))));
}
// catchParam := ClassNotFoundException e1
VarSymbol catchParam = new VarSymbol(SYNTHETIC, make.paramName(1), syms.classNotFoundExceptionType, classDollarSym);
JCStatement rethrow;
if (target.hasInitCause()) {
// rethrow = "throw new NoClassDefFoundError().initCause(e);
JCExpression throwExpr = makeCall(makeNewClass(syms.noClassDefFoundErrorType, List.<JCExpression>nil()), names.initCause, List.<JCExpression>of(make.Ident(catchParam)));
rethrow = make.Throw(throwExpr);
} else {
// getMessageSym := ClassNotFoundException.getMessage()
Symbol getMessageSym = lookupMethod(make_pos, names.getMessage, syms.classNotFoundExceptionType, List.<Type>nil());
// rethrow = "throw new NoClassDefFoundError(e.getMessage());"
rethrow = make.Throw(makeNewClass(syms.noClassDefFoundErrorType, List.<JCExpression>of(make.App(make.Select(make.Ident(catchParam), getMessageSym), List.<JCExpression>nil()))));
}
// rethrowStmt := "( $rethrow )"
JCBlock rethrowStmt = make.Block(0, List.of(rethrow));
// catchBlock := "catch ($catchParam) $rethrowStmt"
JCCatch catchBlock = make.Catch(make.VarDef(catchParam, null), rethrowStmt);
// tryCatch := "try $returnResult $catchBlock"
JCStatement tryCatch = make.Try(returnResult, List.of(catchBlock), null);
return make.Block(0, List.of(tryCatch));
}
Aggregations