Java Code Examples for com.sun.tools.javac.code.Symbol.MethodSymbol#isStatic()

The following examples show how to use com.sun.tools.javac.code.Symbol.MethodSymbol#isStatic() . 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
@DefinedBy(Api.LANGUAGE_MODEL)
public boolean overrides(ExecutableElement riderEl,
                         ExecutableElement rideeEl, TypeElement typeEl) {
    MethodSymbol rider = cast(MethodSymbol.class, riderEl);
    MethodSymbol ridee = cast(MethodSymbol.class, rideeEl);
    ClassSymbol origin = cast(ClassSymbol.class, typeEl);

    return rider.name == ridee.name &&

           // not reflexive as per JLS
           rider != ridee &&

           // we don't care if ridee is static, though that wouldn't
           // compile
           !rider.isStatic() &&

           // Symbol.overrides assumes the following
           ridee.isMemberOf(origin, types) &&

           // check access and signatures; don't check return types
           rider.overrides(ridee, origin, types, false);
}
 
Example 2
public boolean overrides(ExecutableElement e1, ExecutableElement e2, TypeElement cls) {
    MethodSymbol rider = (MethodSymbol)e1;
    MethodSymbol ridee = (MethodSymbol)e2;
    ClassSymbol origin = (ClassSymbol)cls;

    return rider.name == ridee.name &&

           // not reflexive as per JLS
           rider != ridee &&

           // we don't care if ridee is static, though that wouldn't
           // compile
           !rider.isStatic() &&

           // Symbol.overrides assumes the following
           ridee.isMemberOf(origin, toolEnv.getTypes()) &&

           // check access, signatures and check return types
           rider.overrides(ridee, origin, toolEnv.getTypes(), true);
}
 
Example 3
MethodSymbol factoryMethod(ClassSymbol tsym) {
    for (Symbol sym : tsym.members().getSymbolsByName(names.provider, sym -> sym.kind == MTH)) {
        MethodSymbol mSym = (MethodSymbol)sym;
        if (mSym.isStatic() && (mSym.flags() & Flags.PUBLIC) != 0 && mSym.params().isEmpty()) {
            return mSym;
        }
    }
    return null;
}
 
Example 4
Source Project: j2cl   File: CompilationUnitBuilder.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Returns a qualifier for a method invocation that doesn't have one, specifically,
 * instanceMethod() will return a resolved qualifier that may refer to "this" or to the enclosing
 * instances. A staticMethod() will return null.
 */
private static JCExpression getExplicitQualifier(JCMethodInvocation methodInvocation) {

  if (methodInvocation.getMethodSelect().getKind() != Kind.IDENTIFIER) {
    return getQualifier(methodInvocation.getMethodSelect());
  }

  // No qualifier specified.
  MethodSymbol memberSymbol = getMemberSymbol(methodInvocation.getMethodSelect());
  if (memberSymbol.isStatic()) {
    return null;
  }

  return getQualifier(methodInvocation.getMethodSelect());
}
 
Example 5
MethodSymbol factoryMethod(ClassSymbol tsym) {
    for (Symbol sym : tsym.members().getSymbolsByName(names.provider, sym -> sym.kind == MTH)) {
        MethodSymbol mSym = (MethodSymbol)sym;
        if (mSym.isStatic() && (mSym.flags() & Flags.PUBLIC) != 0 && mSym.params().isEmpty()) {
            return mSym;
        }
    }
    return null;
}
 
Example 6
/**
 * Translate a lambda into a method to be inserted into the class.
 * Then replace the lambda site with an invokedynamic call of to lambda
 * meta-factory, which will use the lambda method.
 * @param tree
 */
