Search in sources :

Example 1 with Pair

use of com.sun.tools.javac.util.Pair in project bazel by bazelbuild.

the class TypeAnnotationUtils method createCompoundFromAnnotationMirror.

/**
     * Returns a newly created Attribute.Compound corresponding to an
     * argument AnnotationMirror.
     *
     * @param am  an AnnotationMirror, which may be part of an AST or an internally
     *            created subclass.
     * @return  a new Attribute.Compound corresponding to the AnnotationMirror
     */
public static Attribute.Compound createCompoundFromAnnotationMirror(ProcessingEnvironment env, AnnotationMirror am) {
    // Create a new Attribute to match the AnnotationMirror.
    List<Pair<Symbol.MethodSymbol, Attribute>> values = List.nil();
    for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry : am.getElementValues().entrySet()) {
        Attribute attribute = attributeFromAnnotationValue(env, entry.getKey(), entry.getValue());
        values = values.append(new Pair<>((Symbol.MethodSymbol) entry.getKey(), attribute));
    }
    return new Attribute.Compound((Type.ClassType) am.getAnnotationType(), values);
}
Also used : ArrayType(javax.lang.model.type.ArrayType) TargetType(com.sun.tools.javac.code.TargetType) Type(com.sun.tools.javac.code.Type) Attribute(com.sun.tools.javac.code.Attribute) Symbol(com.sun.tools.javac.code.Symbol) TypeCompound(com.sun.tools.javac.code.Attribute.TypeCompound) Map(java.util.Map) Pair(com.sun.tools.javac.util.Pair)

Example 2 with Pair

use of com.sun.tools.javac.util.Pair in project ceylon-compiler by ceylon.

the class Attr method getSyntheticScopeMapping.

/** Creates a synthetic scope containing fake generic constructors.
     *  Assuming that the original scope contains a constructor of the kind:
     *  Foo(X x, Y y), where X,Y are class type-variables declared in Foo,
     *  the synthetic scope is added a generic constructor of the kind:
     *  <X,Y>Foo<X,Y>(X x, Y y). This is crucial in order to enable diamond
     *  inference. The inferred return type of the synthetic constructor IS
     *  the inferred type for the diamond operator.
     */
private Pair<Scope, Scope> getSyntheticScopeMapping(Type ctype) {
    if (ctype.tag != CLASS) {
        return erroneousMapping;
    }
    Pair<Scope, Scope> mapping = new Pair<Scope, Scope>(ctype.tsym.members(), new Scope(ctype.tsym));
    //declared, and insert it into the new scope.
    for (Scope.Entry e = mapping.fst.lookup(names.init); e.scope != null; e = e.next()) {
        Type synthRestype = new ClassType(ctype.getEnclosingType(), ctype.tsym.type.getTypeArguments(), ctype.tsym);
        MethodSymbol synhConstr = new MethodSymbol(e.sym.flags(), names.init, types.createMethodTypeWithReturn(e.sym.type, synthRestype), e.sym.owner);
        mapping.snd.enter(synhConstr);
    }
    return mapping;
}
Also used : ClassType(com.sun.tools.javac.code.Type.ClassType) MethodType(com.sun.tools.javac.code.Type.MethodType) WildcardType(com.sun.tools.javac.code.Type.WildcardType) Type(com.sun.tools.javac.code.Type) ArrayType(com.sun.tools.javac.code.Type.ArrayType) UnionClassType(com.sun.tools.javac.code.Type.UnionClassType) Scope(com.sun.tools.javac.code.Scope) DelegatedScope(com.sun.tools.javac.code.Scope.DelegatedScope) DynamicMethodSymbol(com.sun.tools.javac.code.Symbol.DynamicMethodSymbol) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) ClassType(com.sun.tools.javac.code.Type.ClassType) UnionClassType(com.sun.tools.javac.code.Type.UnionClassType) Pair(com.sun.tools.javac.util.Pair)

Example 3 with Pair

use of com.sun.tools.javac.util.Pair in project ceylon-compiler by ceylon.

the class CheckAttributedTree method read.

/**
     * Read a file.
     * @param file the file to be read
     * @return the tree for the content of the file
     * @throws IOException if any IO errors occur
     * @throws TreePosTest.ParseException if any errors occur while parsing the file
     */
