Java Code Examples for com.sun.tools.javac.code.Symbol.ClassSymbol#hasOuterInstance()

The following examples show how to use com.sun.tools.javac.code.Symbol.ClassSymbol#hasOuterInstance() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Lower.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/** If tree refers to a class instance creation expression
 *  add all free variables of the freshly created class.
 */
public void visitNewClass(JCNewClass tree) {
    ClassSymbol c = (ClassSymbol)tree.constructor.owner;
    if (tree.encl == null &&
        c.hasOuterInstance() &&
        outerThisStack.head != null)
        visitSymbol(outerThisStack.head);
    super.visitNewClass(tree);
}
 
Example 2
Source File: Lower.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/** If tree refers to a superclass constructor call,
 *  add all free variables of the superclass.
 */
public void visitApply(JCMethodInvocation tree) {
    if (TreeInfo.name(tree.meth) == names._super) {
        Symbol constructor = TreeInfo.symbol(tree.meth);
        ClassSymbol c = (ClassSymbol)constructor.owner;
        if (c.hasOuterInstance() &&
            !tree.meth.hasTag(SELECT) &&
            outerThisStack.head != null)
            visitSymbol(outerThisStack.head);
    }
    super.visitApply(tree);
}
 
Example 3
Source File: JavaEnvironment.java    From j2cl with Apache License 2.0 5 votes vote down vote up
/**
 * Returns true if instances of this type capture its outer instances; i.e. if it is an non static
 * member class, or an anonymous or local class defined in an instance context.
 */
static boolean capturesEnclosingInstance(ClassSymbol classSymbol) {
  if (classSymbol.isAnonymous()) {
    return classSymbol.hasOuterInstance() || !isStatic(classSymbol.getEnclosingElement());
  }
  return classSymbol.hasOuterInstance();
}
 
Example 4
Source File: Lower.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public void visitApply(JCMethodInvocation tree) {
    Symbol meth = TreeInfo.symbol(tree.meth);
    List<Type> argtypes = meth.type.getParameterTypes();
    if (meth.name == names.init && meth.owner == syms.enumSym)
        argtypes = argtypes.tail.tail;
    tree.args = boxArgs(argtypes, tree.args, tree.varargsElement);
    tree.varargsElement = null;
    Name methName = TreeInfo.name(tree.meth);
    if (meth.name==names.init) {
        // We are seeing a this(...) or super(...) constructor call.
        // If an access constructor is used, append null as a last argument.
        Symbol constructor = accessConstructor(tree.pos(), meth);
        if (constructor != meth) {
            tree.args = tree.args.append(makeNull());
            TreeInfo.setSymbol(tree.meth, constructor);
        }

        // If we are calling a constructor of a local class, add
        // free variables after explicit constructor arguments.
        ClassSymbol c = (ClassSymbol)constructor.owner;
        if (c.isLocal()) {
            tree.args = tree.args.appendList(loadFreevars(tree.pos(), freevars(c)));
        }

        // If we are calling a constructor of an enum class, pass
        // along the name and ordinal arguments
        if ((c.flags_field&ENUM) != 0 || c.getQualifiedName() == names.java_lang_Enum) {
            List<JCVariableDecl> params = currentMethodDef.params;
            if (currentMethodSym.owner.hasOuterInstance())
                params = params.tail; // drop this$n
            tree.args = tree.args
                .prepend(make_at(tree.pos()).Ident(params.tail.head.sym)) // ordinal
                .prepend(make.Ident(params.head.sym)); // name
        }

        // If we are calling a constructor of a class with an outer
        // instance, and the call
        // is qualified, pass qualifier as first argument in front of
        // the explicit constructor arguments. If the call
        // is not qualified, pass the correct outer instance as
        // first argument.
        if (c.hasOuterInstance()) {
            JCExpression thisArg;
            if (tree.meth.hasTag(SELECT)) {
                thisArg = attr.
                    makeNullCheck(translate(((JCFieldAccess) tree.meth).selected));
                tree.meth = make.Ident(constructor);
                ((JCIdent) tree.meth).name = methName;
            } else if (c.isLocal() || methName == names._this){
                // local class or this() call
                thisArg = makeThis(tree.meth.pos(), c.type.getEnclosingType().tsym);
            } else {
                // super() call of nested class - never pick 'this'
                thisArg = makeOwnerThisN(tree.meth.pos(), c, false);
            }
            tree.args = tree.args.prepend(thisArg);
        }
    } else {
        // We are seeing a normal method invocation; translate this as usual.
        tree.meth = translate(tree.meth);

        // If the translated method itself is an Apply tree, we are
        // seeing an access method invocation. In this case, append
        // the method arguments to the arguments of the access method.
        if (tree.meth.hasTag(APPLY)) {
            JCMethodInvocation app = (JCMethodInvocation)tree.meth;
            app.args = tree.args.prependList(app.args);
            result = app;
            return;
        }
    }
    result = tree;
}