@Override
public void visitLambda(JCLambda tree) {
    LambdaTranslationContext localContext = (LambdaTranslationContext)context;
    MethodSymbol sym = localContext.translatedSym;
    MethodType lambdaType = (MethodType) sym.type;

    {
        Symbol owner = localContext.owner;
        ListBuffer<Attribute.TypeCompound> ownerTypeAnnos = new ListBuffer<Attribute.TypeCompound>();
        ListBuffer<Attribute.TypeCompound> lambdaTypeAnnos = new ListBuffer<Attribute.TypeCompound>();

        for (Attribute.TypeCompound tc : owner.getRawTypeAttributes()) {
            if (tc.position.onLambda == tree) {
                lambdaTypeAnnos.append(tc);
            } else {
                ownerTypeAnnos.append(tc);
            }
        }
        if (lambdaTypeAnnos.nonEmpty()) {
            owner.setTypeAttributes(ownerTypeAnnos.toList());
            sym.setTypeAttributes(lambdaTypeAnnos.toList());
        }
    }

    //create the method declaration hoisting the lambda body
    JCMethodDecl lambdaDecl = make.MethodDef(make.Modifiers(sym.flags_field),
            sym.name,
            make.QualIdent(lambdaType.getReturnType().tsym),
            List.<JCTypeParameter>nil(),
            localContext.syntheticParams,
            lambdaType.getThrownTypes() == null ?
                List.<JCExpression>nil() :
                make.Types(lambdaType.getThrownTypes()),
            null,
            null);
    lambdaDecl.sym = sym;
    lambdaDecl.type = lambdaType;

    //translate lambda body
    //As the lambda body is translated, all references to lambda locals,
    //captured variables, enclosing members are adjusted accordingly
    //to refer to the static method parameters (rather than i.e. acessing to
    //captured members directly).
    lambdaDecl.body = translate(makeLambdaBody(tree, lambdaDecl));

    //Add the method to the list of methods to be added to this class.
    kInfo.addMethod(lambdaDecl);

    //now that we have generated a method for the lambda expression,
    //we can translate the lambda into a method reference pointing to the newly
    //created method.
    //
    //Note that we need to adjust the method handle so that it will match the
    //signature of the SAM descriptor - this means that the method reference
    //should be added the following synthetic arguments:
    //
    // * the "this" argument if it is an instance method
    // * enclosing locals captured by the lambda expression

    ListBuffer<JCExpression> syntheticInits = new ListBuffer<>();

    if (localContext.methodReferenceReceiver != null) {
        syntheticInits.append(localContext.methodReferenceReceiver);
    } else if (!sym.isStatic()) {
        syntheticInits.append(makeThis(
                sym.owner.enclClass().asType(),
                localContext.owner.enclClass()));
    }

    //add captured locals
    for (Symbol fv : localContext.getSymbolMap(CAPTURED_VAR).keySet()) {
        if (fv != localContext.self) {
            JCTree captured_local = make.Ident(fv).setType(fv.type);
            syntheticInits.append((JCExpression) captured_local);
        }
    }

    //then, determine the arguments to the indy call
    List<JCExpression> indy_args = translate(syntheticInits.toList(), localContext.prev);

    //build a sam instance using an indy call to the meta-factory
    int refKind = referenceKind(sym);

    //convert to an invokedynamic call
    result = makeMetafactoryIndyCall(context, refKind, sym, indy_args);
}
 
Example 7
/**
 * Translate a lambda into a method to be inserted into the class.
 * Then replace the lambda site with an invokedynamic call of to lambda
 * meta-factory, which will use the lambda method.
 * @param tree
 */
