Search in sources :

Example 16 with JCFieldAccess

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

the class ImportScanner method visitTopLevel.

@Override
public void visitTopLevel(JCCompilationUnit that) {
    JCExpression pid = that.pid;
    String pkgName;
    if (pid instanceof JCFieldAccess) {
        pkgName = ((JCFieldAccess) pid).toString();
    } else if (pid instanceof JCIdent) {
        pkgName = ((JCIdent) pid).toString();
    } else {
        // default package
        pkgName = "";
    }
    Package thisPackage = modelLoader.findPackage(pkgName);
    if (thisPackage == null)
        // give up
        return;
    importingModule = thisPackage.getModule();
    // Ugly special case where we skip the test when we're compiling the language module itself
    if (importingModule == modelLoader.getLanguageModule())
        return;
    visit(that.defs);
}
Also used : JCIdent(com.sun.tools.javac.tree.JCTree.JCIdent) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) JCFieldAccess(com.sun.tools.javac.tree.JCTree.JCFieldAccess) Package(com.redhat.ceylon.model.typechecker.model.Package)

Example 17 with JCFieldAccess

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

the class ImportScanner method importPackage.

private void importPackage(JCFieldAccess selected) {
    String importedPkg = selected.toString();
    if (importedPkg.isEmpty())
        return;
    Package importedPackage = importingModule.getPackage(importedPkg);
    if (importedPackage == null) {
        // try one level up to skip potential types
        if (selected.selected instanceof JCFieldAccess) {
            importPackage((JCFieldAccess) selected.selected);
        }
    }
}
Also used : JCFieldAccess(com.sun.tools.javac.tree.JCTree.JCFieldAccess) Package(com.redhat.ceylon.model.typechecker.model.Package)

Example 18 with JCFieldAccess

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

the class ClassTransformer method transformObject.

