com.sun.tools.javac.code.Type Java Examples

The following examples show how to use com.sun.tools.javac.code.Type. 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: JavacTrees.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Boolean visitType(Type t, Type s) {
    if (t == s)
        return true;

    if (s.isPartial())
        return visit(s, t);

    switch (t.getTag()) {
    case BYTE: case CHAR: case SHORT: case INT: case LONG: case FLOAT:
    case DOUBLE: case BOOLEAN: case VOID: case BOT: case NONE:
        return t.hasTag(s.getTag());
    default:
        throw new AssertionError("fuzzyMatcher " + t.getTag());
    }
}
 
Example #2
Source File: TypeHarness.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public final Type getType(String type) {
    JavaSource source = new JavaSource(type);
    MyAttr.theType = null;
    MyAttr.typeParameters = List.nil();
    tool.clear();
    List<JavaFileObject> inputs = of(source);
    try {
        tool.compile(inputs);
    } catch (Throwable ex) {
        throw new Abort(ex);
    }
    if (typeVariables != null) {
        return types.subst(MyAttr.theType, MyAttr.typeParameters, typeVariables);
    }
    return MyAttr.theType;
}
 
Example #3
Source File: DPrinter.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
protected void printObject(String label, Object item, Details details) {
    if (item == null) {
        printNull(label);
    } else if (item instanceof Attribute) {
        printAttribute(label, (Attribute) item);
    } else if (item instanceof Symbol) {
        printSymbol(label, (Symbol) item, details);
    } else if (item instanceof Type) {
        printType(label, (Type) item, details);
    } else if (item instanceof JCTree) {
        printTree(label, (JCTree) item);
    } else if (item instanceof List) {
        printList(label, (List) item);
    } else if (item instanceof Name) {
        printName(label, (Name) item);
    } else {
        printString(label, String.valueOf(item));
    }
}
 
Example #4
Source File: Annotate.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private Attribute getAnnotationArrayValue(Type expectedElementType, JCExpression tree, Env<AttrContext> env) {
    // Special case, implicit array
    if (!tree.hasTag(NEWARRAY)) {
        tree = make.at(tree.pos).
                NewArray(null, List.nil(), List.of(tree));
    }

    JCNewArray na = (JCNewArray)tree;
    if (na.elemtype != null) {
        log.error(na.elemtype.pos(), Errors.NewNotAllowedInAnnotation);
    }
    ListBuffer<Attribute> buf = new ListBuffer<>();
    for (List<JCExpression> l = na.elems; l.nonEmpty(); l=l.tail) {
        buf.append(attributeAnnotationValue(types.elemtype(expectedElementType),
                l.head,
                env));
    }
    na.type = expectedElementType;
    return new Attribute.
            Array(expectedElementType, buf.toArray(new Attribute[buf.length()]));
}
 
Example #5
Source File: TypeMaker.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Return the string representation of a type use.  Bounds of type
 * variables are not included; bounds of wildcard types are.
 * Class names are qualified if "full" is true.
 */
static String getTypeString(DocEnv env, Type t, boolean full) {
    // TODO: should annotations be included here?
    if (t.isAnnotated()) {
        t = t.unannotatedType();
    }
    switch (t.getTag()) {
    case ARRAY:
        StringBuilder s = new StringBuilder();
        while (t.hasTag(ARRAY)) {
            s.append("[]");
            t = env.types.elemtype(t);
        }
        s.insert(0, getTypeString(env, t, full));
        return s.toString();
    case CLASS:
        return ParameterizedTypeImpl.
                    parameterizedTypeToString(env, (ClassType)t, full);
    case WILDCARD:
        Type.WildcardType a = (Type.WildcardType)t;
        return WildcardTypeImpl.wildcardTypeToString(env, a, full);
    default:
        return t.tsym.getQualifiedName().toString();
    }
}
 
Example #6
Source File: Infer.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private List<Pair<Type, Type>> getParameterizedSupers(Type t, Type s) {
    Type lubResult = types.lub(t, s);
    if (lubResult == syms.errType || lubResult == syms.botType) {
        return List.nil();
    }
    List<Type> supertypesToCheck = lubResult.isIntersection() ?
            ((IntersectionClassType)lubResult).getComponents() :
            List.of(lubResult);
    ListBuffer<Pair<Type, Type>> commonSupertypes = new ListBuffer<>();
    for (Type sup : supertypesToCheck) {
        if (sup.isParameterized()) {
            Type asSuperOfT = asSuper(t, sup);
            Type asSuperOfS = asSuper(s, sup);
            commonSupertypes.add(new Pair<>(asSuperOfT, asSuperOfS));
        }
    }
    return commonSupertypes.toList();
}
 