@Override
public void visitLambda(JCLambda tree) {
    LambdaTranslationContext localContext = (LambdaTranslationContext)context;
    MethodSymbol sym = localContext.translatedSym;
    MethodType lambdaType = (MethodType) sym.type;

    {
        Symbol owner = localContext.owner;
        ListBuffer<Attribute.TypeCompound> ownerTypeAnnos = new ListBuffer<Attribute.TypeCompound>();
        ListBuffer<Attribute.TypeCompound> lambdaTypeAnnos = new ListBuffer<Attribute.TypeCompound>();

        for (Attribute.TypeCompound tc : owner.getRawTypeAttributes()) {
            if (tc.position.onLambda == tree) {
                lambdaTypeAnnos.append(tc);
            } else {
                ownerTypeAnnos.append(tc);
            }
        }
        if (lambdaTypeAnnos.nonEmpty()) {
            owner.setTypeAttributes(ownerTypeAnnos.toList());
            sym.setTypeAttributes(lambdaTypeAnnos.toList());
        }
    }

    //create the method declaration hoisting the lambda body
    JCMethodDecl lambdaDecl = make.MethodDef(make.Modifiers(sym.flags_field),
            sym.name,
            make.QualIdent(lambdaType.getReturnType().tsym),
            List.<JCTypeParameter>nil(),
            localContext.syntheticParams,
            lambdaType.getThrownTypes() == null ?
                List.<JCExpression>nil() :
                make.Types(lambdaType.getThrownTypes()),
            null,
            null);
    lambdaDecl.sym = sym;
    lambdaDecl.type = lambdaType;

    //translate lambda body
    //As the lambda body is translated, all references to lambda locals,
    //captured variables, enclosing members are adjusted accordingly
    //to refer to the static method parameters (rather than i.e. acessing to
    //captured members directly).
    lambdaDecl.body = translate(makeLambdaBody(tree, lambdaDecl));

    //Add the method to the list of methods to be added to this class.
    kInfo.addMethod(lambdaDecl);

    //now that we have generated a method for the lambda expression,
    //we can translate the lambda into a method reference pointing to the newly
    //created method.
    //
    //Note that we need to adjust the method handle so that it will match the
    //signature of the SAM descriptor - this means that the method reference
    //should be added the following synthetic arguments:
    //
    // * the "this" argument if it is an instance method
    // * enclosing locals captured by the lambda expression

    ListBuffer<JCExpression> syntheticInits = new ListBuffer<>();

    if (localContext.methodReferenceReceiver != null) {
        syntheticInits.append(localContext.methodReferenceReceiver);
    } else if (!sym.isStatic()) {
        syntheticInits.append(makeThis(
                sym.owner.enclClass().asType(),
                localContext.owner.enclClass()));
    }

    //add captured locals
    for (Symbol fv : localContext.getSymbolMap(CAPTURED_VAR).keySet()) {
        if (fv != localContext.self) {
            JCTree captured_local = make.Ident(fv).setType(fv.type);
            syntheticInits.append((JCExpression) captured_local);
        }
    }

    //then, determine the arguments to the indy call
    List<JCExpression> indy_args = translate(syntheticInits.toList(), localContext.prev);

    //build a sam instance using an indy call to the meta-factory
    int refKind = referenceKind(sym);

    //convert to an invokedynamic call
    result = makeMetafactoryIndyCall(context, refKind, sym, indy_args);
}
 
Example 8
/**
 * Translate a lambda into a method to be inserted into the class.
 * Then replace the lambda site with an invokedynamic call of to lambda
 * meta-factory, which will use the lambda method.
 * @param tree
 */
@Override
public void visitLambda(JCLambda tree) {
    LambdaTranslationContext localContext = (LambdaTranslationContext)context;
    MethodSymbol sym = (MethodSymbol)localContext.translatedSym;
    MethodType lambdaType = (MethodType) sym.type;

    {
        Symbol owner = localContext.owner;
        ListBuffer<Attribute.TypeCompound> ownerTypeAnnos = new ListBuffer<Attribute.TypeCompound>();
        ListBuffer<Attribute.TypeCompound> lambdaTypeAnnos = new ListBuffer<Attribute.TypeCompound>();

        for (Attribute.TypeCompound tc : owner.getRawTypeAttributes()) {
            if (tc.position.onLambda == tree) {
                lambdaTypeAnnos.append(tc);
            } else {
                ownerTypeAnnos.append(tc);
            }
        }
        if (lambdaTypeAnnos.nonEmpty()) {
            owner.setTypeAttributes(ownerTypeAnnos.toList());
            sym.setTypeAttributes(lambdaTypeAnnos.toList());
        }
    }

    //create the method declaration hoisting the lambda body
    JCMethodDecl lambdaDecl = make.MethodDef(make.Modifiers(sym.flags_field),
            sym.name,
            make.QualIdent(lambdaType.getReturnType().tsym),
            List.<JCTypeParameter>nil(),
            localContext.syntheticParams,
            lambdaType.getThrownTypes() == null ?
                List.<JCExpression>nil() :
                make.Types(lambdaType.getThrownTypes()),
            null,
            null);
    lambdaDecl.sym = sym;
    lambdaDecl.type = lambdaType;

    //translate lambda body
    //As the lambda body is translated, all references to lambda locals,
    //captured variables, enclosing members are adjusted accordingly
    //to refer to the static method parameters (rather than i.e. acessing to
    //captured members directly).
    lambdaDecl.body = translate(makeLambdaBody(tree, lambdaDecl));

    //Add the method to the list of methods to be added to this class.
    kInfo.addMethod(lambdaDecl);

    //now that we have generated a method for the lambda expression,
    //we can translate the lambda into a method reference pointing to the newly
    //created method.
    //
    //Note that we need to adjust the method handle so that it will match the
    //signature of the SAM descriptor - this means that the method reference
    //should be added the following synthetic arguments:
    //
    // * the "this" argument if it is an instance method
    // * enclosing locals captured by the lambda expression

    ListBuffer<JCExpression> syntheticInits = new ListBuffer<>();

    if (!sym.isStatic()) {
        syntheticInits.append(makeThis(
                sym.owner.enclClass().asType(),
                localContext.owner.enclClass()));
    }

    //add captured locals
    for (Symbol fv : localContext.getSymbolMap(CAPTURED_VAR).keySet()) {
        if (fv != localContext.self) {
            JCTree captured_local = make.Ident(fv).setType(fv.type);
            syntheticInits.append((JCExpression) captured_local);
        }
    }

    //then, determine the arguments to the indy call
    List<JCExpression> indy_args = translate(syntheticInits.toList(), localContext.prev);

    //build a sam instance using an indy call to the meta-factory
    int refKind = referenceKind(sym);

    //convert to an invokedynamic call
    result = makeMetafactoryIndyCall(context, refKind, sym, indy_args);
}
 