List<Pair<JCCompilationUnit, JCTree>> read(File file) throws IOException, AttributionException {
    JavacTool tool = JavacTool.create();
    r.errors = 0;
    Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(file);
    String[] opts = { "-XDshouldStopPolicy=ATTR", "-XDverboseCompilePolicy" };
    JavacTask task = tool.getTask(pw, fm, r, Arrays.asList(opts), null, files);
    final List<Element> analyzedElems = new ArrayList<>();
    task.setTaskListener(new TaskListener() {

        public void started(TaskEvent e) {
            if (e.getKind() == TaskEvent.Kind.ANALYZE)
                analyzedElems.add(e.getTypeElement());
        }

        public void finished(TaskEvent e) {
        }
    });
    try {
        Iterable<? extends CompilationUnitTree> trees = task.parse();
        task.analyze();
        List<Pair<JCCompilationUnit, JCTree>> res = new ArrayList<>();
        //System.out.println("Try to add pairs. Elems are " + analyzedElems);
        for (CompilationUnitTree t : trees) {
            JCCompilationUnit cu = (JCCompilationUnit) t;
            for (JCTree def : cu.defs) {
                if (def.getTag() == JCTree.CLASSDEF && analyzedElems.contains(((JCTree.JCClassDecl) def).sym)) {
                    //System.out.println("Adding pair...");
                    res.add(new Pair<>(cu, def));
                }
            }
        }
        return res;
    } catch (Throwable t) {
        throw new AttributionException("Exception while attributing file: " + file);
    }
}
Also used : JCCompilationUnit(com.sun.tools.javac.tree.JCTree.JCCompilationUnit) CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) JCClassDecl(com.sun.tools.javac.tree.JCTree.JCClassDecl) JavacTool(com.sun.tools.javac.api.JavacTool) Element(javax.lang.model.element.Element) ArrayList(java.util.ArrayList) JCTree(com.sun.tools.javac.tree.JCTree) TaskListener(com.sun.source.util.TaskListener) TaskEvent(com.sun.source.util.TaskEvent) JavacTask(com.sun.source.util.JavacTask) Pair(com.sun.tools.javac.util.Pair)

Example 4 with Pair

use of com.sun.tools.javac.util.Pair in project bazel by bazelbuild.

the class TypeAnnotationUtils method createTypeCompoundFromAnnotationMirror.

/**
     * Returns a newly created Attribute.TypeCompound corresponding to an
     * argument AnnotationMirror.
     *
     * @param am  an AnnotationMirror, which may be part of an AST or an internally
     *            created subclass.
     * @param tapos  the type annotation position to use.
     * @return  a new Attribute.TypeCompound corresponding to the AnnotationMirror
     */
public static Attribute.TypeCompound createTypeCompoundFromAnnotationMirror(ProcessingEnvironment env, AnnotationMirror am, TypeAnnotationPosition tapos) {
    // Create a new Attribute to match the AnnotationMirror.
    List<Pair<Symbol.MethodSymbol, Attribute>> values = List.nil();
    for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry : am.getElementValues().entrySet()) {
        Attribute attribute = attributeFromAnnotationValue(env, entry.getKey(), entry.getValue());
        values = values.append(new Pair<>((Symbol.MethodSymbol) entry.getKey(), attribute));
    }
    return new Attribute.TypeCompound((Type.ClassType) am.getAnnotationType(), values, tapos);
}
Also used : TypeCompound(com.sun.tools.javac.code.Attribute.TypeCompound) ArrayType(javax.lang.model.type.ArrayType) TargetType(com.sun.tools.javac.code.TargetType) Type(com.sun.tools.javac.code.Type) Attribute(com.sun.tools.javac.code.Attribute) Symbol(com.sun.tools.javac.code.Symbol) Map(java.util.Map) Pair(com.sun.tools.javac.util.Pair)

Aggregations

Pair (com.sun.tools.javac.util.Pair)4 Type (com.sun.tools.javac.code.Type)3 Attribute (com.sun.tools.javac.code.Attribute)2 TypeCompound (com.sun.tools.javac.code.Attribute.TypeCompound)2 Symbol (com.sun.tools.javac.code.Symbol)2 TargetType (com.sun.tools.javac.code.TargetType)2 Map (java.util.Map)2 ArrayType (javax.lang.model.type.ArrayType)2 CompilationUnitTree (com.sun.source.tree.CompilationUnitTree)1 JavacTask (com.sun.source.util.JavacTask)1 TaskEvent (com.sun.source.util.TaskEvent)1 TaskListener (com.sun.source.util.TaskListener)1 JavacTool (com.sun.tools.javac.api.JavacTool)1 Scope (com.sun.tools.javac.code.Scope)1 DelegatedScope (com.sun.tools.javac.code.Scope.DelegatedScope)1 DynamicMethodSymbol (com.sun.tools.javac.code.Symbol.DynamicMethodSymbol)1 MethodSymbol (com.sun.tools.javac.code.Symbol.MethodSymbol)1 ArrayType (com.sun.tools.javac.code.Type.ArrayType)1 ClassType (com.sun.tools.javac.code.Type.ClassType)1 MethodType (com.sun.tools.javac.code.Type.MethodType)1