Example #7
Source File: GenericTypeWellFormednessTest.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
void test() {
    for (int i = 0; i < rows.length ; i++) {
        for (int j = 0; j < columns.length ; j++) {
            Type decl = columns[j];
            Type inst = rows[i].inst(decl);
            if (isValidInstantiation[i][j] != Result.IGNORE) {
                executedCount++;
                assertValidGenericType(inst, isValidInstantiation[i][j].value);
            } else {
                ignoredCount++;
            }
        }
    }
}
 
Example #8
Source File: ExtensionTransformer.java    From manifold with Apache License 2.0 5 votes vote down vote up
/**
 * Erase all structural interface type literals to Object
 */
@Override
public void visitIdent( JCTree.JCIdent tree )
{
  super.visitIdent( tree );

  if( _tp.isGenerate() && !shouldProcessForGeneration() )
  {
    // Don't process tree during GENERATE, unless the tree was generated e.g., a bridge method
    return;
  }

  if( TypeUtil.isStructuralInterface( _tp, tree.sym ) && !isReceiver( tree ) )
  {
    Symbol.ClassSymbol objectSym = getObjectClass();
    Tree parent = _tp.getParent( tree );
    JCTree.JCIdent objIdent = _tp.getTreeMaker().Ident( objectSym );
    if( parent instanceof JCTree.JCVariableDecl )
    {
      ((JCTree.JCVariableDecl)parent).type = objectSym.type;

      long parameterModifier = 8589934592L; // Flag.Flag.PARAMETER.value
      if( (((JCTree.JCVariableDecl)parent).mods.flags & parameterModifier) != 0 )
      {
        objIdent.type = objectSym.type;
        ((JCTree.JCVariableDecl)parent).sym.type = objectSym.type;
        ((JCTree.JCVariableDecl)parent).vartype = objIdent;
      }
    }
    else if( parent instanceof JCTree.JCWildcard )
    {
      JCTree.JCWildcard wildcard = (JCTree.JCWildcard)parent;
      wildcard.type = new Type.WildcardType( objectSym.type, wildcard.kind.kind, wildcard.type.tsym );
    }
    tree = objIdent;
    tree.type = objectSym.type;
  }
  result = tree;
}
 
Example #9
Source File: ArgumentAttr.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Override
Type overloadCheck(ResultInfo resultInfo, DeferredAttrContext deferredAttrContext) {
    try {
        //compute target-type; this logic could be shared with Attr
        TargetInfo targetInfo = attr.getTargetInfo(speculativeTree, resultInfo, argtypes());
        Type lambdaType = targetInfo.descriptor;
        Type currentTarget = targetInfo.target;
        //check compatibility
        checkLambdaCompatible(lambdaType, resultInfo);
        return currentTarget;
    } catch (FunctionDescriptorLookupError ex) {
        resultInfo.checkContext.report(null, ex.getDiagnostic());
        return null; //cannot get here
    }
}
 
Example #10
Source File: LambdaToMethod.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/** does this functional expression require serialization support? */
boolean isSerializable() {
    for (Type target : tree.targets) {
        if (types.asSuper(target, syms.serializableType.tsym) != null) {
            return true;
        }
    }
    return false;
}
 
Example #11
Source File: LambdaToMethod.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
private JCExpression makeReceiver(VarSymbol rcvr) {
    if (rcvr == null) return null;
    JCExpression rcvrExpr = make.Ident(rcvr);
    Type rcvrType = tree.ownerAccessible ? tree.sym.enclClass().type : tree.expr.type;
    if (rcvrType == syms.arrayClass.type) {
        // Map the receiver type to the actually type, not just "array"
        rcvrType = tree.getQualifierExpression().type;
    }
    if (!rcvr.type.tsym.isSubClass(rcvrType.tsym, types)) {
        rcvrExpr = make.TypeCast(make.Type(rcvrType), rcvrExpr).setType(rcvrType);
    }
    return rcvrExpr;
}
 
Example #12
Source File: LambdaToMethod.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
private JCIdent makeThis(Type type, Symbol owner) {
    VarSymbol _this = new VarSymbol(PARAMETER | FINAL | SYNTHETIC,
            names._this,
            type,
            owner);
    return make.Ident(_this);
}
 
Example #13
Source File: TypeMaker.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Like the above version, but use and return the array given.
 */
public static com.sun.javadoc.Type[] getTypes(DocEnv env, List<Type> ts,
                                              com.sun.javadoc.Type res[]) {
    int i = 0;
    for (Type t : ts) {
        res[i++] = getType(env, t);
    }
    return res;
}
 