Example 9
/**
 * Translate a lambda into a method to be inserted into the class.
 * Then replace the lambda site with an invokedynamic call of to lambda
 * meta-factory, which will use the lambda method.
 * @param tree
 */
@Override
public void visitLambda(JCLambda tree) {
    LambdaTranslationContext localContext = (LambdaTranslationContext)context;
    MethodSymbol sym = (MethodSymbol)localContext.translatedSym;
    MethodType lambdaType = (MethodType) sym.type;

    {
        Symbol owner = localContext.owner;
        ListBuffer<Attribute.TypeCompound> ownerTypeAnnos = new ListBuffer<Attribute.TypeCompound>();
        ListBuffer<Attribute.TypeCompound> lambdaTypeAnnos = new ListBuffer<Attribute.TypeCompound>();

        for (Attribute.TypeCompound tc : owner.getRawTypeAttributes()) {
            if (tc.position.onLambda == tree) {
                lambdaTypeAnnos.append(tc);
            } else {
                ownerTypeAnnos.append(tc);
            }
        }
        if (lambdaTypeAnnos.nonEmpty()) {
            owner.setTypeAttributes(ownerTypeAnnos.toList());
            sym.setTypeAttributes(lambdaTypeAnnos.toList());
        }
    }

    //create the method declaration hoisting the lambda body
    JCMethodDecl lambdaDecl = make.MethodDef(make.Modifiers(sym.flags_field),
            sym.name,
            make.QualIdent(lambdaType.getReturnType().tsym),
            List.<JCTypeParameter>nil(),
            localContext.syntheticParams,
            lambdaType.getThrownTypes() == null ?
                List.<JCExpression>nil() :
                make.Types(lambdaType.getThrownTypes()),
            null,
            null);
    lambdaDecl.sym = sym;
    lambdaDecl.type = lambdaType;

    //translate lambda body
    //As the lambda body is translated, all references to lambda locals,
    //captured variables, enclosing members are adjusted accordingly
    //to refer to the static method parameters (rather than i.e. acessing to
    //captured members directly).
    lambdaDecl.body = translate(makeLambdaBody(tree, lambdaDecl));

    //Add the method to the list of methods to be added to this class.
    kInfo.addMethod(lambdaDecl);

    //now that we have generated a method for the lambda expression,
    //we can translate the lambda into a method reference pointing to the newly
    //created method.
    //
    //Note that we need to adjust the method handle so that it will match the
    //signature of the SAM descriptor - this means that the method reference
    //should be added the following synthetic arguments:
    //
    // * the "this" argument if it is an instance method
    // * enclosing locals captured by the lambda expression

    ListBuffer<JCExpression> syntheticInits = new ListBuffer<>();

    if (!sym.isStatic()) {
        syntheticInits.append(makeThis(
                sym.owner.enclClass().asType(),
                localContext.owner.enclClass()));
    }

    //add captured locals
    for (Symbol fv : localContext.getSymbolMap(CAPTURED_VAR).keySet()) {
        if (fv != localContext.self) {
            JCTree captured_local = make.Ident(fv).setType(fv.type);
            syntheticInits.append((JCExpression) captured_local);
        }
    }

    //then, determine the arguments to the indy call
    List<JCExpression> indy_args = translate(syntheticInits.toList(), localContext.prev);

    //build a sam instance using an indy call to the meta-factory
    int refKind = referenceKind(sym);

    //convert to an invokedynamic call
    result = makeMetafactoryIndyCall(context, refKind, sym, indy_args);
}