Ejemplo n.º 1
0
 public WildcardType getWildcardType(TypeMirror extendsBound, TypeMirror superBound) {
   BoundKind bkind;
   Type bound;
   if (extendsBound == null && superBound == null) {
     bkind = BoundKind.UNBOUND;
     bound = syms.objectType;
   } else if (superBound == null) {
     bkind = BoundKind.EXTENDS;
     bound = (Type) extendsBound;
   } else if (extendsBound == null) {
     bkind = BoundKind.SUPER;
     bound = (Type) superBound;
   } else {
     throw new IllegalArgumentException("Extends and super bounds cannot both be provided");
   }
   switch (bound.getKind()) {
     case ARRAY:
     case DECLARED:
     case ERROR:
     case TYPEVAR:
       return new Type.WildcardType(bound, bkind, syms.boundClass);
     default:
       throw new IllegalArgumentException(bound.toString());
   }
 }
Ejemplo n.º 2
0
 public PrimitiveType unboxedType(TypeMirror t) {
   if (t.getKind() != TypeKind.DECLARED) throw new IllegalArgumentException(t.toString());
   Type unboxed = types.unboxedType((Type) t);
   if (!unboxed.isPrimitive()) // only true primitives, not void
   throw new IllegalArgumentException(t.toString());
   return unboxed;
 }
Ejemplo n.º 3
0
 public Element asElement(TypeMirror t) {
   switch (t.getKind()) {
     case DECLARED:
     case ERROR:
     case TYPEVAR:
       Type type = cast(Type.class, t);
       return type.asElement();
     default:
       return null;
   }
 }
Ejemplo n.º 4
0
 public Element asElement(TypeMirror t) {
   Type type = cast(Type.class, t);
   switch (type.tag) {
     case TypeTags.CLASS:
     case TypeTags.ERROR:
     case TypeTags.TYPEVAR:
       return type.asElement();
     default:
       return null;
   }
 }
Ejemplo n.º 5
0
  public DeclaredType getDeclaredType(
      DeclaredType enclosing, TypeElement typeElem, TypeMirror... typeArgs) {
    if (enclosing == null) return getDeclaredType(typeElem, typeArgs);

    ClassSymbol sym = (ClassSymbol) typeElem;
    Type outer = (Type) enclosing;

    if (outer.tsym != sym.owner.enclClass())
      throw new IllegalArgumentException(enclosing.toString());
    if (!outer.isParameterized()) return getDeclaredType(typeElem, typeArgs);

    return getDeclaredType0(outer, sym, typeArgs);
  }
Ejemplo n.º 6
0
  /** {@inheritDoc} Overridden here to handle @Inherited. */
  public <A extends Annotation> A getAnnotation(Class<A> annoType) {

    boolean inherited = annoType.isAnnotationPresent(Inherited.class);
    for (Type t = sym.type;
        t.tsym != env.symtab.objectType.tsym && !t.isErroneous();
        t = env.jctypes.supertype(t)) {

      A result = getAnnotation(annoType, t.tsym);
      if (result != null || !inherited) {
        return result;
      }
    }
    return null;
  }
Ejemplo n.º 7
0
 /**
  * Get a localized string represenation for a given type.
  *
  * @param t type to be displayed
  * @param locale the locale in which the string is to be rendered
  * @return localized string representation
  */
 public String visit(Type t, Locale locale) {
   return t.accept(this, locale);
 }
 Attribute enterAttributeValue(Type expected, JCExpression tree, Env<AttrContext> env) {
   // first, try completing the attribution value sym - if a completion
   // error is thrown, we should recover gracefully, and display an
   // ordinary resolution diagnostic.
   try {
     expected.tsym.complete();
   } catch (CompletionFailure e) {
     log.error(tree.pos(), "cant.resolve", Kinds.kindName(e.sym), e.sym);
     return new Attribute.Error(expected);
   }
   if (expected.isPrimitive() || types.isSameType(expected, syms.stringType)) {
     Type result = attr.attribExpr(tree, env, expected);
     if (result.isErroneous()) return new Attribute.Error(expected);
     if (result.constValue() == null) {
       log.error(tree.pos(), "attribute.value.must.be.constant");
       return new Attribute.Error(expected);
     }
     result = cfolder.coerce(result, expected);
     return new Attribute.Constant(expected, result.constValue());
   }
   if (expected.tsym == syms.classType.tsym) {
     Type result = attr.attribExpr(tree, env, expected);
     if (result.isErroneous()) return new Attribute.Error(expected);
     if (TreeInfo.name(tree) != names._class) {
       log.error(tree.pos(), "annotation.value.must.be.class.literal");
       return new Attribute.Error(expected);
     }
     return new Attribute.Class(types, (((JCFieldAccess) tree).selected).type);
   }
   if ((expected.tsym.flags() & Flags.ANNOTATION) != 0
       || types.isSameType(expected, syms.annotationType)) {
     if (tree.getTag() != JCTree.ANNOTATION) {
       log.error(tree.pos(), "annotation.value.must.be.annotation");
       expected = syms.errorType;
     }
     return enterAnnotation((JCAnnotation) tree, expected, env);
   }
   if (expected.tag == TypeTags.ARRAY) { // should really be isArray()
     if (tree.getTag() != JCTree.NEWARRAY) {
       tree = make.at(tree.pos).NewArray(null, List.<JCExpression>nil(), List.of(tree));
     }
     JCNewArray na = (JCNewArray) tree;
     if (na.elemtype != null) {
       log.error(na.elemtype.pos(), "new.not.allowed.in.annotation");
       return new Attribute.Error(expected);
     }
     ListBuffer<Attribute> buf = new ListBuffer<Attribute>();
     for (List<JCExpression> l = na.elems; l.nonEmpty(); l = l.tail) {
       buf.append(enterAttributeValue(types.elemtype(expected), l.head, env));
     }
     na.type = expected;
     return new Attribute.Array(expected, buf.toArray(new Attribute[buf.length()]));
   }
   if (expected.tag == TypeTags.CLASS && (expected.tsym.flags() & Flags.ENUM) != 0) {
     attr.attribExpr(tree, env, expected);
     Symbol sym = TreeInfo.symbol(tree);
     if (sym == null
         || TreeInfo.nonstaticSelect(tree)
         || sym.kind != Kinds.VAR
         || (sym.flags() & Flags.ENUM) == 0) {
       log.error(tree.pos(), "enum.annotation.must.be.enum.constant");
       return new Attribute.Error(expected);
     }
     VarSymbol enumerator = (VarSymbol) sym;
     return new Attribute.Enum(expected, enumerator);
   }
   if (!expected.isErroneous()) log.error(tree.pos(), "annotation.value.not.allowable.type");
   return new Attribute.Error(attr.attribExpr(tree, env, expected));
 }