private List<JCTree> transformObject(Node def, Tree.Declaration annotated, Tree.SatisfiedTypes satisfiesTypes, Value model, Class klass, ClassDefinitionBuilder containingClassBuilder, boolean makeLocalInstance) {
    naming.clearSubstitutions(klass);
    String name = klass.getName();
    String javaClassName = Naming.quoteClassName(name);
    ClassDefinitionBuilder objectClassBuilder = ClassDefinitionBuilder.object(this, javaClassName, name, Decl.isLocal(klass)).forDefinition(klass);
    if (Strategy.introduceJavaIoSerializable(klass, typeFact().getJavaIoSerializable())) {
        objectClassBuilder.introduce(make().QualIdent(syms().serializableType.tsym));
        if (def instanceof Tree.ObjectDefinition && klass.isMember() && (klass.isShared() || klass.isCaptured() || model.isCaptured())) {
            addWriteReplace(klass, objectClassBuilder);
        }
    }
    makeReadResolve(objectClassBuilder, klass, model);
    // Make sure top types satisfy reified type
    addReifiedTypeInterface(objectClassBuilder, klass);
    if (supportsReifiedAlias(klass))
        objectClassBuilder.reifiedAlias(klass.getType());
    CeylonVisitor visitor = gen().visitor;
    final ListBuffer<JCTree> prevDefs = visitor.defs;
    final boolean prevInInitializer = visitor.inInitializer;
    final ClassDefinitionBuilder prevClassBuilder = visitor.classBuilder;
    List<JCStatement> childDefs;
    try {
        visitor.defs = new ListBuffer<JCTree>();
        visitor.inInitializer = true;
        visitor.classBuilder = objectClassBuilder;
        def.visitChildren(visitor);
        childDefs = (List<JCStatement>) visitor.getResult().toList();
    } finally {
        visitor.classBuilder = prevClassBuilder;
        visitor.inInitializer = prevInInitializer;
        visitor.defs = prevDefs;
    }
    addMissingUnrefinedMembers(def, klass, objectClassBuilder);
    satisfaction(satisfiesTypes, klass, objectClassBuilder);
    serialization(klass, objectClassBuilder);
    if (model != null && Decl.isToplevel(model) && def instanceof Tree.ObjectDefinition) {
        // generate a field and getter
        AttributeDefinitionBuilder builder = AttributeDefinitionBuilder.wrapped(this, null, objectClassBuilder, model.getName(), model, true).userAnnotations(makeAtIgnore()).userAnnotationsSetter(makeAtIgnore()).immutable().initialValue(makeNewClass(naming.makeName(model, Naming.NA_FQ | Naming.NA_WRAPPER))).is(PUBLIC, Decl.isShared(klass)).is(STATIC, true);
        if (annotated != null) {
            builder.fieldAnnotations(expressionGen().transformAnnotations(OutputElement.FIELD, annotated));
            builder.userAnnotations(expressionGen().transformAnnotations(OutputElement.GETTER, annotated));
        }
        objectClassBuilder.defs(builder.build());
    }
    if (annotated != null) {
        objectClassBuilder.annotations(expressionGen().transformAnnotations(OutputElement.TYPE, annotated));
        objectClassBuilder.getInitBuilder().userAnnotations(expressionGen().transformAnnotations(OutputElement.CONSTRUCTOR, annotated));
    }
    // make sure we set the container in case we move it out
    addAtContainer(objectClassBuilder, klass);
    objectClassBuilder.annotations(makeAtObject()).satisfies(klass.getSatisfiedTypes()).defs((List) childDefs);
    objectClassBuilder.getInitBuilder().modifiers(PRIVATE);
    objectClassBuilder.addGetTypeMethod(klass.getType());
    if (model != null)
        objectClassBuilder.modelAnnotations(model.getAnnotations()).modifiers(transformObjectDeclFlags(model));
    List<JCTree> result = objectClassBuilder.build();
    if (makeLocalInstance) {
        if (model.isSelfCaptured()) {
            // if it's captured we need to box it and define the var before the class, so it can access it
            JCNewClass newInstance = makeNewClass(objectClassBuilder.getClassName(), false, null);
            JCFieldAccess setter = naming.makeSelect(Naming.getLocalValueName(model), Naming.getSetterName(model));
            JCStatement assign = make().Exec(make().Assign(setter, newInstance));
            result = result.prepend(assign);
            JCVariableDecl localDecl = makeVariableBoxDecl(null, model);
            result = result.prepend(localDecl);
        } else {
            // not captured, we can define the var after the class
            JCVariableDecl localDecl = makeLocalIdentityInstance(name, objectClassBuilder.getClassName(), false);
            result = result.append(localDecl);
        }
    } else if (model != null && Decl.withinClassOrInterface(model)) {
        boolean generateGetter = Decl.isCaptured(model);
        JCExpression type = makeJavaType(klass.getType());
        if (generateGetter) {
            int modifiers = TRANSIENT | PRIVATE;
            JCExpression initialValue = makeNull();
            containingClassBuilder.field(modifiers, name, type, initialValue, false);
            AttributeDefinitionBuilder getter = AttributeDefinitionBuilder.getter(this, name, model).modifiers(transformAttributeGetSetDeclFlags(model, false));
            if (def instanceof Tree.ObjectDefinition) {
                getter.userAnnotations(expressionGen().transformAnnotations(OutputElement.GETTER, ((Tree.ObjectDefinition) def)));
            }
            ListBuffer<JCStatement> stmts = ListBuffer.<JCStatement>lb();
            stmts.add(make().If(make().Binary(JCTree.EQ, naming.makeUnquotedIdent(Naming.quoteFieldName(name)), makeNull()), make().Exec(make().Assign(naming.makeUnquotedIdent(Naming.quoteFieldName(name)), makeNewClass(makeJavaType(klass.getType()), null))), null));
            stmts.add(make().Return(naming.makeUnquotedIdent(Naming.quoteFieldName(name))));
            getter.getterBlock(make().Block(0, stmts.toList()));
            result = result.appendList(getter.build());
        } else {
            int modifiers = FINAL;
            JCExpression initialValue = makeNewClass(makeJavaType(klass.getType()), null);
            containingClassBuilder.field(modifiers, name, type, initialValue, true);
        }
    }
    return result;
}
Also used : JCFieldAccess(com.sun.tools.javac.tree.JCTree.JCFieldAccess) ListBuffer(com.sun.tools.javac.util.ListBuffer) JCTree(com.sun.tools.javac.tree.JCTree) JCStatement(com.sun.tools.javac.tree.JCTree.JCStatement) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) JCPrimitiveTypeTree(com.sun.tools.javac.tree.JCTree.JCPrimitiveTypeTree) JCTree(com.sun.tools.javac.tree.JCTree) Tree(com.redhat.ceylon.compiler.typechecker.tree.Tree) JCNewClass(com.sun.tools.javac.tree.JCTree.JCNewClass)