Example #14
Source File: T6889255.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
String getExpectedName(VarSymbol v, int i) {
    // special cases:
    // synthetic method
    if (((v.owner.owner.flags() & Flags.ENUM) != 0)
            && v.owner.name.toString().equals("valueOf"))
        return "name";
    // interfaces don't have saved names
    // -- no Code attribute for the LocalVariableTable attribute
    if ((v.owner.owner.flags() & Flags.INTERFACE) != 0)
        return "arg" + (i - 1);
    // abstract methods don't have saved names
    // -- no Code attribute for the LocalVariableTable attribute
    if ((v.owner.flags() & Flags.ABSTRACT) != 0)
        return "arg" + (i - 1);
    // bridge methods use argN. No LVT for them anymore
    if ((v.owner.flags() & Flags.BRIDGE) != 0)
        return "arg" + (i - 1);

    // The rest of this method assumes the local conventions in the test program
    Type t = v.type;
    String s;
    if (t.hasTag(TypeTag.CLASS))
        s = ((ClassType) t).tsym.name.toString();
    else
        s = t.toString();
    return String.valueOf(Character.toLowerCase(s.charAt(0))) + i;
}
 
Example #15
Source File: BoxingConversionTest.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
void testConversion(ConversionKind convKind, TestKind testKind) {
    Type[] rows = testKind.getFromTypes(this);
    Type[] cols = testKind.getToTypes(this);
    for (int i = 0; i < rows.length ; i++) {
        for (int j = 0; j < cols.length ; j++) {
            convKind.check(this, rows[i], cols[j], testKind.getResults(this)[i][j]);
        }
    }
}
 
Example #16
Source File: ArgumentAttr.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/** Get the type associated with given return expression. */
Type getReturnType(JCReturn ret) {
    if (ret.expr == null) {
        return syms.voidType;
    } else {
        return ret.expr.type;
    }
}
 
Example #17
Source File: TypeHarness.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/** assert that 's' is/is not convertible (method invocation conversion) to 't' */
public void assertConvertible(Type s, Type t, boolean expected) {
    if (types.isConvertible(s, t) != expected) {
        String msg = expected ?
            " is not convertible to " :
            " is convertible to ";
        error(s + msg + t);
    }
}
 
Example #18
Source File: ArgumentAttr.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Set the results of method attribution.
 */
void setResult(JCExpression tree, Type type) {
    result = type;
    if (env.info.isSpeculative) {
        //if we are in a speculative branch we can save the type in the tree itself
        //as there's no risk of polluting the original tree.
        tree.type = result;
    }
}
 
Example #19
Source File: TypeHarness.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/** compute the boxed type associated with 't' */
public Type box(Type t) {
    if (!t.isPrimitive()) {
        throw new AssertionError("Cannot box non-primitive type: " + t);
    }
    return types.boxedClass(t).type;
}
 
Example #20
Source File: HandleExtensionMethod.java    From EasyMPermission with MIT License 5 votes vote down vote up
public List<Extension> getExtensions(final JavacNode typeNode, final List<Object> extensionProviders) {
	List<Extension> extensions = new ArrayList<Extension>();
	for (Object extensionProvider : extensionProviders) {
		if (!(extensionProvider instanceof JCFieldAccess)) continue;
		JCFieldAccess provider = (JCFieldAccess) extensionProvider;
		if (!("class".equals(provider.name.toString()))) continue;
		Type providerType = CLASS.resolveMember(typeNode, provider.selected);
		if (providerType == null) continue;
		if ((providerType.tsym.flags() & (INTERFACE | ANNOTATION)) != 0) continue;
		
		extensions.add(getExtension(typeNode, (ClassType) providerType));
	}
	return extensions;
}
 
Example #21
Source File: RichDiagnosticFormatter.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
@Override
public String visitType(Type t, Locale locale) {
    String s = super.visitType(t, locale);
    if (t == syms.botType)
        s = localize(locale, "compiler.misc.type.null");
    return s;
}
 
Example #22
Source File: InferenceContext.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private List<Type> filterVars(Filter<UndetVar> fu) {
    ListBuffer<Type> res = new ListBuffer<>();
    for (Type t : undetvars) {
        UndetVar uv = (UndetVar)t;
        if (fu.accepts(uv)) {
            res.append(uv.qtype);
        }
    }
    return res.toList();
}
 
Example #23
Source File: LambdaToMethod.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private JCIdent makeThis(Type type, Symbol owner) {
    VarSymbol _this = new VarSymbol(PARAMETER | FINAL | SYNTHETIC,
            names._this,
            type,
            owner);
    return make.Ident(_this);
}
 
