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

The following examples show how to use com.sun.tools.javac.code.Type. These examples are extracted from open source projects. 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
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 2
Source Project: openjdk-jdk9   Source File: JavacTrees.java    License: 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 3
/**
 * 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 4
Source Project: openjdk-jdk9   Source File: TypeHarness.java    License: 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 5
Source Project: TencentKona-8   Source File: DPrinter.java    License: 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 6
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 7
/** Is exc an exception type that need not be declared?
 */
boolean isUnchecked(Type exc) {
    return
        (exc.hasTag(TYPEVAR)) ? isUnchecked(types.supertype(exc)) :
        (exc.hasTag(CLASS)) ? isUnchecked((ClassSymbol)exc.tsym) :
        exc.hasTag(BOT);
}
 
Example 8
List<Type> solveBasic(List<Type> varsToSolve, EnumSet<InferenceStep> steps) {
    ListBuffer<Type> solvedVars = new ListBuffer<>();
    for (Type t : varsToSolve.intersect(restvars())) {
        UndetVar uv = (UndetVar)asUndetVar(t);
        for (InferenceStep step : steps) {
            if (step.accepts(uv, this)) {
                uv.setInst(step.solve(uv, this));
                solvedVars.add(uv.qtype);
                break;
            }
        }
    }
    return solvedVars.toList();
}
 
Example 9
Source Project: openjdk-8   Source File: LambdaToMethod.java    License: 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 10
/**
 * 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 11
Source Project: manifold   Source File: ExtensionTransformer.java    License: 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 12
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 13
/** The name of a this$n field
 *  @param type   The class referenced by the this$n field
 */
Name outerThisName(Type type, Symbol owner) {
    Type t = type.getEnclosingType();
    int nestingLevel = 0;
    while (t.hasTag(CLASS)) {
        t = t.getEnclosingType();
        nestingLevel++;
    }
    Name result = names.fromString("this" + target.syntheticNameChar() + nestingLevel);
    while (owner.kind == TYP && ((ClassSymbol)owner).members().findFirst(result) != null)
        result = names.fromString(result.toString() + target.syntheticNameChar());
    return result;
}
 
Example 14
@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 15
Source Project: Refaster   Source File: UForAll.java    License: Apache License 2.0 5 votes vote down vote up
@Override
@Nullable
public Unifier visitForAll(ForAll target, @Nullable Unifier unifier) {
  Types types = unifier.types();
  try {
    Type myType = inline(new Inliner(unifier.getContext(), Bindings.create()));
    if (types.overrideEquivalent(types.erasure(myType), types.erasure(target))) {
      return unifier;
    }
  } catch (CouldNotResolveImportException e) {
    // fall through
  }
  return null;
}
 
Example 16
public void visitSwitch(JCSwitch tree) {
    Type selsuper = types.supertype(tree.selector.type);
    boolean enumSwitch = selsuper != null &&
        selsuper.tsym == syms.enumSym;
    Type target = enumSwitch ? erasure(tree.selector.type) : syms.intType;
    tree.selector = translate(tree.selector, target);
    tree.cases = translateCases(tree.cases);
    result = tree;
}
 
Example 17
Source Project: hottub   Source File: LambdaToMethod.java    License: 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 18
Source Project: hottub   Source File: LambdaToMethod.java    License: 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 19
Source Project: openjdk-8   Source File: T6889255.java    License: 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 20
Source Project: TencentKona-8   Source File: TypeHarness.java    License: 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 21
/**
 * 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 22
/** 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 23
Source Project: EasyMPermission   Source File: HandleExtensionMethod.java    License: 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 24
@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 25
private JCIdent makeThis(Type type, Symbol owner) {
    VarSymbol _this = new VarSymbol(PARAMETER | FINAL | SYNTHETIC,
            names._this,
            type,
            owner);
    return make.Ident(_this);
}
 
Example 26
Source Project: netbeans   Source File: ElementsService.java    License: 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 27
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 28
/** 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 29
Source Project: openjdk-jdk9   Source File: ClassDocImpl.java    License: 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 30
Source Project: openjdk-jdk9   Source File: ArgumentAttr.java    License: 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;
    }
}