Example 19 with JCFieldAccess

use of com.sun.tools.javac.tree.JCTree.JCFieldAccess in project error-prone by google.

the class ExpressionTemplate method getPrecedence.

/**
   * Returns the precedence level appropriate for unambiguously printing
   * leaf as a subexpression of its parent.
   */
private static int getPrecedence(JCTree leaf, Context context) {
    JCCompilationUnit comp = context.get(JCCompilationUnit.class);
    JCTree parent = TreeInfo.pathFor(leaf, comp).get(1);
    if (parent instanceof JCConditional) {
        // This intentionally differs from Pretty, because Pretty appears buggy:
        // http://mail.openjdk.java.net/pipermail/compiler-dev/2013-September/007303.html
        JCConditional conditional = (JCConditional) parent;
        return TreeInfo.condPrec + ((conditional.cond == leaf) ? 1 : 0);
    } else if (parent instanceof JCAssign) {
        JCAssign assign = (JCAssign) parent;
        return TreeInfo.assignPrec + ((assign.lhs == leaf) ? 1 : 0);
    } else if (parent instanceof JCAssignOp) {
        JCAssignOp assignOp = (JCAssignOp) parent;
        return TreeInfo.assignopPrec + ((assignOp.lhs == leaf) ? 1 : 0);
    } else if (parent instanceof JCUnary) {
        return TreeInfo.opPrec(parent.getTag());
    } else if (parent instanceof JCBinary) {
        JCBinary binary = (JCBinary) parent;
        return TreeInfo.opPrec(parent.getTag()) + ((binary.rhs == leaf) ? 1 : 0);
    } else if (parent instanceof JCTypeCast) {
        JCTypeCast typeCast = (JCTypeCast) parent;
        return (typeCast.expr == leaf) ? TreeInfo.prefixPrec : TreeInfo.noPrec;
    } else if (parent instanceof JCInstanceOf) {
        JCInstanceOf instanceOf = (JCInstanceOf) parent;
        return TreeInfo.ordPrec + ((instanceOf.clazz == leaf) ? 1 : 0);
    } else if (parent instanceof JCArrayAccess) {
        JCArrayAccess arrayAccess = (JCArrayAccess) parent;
        return (arrayAccess.indexed == leaf) ? TreeInfo.postfixPrec : TreeInfo.noPrec;
    } else if (parent instanceof JCFieldAccess) {
        JCFieldAccess fieldAccess = (JCFieldAccess) parent;
        return (fieldAccess.selected == leaf) ? TreeInfo.postfixPrec : TreeInfo.noPrec;
    } else {
        return TreeInfo.noPrec;
    }
}
Also used : JCCompilationUnit(com.sun.tools.javac.tree.JCTree.JCCompilationUnit) JCTypeCast(com.sun.tools.javac.tree.JCTree.JCTypeCast) JCAssign(com.sun.tools.javac.tree.JCTree.JCAssign) JCUnary(com.sun.tools.javac.tree.JCTree.JCUnary) JCFieldAccess(com.sun.tools.javac.tree.JCTree.JCFieldAccess) JCConditional(com.sun.tools.javac.tree.JCTree.JCConditional) JCTree(com.sun.tools.javac.tree.JCTree) JCBinary(com.sun.tools.javac.tree.JCTree.JCBinary) JCArrayAccess(com.sun.tools.javac.tree.JCTree.JCArrayAccess) JCAssignOp(com.sun.tools.javac.tree.JCTree.JCAssignOp) JCInstanceOf(com.sun.tools.javac.tree.JCTree.JCInstanceOf)

