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; }
private void scanForMultipart( ExecutableElement executableElement, RestDocumentation.Resource.Method doc) { for (VariableElement var : executableElement.getParameters()) { TypeMirror varType = var.asType(); if (varType.toString().startsWith(MultipartHttpServletRequest.class.getName())) { doc.setMultipartRequest(true); return; } } }
public ArrayType getArrayType(TypeMirror componentType) { switch (componentType.getKind()) { case VOID: case EXECUTABLE: case WILDCARD: // heh! case PACKAGE: throw new IllegalArgumentException(componentType.toString()); } return new Type.ArrayType((Type) componentType, syms.arrayClass); }
private boolean doesClassContainNoArgsConstructor(Element el) { for (Element subelement : el.getEnclosedElements()) { if (subelement.getKind() == ElementKind.CONSTRUCTOR && subelement.getModifiers().contains(Modifier.PUBLIC)) { TypeMirror mirror = subelement.asType(); if (mirror.accept(noArgsVisitor, null)) return true; } } return false; }
// where private DeclaredType getDeclaredType0(Type outer, ClassSymbol sym, TypeMirror... typeArgs) { if (typeArgs.length != sym.type.getTypeArguments().length()) throw new IllegalArgumentException("Incorrect number of type arguments"); ListBuffer<Type> targs = new ListBuffer<Type>(); for (TypeMirror t : typeArgs) { if (!(t instanceof ReferenceType || t instanceof WildcardType)) throw new IllegalArgumentException(t.toString()); targs.append((Type) t); } // TODO: Would like a way to check that type args match formals. return (DeclaredType) new Type.ClassType(outer, targs.toList(), sym); }
private JsonType newJsonType(TypeMirror typeMirror) { if (isJsonPrimitive(typeMirror)) { return new JsonPrimitive(typeMirror.toString()); } else if (typeMirror.getKind() == TypeKind.DECLARED) { // some sort of object... walk it DeclaredType type = (DeclaredType) typeMirror; return newJsonType(type, type.getTypeArguments(), new HashSet<DeclaredType>()); } else if (typeMirror.getKind() == TypeKind.VOID) { return null; } else if (typeMirror.getKind() == TypeKind.ARRAY) { return newJsonType(((ArrayType) typeMirror).getComponentType()); } else { throw new UnsupportedOperationException(typeMirror.toString()); } }
private String asString(TypeMirror t) { if (t == null) return "[typ:null]"; return t.accept( new SimpleTypeVisitor<String, Void>() { @Override public String defaultAction(TypeMirror t, Void ignore) { return "[typ:" + t.toString() + "]"; } @Override public String visitDeclared(DeclaredType t, Void ignore) { checkEqual(t.asElement(), types.asElement(t)); String s = asString(t.asElement()); List<? extends TypeMirror> args = t.getTypeArguments(); if (!args.isEmpty()) s += "<" + asString(args, ",") + ">"; return s; } @Override public String visitTypeVariable(TypeVariable t, Void ignore) { return "tvar " + t; } @Override public String visitError(ErrorType t, Void ignore) { return "!:" + visitDeclared(t, ignore); } }, null); }
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; } }
private boolean isInstanceOf(TypeMirror typeMirror, Class type) { if (!(typeMirror instanceof DeclaredType)) { return false; } if (typeMirror.toString().startsWith(type.getName())) { return true; } DeclaredType declaredType = (DeclaredType) typeMirror; TypeElement typeElement = (TypeElement) declaredType.asElement(); for (TypeMirror iface : typeElement.getInterfaces()) { if (isInstanceOf(iface, type)) { return true; } } return isInstanceOf(typeElement.getSuperclass(), type); }
@Override public JsonType visitDeclared(DeclaredType declaredType, Void o) { if (isJsonPrimitive(declaredType)) { // 'primitive'-ish things return new JsonPrimitive(declaredType.toString()); } else if (isInstanceOf(declaredType, Collection.class)) { if (declaredType.getTypeArguments().size() == 0) { return new JsonArray(new JsonPrimitive(Object.class.getName())); } else { TypeMirror elem = declaredType.getTypeArguments().get(0); return new JsonArray(elem.accept(this, o)); } } else if (isInstanceOf(declaredType, Map.class)) { if (declaredType.getTypeArguments().size() == 0) { return new JsonDict( new JsonPrimitive(Object.class.getName()), new JsonPrimitive(Object.class.getName())); } else { TypeMirror key = declaredType.getTypeArguments().get(0); TypeMirror val = declaredType.getTypeArguments().get(1); return new JsonDict(key.accept(this, o), val.accept(this, o)); } } else { TypeElement element = (TypeElement) declaredType.asElement(); if (element.getKind() == ElementKind.ENUM) { List<String> enumConstants = new ArrayList(); for (Element e : element.getEnclosedElements()) { if (e.getKind() == ElementKind.ENUM_CONSTANT) { enumConstants.add(e.toString()); } } JsonPrimitive primitive = new JsonPrimitive(String.class.getName()); // TODO is this always a string? primitive.setRestrictions(enumConstants); return primitive; } else { return buildType(declaredType, element); } } }
@Override public JsonType visitUnknown(TypeMirror typeMirror, Void o) { throw new UnsupportedOperationException(typeMirror.toString()); }
private boolean isJsonPrimitive(TypeMirror typeMirror) { return (typeMirror.getKind().isPrimitive() || JsonPrimitive.isPrimitive(typeMirror.toString())); }
/** Throws an IllegalArgumentException if a type's kind is one of a set. */ private void validateTypeNotIn(TypeMirror t, Set<TypeKind> invalidKinds) { if (invalidKinds.contains(t.getKind())) throw new IllegalArgumentException(t.toString()); }
public TypeMirror erasure(TypeMirror t) { if (t.getKind() == TypeKind.PACKAGE) throw new IllegalArgumentException(t.toString()); return types.erasure((Type) t); }