Example #24
Source File: ElementsService.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public boolean alreadyDefinedIn(CharSequence name, ExecutableType method, TypeElement enclClass) {
    Type.MethodType meth = ((Type)method).asMethodType();
    ClassSymbol clazz = (ClassSymbol)enclClass;
    Scope scope = clazz.members();
    Name n = names.fromString(name.toString());
    for (Symbol sym : scope.getSymbolsByName(n, Scope.LookupKind.NON_RECURSIVE)) {
        if(sym.type instanceof ExecutableType &&
                types.isSubsignature(meth, (ExecutableType)sym.type))
            return true;
    }
    return false;
}
 
Example #25
Source File: JavacTrees.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
boolean fuzzyMatch(List<Type> paramTypes, List<Type> methodParamTypes) {
    List<Type> l1 = paramTypes;
    List<Type> l2 = methodParamTypes;
    while (l1.nonEmpty()) {
        if (!fuzzyMatch(l1.head, l2.head))
            return false;
        l1 = l1.tail;
        l2 = l2.tail;
    }
    return true;
}
 
Example #26
Source File: InferenceContext.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
List<Type> instTypes() {
    ListBuffer<Type> buf = new ListBuffer<>();
    for (Type t : undetvars) {
        UndetVar uv = (UndetVar)t;
        buf.append(uv.getInst() != null ? uv.getInst() : uv.qtype);
    }
    return buf.toList();
}
 
Example #27
Source File: ClassReader.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/** Convert (implicit) signature to list of types
 *  until `terminator' is encountered.
 */
List<Type> sigToTypes(char terminator) {
    List<Type> head = List.of(null);
    List<Type> tail = head;
    while (signature[sigp] != terminator)
        tail = tail.setTail(List.of(sigToType()));
    sigp++;
    return head.tail;
}
 
Example #28
Source File: ClassDocImpl.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Return the superclass of this class.  Return null if this is an
 * interface.  A superclass is represented by either a
 * <code>ClassDoc</code> or a <code>ParameterizedType</code>.
 */
public com.sun.javadoc.Type superclassType() {
    if (isInterface() || isAnnotationType() ||
            (tsym == env.syms.objectType.tsym))
        return null;
    Type sup = env.types.supertype(type);
    return TypeMaker.getType(env,
                             (sup.hasTag(TypeTag.NONE)) ? env.syms.objectType : sup);
}
 
Example #29
Source File: Lower.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void visitMethodDef(JCMethodDecl tree) {
    if (tree.name == names.init && (currentClass.flags_field&ENUM) != 0) {
        // Add "String $enum$name, int $enum$ordinal" to the beginning of the
        // argument list for each constructor of an enum.
        JCVariableDecl nameParam = make_at(tree.pos()).
            Param(names.fromString(target.syntheticNameChar() +
                                   "enum" + target.syntheticNameChar() + "name"),
                  syms.stringType, tree.sym);
        nameParam.mods.flags |= SYNTHETIC; nameParam.sym.flags_field |= SYNTHETIC;
        JCVariableDecl ordParam = make.
            Param(names.fromString(target.syntheticNameChar() +
                                   "enum" + target.syntheticNameChar() +
                                   "ordinal"),
                  syms.intType, tree.sym);
        ordParam.mods.flags |= SYNTHETIC; ordParam.sym.flags_field |= SYNTHETIC;

        MethodSymbol m = tree.sym;
        tree.params = tree.params.prepend(ordParam).prepend(nameParam);

        m.extraParams = m.extraParams.prepend(ordParam.sym);
        m.extraParams = m.extraParams.prepend(nameParam.sym);
        Type olderasure = m.erasure(types);
        m.erasure_field = new MethodType(
            olderasure.getParameterTypes().prepend(syms.intType).prepend(syms.stringType),
            olderasure.getReturnType(),
            olderasure.getThrownTypes(),
            syms.methodClass);
    }

    JCMethodDecl prevMethodDef = currentMethodDef;
    MethodSymbol prevMethodSym = currentMethodSym;
    try {
        currentMethodDef = tree;
        currentMethodSym = tree.sym;
        visitMethodDefInternal(tree);
    } finally {
        currentMethodDef = prevMethodDef;
        currentMethodSym = prevMethodSym;
    }
}
 
Example #30
Source File: TypeHarness.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/** assert that 's' is/is not the same type as 't' */
public void assertSameType(Type s, Type t, boolean expected) {
    if (types.isSameType(s, t) != expected) {
        String msg = expected ?
            " is not the same type as " :
            " is the same type as ";
        error(s + msg + t);
    }
}