Example 20 with JCFieldAccess

use of com.sun.tools.javac.tree.JCTree.JCFieldAccess in project error-prone by google.

the class AbstractReturnValueIgnored method describe.

/**
   * Fixes the error by assigning the result of the call to the receiver reference, or deleting the
   * method call.
   */
public Description describe(MethodInvocationTree methodInvocationTree, VisitorState state) {
    // Find the root of the field access chain, i.e. a.intern().trim() ==> a.
    ExpressionTree identifierExpr = ASTHelpers.getRootAssignable(methodInvocationTree);
    String identifierStr = null;
    Type identifierType = null;
    if (identifierExpr != null) {
        identifierStr = identifierExpr.toString();
        if (identifierExpr instanceof JCIdent) {
            identifierType = ((JCIdent) identifierExpr).sym.type;
        } else if (identifierExpr instanceof JCFieldAccess) {
            identifierType = ((JCFieldAccess) identifierExpr).sym.type;
        } else {
            throw new IllegalStateException("Expected a JCIdent or a JCFieldAccess");
        }
    }
    Type returnType = ASTHelpers.getReturnType(((JCMethodInvocation) methodInvocationTree).getMethodSelect());
    Fix fix;
    if (identifierStr != null && !"this".equals(identifierStr) && returnType != null && state.getTypes().isAssignable(returnType, identifierType)) {
        // Fix by assigning the assigning the result of the call to the root receiver reference.
        fix = SuggestedFix.prefixWith(methodInvocationTree, identifierStr + " = ");
    } else {
        // Unclear what the programmer intended.  Delete since we don't know what else to do.
        Tree parent = state.getPath().getParentPath().getLeaf();
        fix = SuggestedFix.delete(parent);
    }
    return describeMatch(methodInvocationTree, fix);
}
Also used : Type(com.sun.tools.javac.code.Type) JCIdent(com.sun.tools.javac.tree.JCTree.JCIdent) Fix(com.google.errorprone.fixes.Fix) SuggestedFix(com.google.errorprone.fixes.SuggestedFix) JCFieldAccess(com.sun.tools.javac.tree.JCTree.JCFieldAccess) ExpressionTree(com.sun.source.tree.ExpressionTree) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) IdentifierTree(com.sun.source.tree.IdentifierTree) Tree(com.sun.source.tree.Tree) ExpressionTree(com.sun.source.tree.ExpressionTree) StatementTree(com.sun.source.tree.StatementTree)

Aggregations

JCFieldAccess (com.sun.tools.javac.tree.JCTree.JCFieldAccess)35 JCTree (com.sun.tools.javac.tree.JCTree)13 ExpressionTree (com.sun.source.tree.ExpressionTree)12 JCExpression (com.sun.tools.javac.tree.JCTree.JCExpression)11 JCIdent (com.sun.tools.javac.tree.JCTree.JCIdent)11 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)8 JCVariableDecl (com.sun.tools.javac.tree.JCTree.JCVariableDecl)7 Type (com.sun.tools.javac.code.Type)6 ListBuffer (com.sun.tools.javac.util.ListBuffer)6 JCClassDecl (com.sun.tools.javac.tree.JCTree.JCClassDecl)5 JCImport (com.sun.tools.javac.tree.JCTree.JCImport)5 JCMethodDecl (com.sun.tools.javac.tree.JCTree.JCMethodDecl)5 Name (com.sun.tools.javac.util.Name)5 SuggestedFix (com.google.errorprone.fixes.SuggestedFix)4 Symbol (com.sun.tools.javac.code.Symbol)4 JCPrimitiveTypeTree (com.sun.tools.javac.tree.JCTree.JCPrimitiveTypeTree)4 JCTypeApply (com.sun.tools.javac.tree.JCTree.JCTypeApply)4 JavacTreeMaker (lombok.javac.JavacTreeMaker)4 Fix (com.google.errorprone.fixes.Fix)3 ClassSymbol (com.sun.tools.javac.code.Symbol.ClassSymbol)3