Search in sources :

Example 11 with JCExpressionStatement

use of com.sun.tools.javac.tree.JCTree.JCExpressionStatement in project lombok by rzwitserloot.

the class JavacHandlerUtil method isConstructorCall.

public static boolean isConstructorCall(final JCStatement statement) {
    if (!(statement instanceof JCExpressionStatement))
        return false;
    JCExpression expr = ((JCExpressionStatement) statement).expr;
    if (!(expr instanceof JCMethodInvocation))
        return false;
    JCExpression invocation = ((JCMethodInvocation) expr).meth;
    String name;
    if (invocation instanceof JCFieldAccess) {
        name = ((JCFieldAccess) invocation).name.toString();
    } else if (invocation instanceof JCIdent) {
        name = ((JCIdent) invocation).name.toString();
    } else {
        name = "";
    }
    return "super".equals(name) || "this".equals(name);
}
Also used : JCMethodInvocation(com.sun.tools.javac.tree.JCTree.JCMethodInvocation) JCIdent(com.sun.tools.javac.tree.JCTree.JCIdent) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) JCFieldAccess(com.sun.tools.javac.tree.JCTree.JCFieldAccess) JCExpressionStatement(com.sun.tools.javac.tree.JCTree.JCExpressionStatement)

Example 12 with JCExpressionStatement

use of com.sun.tools.javac.tree.JCTree.JCExpressionStatement in project ceylon-compiler by ceylon.

the class ClassTransformer method makeCompanionInstanceAssignment.

/**
     * Returns the companion instances assignment expression used in the constructor,
     * e.g.
     * <pre>
     * this.$ceylon$language$Enumerable$this$ = new .ceylon.language.Enumerable$impl<.com.redhat.ceylon.compiler.java.test.structure.klass.SerializableEnumerable>(.com.redhat.ceylon.compiler.java.test.structure.klass.SerializableEnumerable.$TypeDescriptor$, this);
     * </pre>
     * @param classBuilder 
     * @return 
     */
private JCExpressionStatement makeCompanionInstanceAssignment(final Class model, final Interface iface, final Type satisfiedType) {
    final Type bestSatisfiedType = getBestSatisfiedType(model.getType(), iface);
    JCExpression containerInstance = null;
    if (!Decl.isToplevel(iface) && !Decl.isLocal(iface)) {
        // if it's a member type we need to qualify the new instance with its $impl container
        ClassOrInterface interfaceContainer = Decl.getClassOrInterfaceContainer(iface, false);
        if (interfaceContainer instanceof Interface) {
            ClassOrInterface modelContainer = model;
            // first try to find exactly the interface we are looking for
            while ((modelContainer = Decl.getClassOrInterfaceContainer(modelContainer, false)) != null && !modelContainer.equals(interfaceContainer)) {
            // keep searching
            }
            // then find one that inherits it
            if (modelContainer == null) {
                modelContainer = model;
                while ((modelContainer = Decl.getClassOrInterfaceContainer(modelContainer, false)) != null && modelContainer.getType().getSupertype(interfaceContainer) == null) {
                // keep searching
                }
            }
            if (modelContainer == null) {
                throw new BugException("Could not find container that satisfies interface " + iface.getQualifiedNameString() + " to find qualifying instance for companion instance for " + model.getQualifiedNameString());
            }
            // if it's an interface we just qualify it properly
            if (modelContainer instanceof Interface) {
                JCExpression containerType = makeJavaType(modelContainer.getType(), JT_COMPANION | JT_SATISFIES | JT_RAW);
                containerInstance = makeSelect(containerType, "this");
            } else {
                // it's a class: find the right field used for the interface container impl
                String containerFieldName = getCompanionFieldName((Interface) interfaceContainer);
                JCExpression containerType = makeJavaType(modelContainer.getType(), JT_SATISFIES);
                containerInstance = makeSelect(makeSelect(containerType, "this"), containerFieldName);
            }
        }
    }
    List<JCExpression> state = List.nil();
    // pass all reified type info to the constructor
    for (JCExpression t : makeReifiedTypeArguments(satisfiedType)) {
        state = state.append(t);
    }
    // pass the instance of this
    state = state.append(expressionGen().applyErasureAndBoxing(naming.makeThis(), model.getType(), false, true, BoxingStrategy.BOXED, bestSatisfiedType, ExpressionTransformer.EXPR_FOR_COMPANION));
    final JCExpression ifaceImplType;
    if (!Decl.isToplevel(iface) && !Decl.isLocal(iface) && Decl.getClassOrInterfaceContainer(iface, false) instanceof Interface) {
        ifaceImplType = makeJavaType(bestSatisfiedType, JT_COMPANION | JT_CLASS_NEW | JT_NON_QUALIFIED);
    } else {
        ifaceImplType = makeJavaType(bestSatisfiedType, JT_COMPANION | JT_CLASS_NEW);
    }
    JCExpression newInstance = make().NewClass(containerInstance, null, ifaceImplType, state, null);
    JCExpressionStatement companionInstanceAssign = make().Exec(make().Assign(// TODO Use qualified name for quoting? 
    makeSelect("this", getCompanionFieldName(iface)), newInstance));
    return companionInstanceAssign;
}
Also used : ClassOrInterface(com.redhat.ceylon.model.typechecker.model.ClassOrInterface) Type(com.redhat.ceylon.model.typechecker.model.Type) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) JCExpressionStatement(com.sun.tools.javac.tree.JCTree.JCExpressionStatement) ClassOrInterface(com.redhat.ceylon.model.typechecker.model.ClassOrInterface) LazyInterface(com.redhat.ceylon.model.loader.model.LazyInterface) Interface(com.redhat.ceylon.model.typechecker.model.Interface)

Aggregations

JCExpressionStatement (com.sun.tools.javac.tree.JCTree.JCExpressionStatement)12 JCExpression (com.sun.tools.javac.tree.JCTree.JCExpression)7 JCMethodInvocation (com.sun.tools.javac.tree.JCTree.JCMethodInvocation)5 JCStatement (com.sun.tools.javac.tree.JCTree.JCStatement)5 JCTree (com.sun.tools.javac.tree.JCTree)3 JCIf (com.sun.tools.javac.tree.JCTree.JCIf)3 JCMethodDecl (com.sun.tools.javac.tree.JCTree.JCMethodDecl)3 JCVariableDecl (com.sun.tools.javac.tree.JCTree.JCVariableDecl)3 JCAnnotation (com.sun.tools.javac.tree.JCTree.JCAnnotation)2 JCAssign (com.sun.tools.javac.tree.JCTree.JCAssign)2 JCBinary (com.sun.tools.javac.tree.JCTree.JCBinary)2 JCBlock (com.sun.tools.javac.tree.JCTree.JCBlock)2 JCCatch (com.sun.tools.javac.tree.JCTree.JCCatch)2 JCCompilationUnit (com.sun.tools.javac.tree.JCTree.JCCompilationUnit)2 JCFieldAccess (com.sun.tools.javac.tree.JCTree.JCFieldAccess)2 JCIdent (com.sun.tools.javac.tree.JCTree.JCIdent)2 JCNewClass (com.sun.tools.javac.tree.JCTree.JCNewClass)2 JCTry (com.sun.tools.javac.tree.JCTree.JCTry)2 LetExpr (com.sun.tools.javac.tree.JCTree.LetExpr)2 Pretty (com.sun.tools.javac.tree.Pretty)2