private String getClassLevelUrlPath(TypeElement cls) { RestApiMountPoint mountPoint = cls.getAnnotation(RestApiMountPoint.class); String path = mountPoint == null ? "/" : mountPoint.value(); RequestMapping clsAnno = cls.getAnnotation(RequestMapping.class); if (clsAnno == null || clsAnno.value().length == 0) { return path; } else if (clsAnno.value().length == 1) { return Utils.joinPaths(path, clsAnno.value()[0]); } else { throw new IllegalStateException( String.format( "The RequestMapping annotation of class %s has multiple value strings. Only zero or one value is supported", cls.getQualifiedName())); } }
public TypeVisitorImpl( DeclaredType type, List<? extends TypeMirror> typeArguments, Collection<DeclaredType> typeRecursionGuard) { TypeElement elem = (TypeElement) type.asElement(); _typeRecursionGuard = typeRecursionGuard; _type = type; List<? extends TypeParameterElement> generics = elem.getTypeParameters(); for (int i = 0; i < generics.size(); i++) { DeclaredType value = (typeArguments.isEmpty() || !(typeArguments.get(i) instanceof DeclaredType)) ? null : (DeclaredType) typeArguments.get(i); _typeArguments.put(generics.get(i).getSimpleName(), value); } }
@Override public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { for (TypeElement te : ElementFilter.typesIn(roundEnv.getRootElements())) { if (isSimpleName(te, "InvalidSource")) { for (Element c : te.getEnclosedElements()) { for (AnnotationMirror am : c.getAnnotationMirrors()) { Element ate = am.getAnnotationType().asElement(); if (isSimpleName(ate, "ExpectInterfaces")) { checkInterfaces((TypeElement) c, getValue(am)); } else if (isSimpleName(ate, "ExpectSupertype")) { checkSupertype((TypeElement) c, getValue(am)); } } } } } return true; }
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); }
private JsonType buildType(DeclaredType declaredType, TypeElement element) { if (_typeRecursionGuard.contains(declaredType)) { return new JsonRecursiveObject(element.getSimpleName().toString()); } JsonObject json = new JsonObject(); buildTypeContents(json, element); return json; // we've already added to the cache; short-circuit to handle recursion }
@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); } } }
private void buildTypeContents(JsonObject o, TypeElement element) { if ("org.springframework.web.servlet.ModelAndView" .equals(element.getQualifiedName().toString())) { return; } if (element.getSuperclass().getKind() != TypeKind.NONE) { // an interface's superclass is TypeKind.NONE DeclaredType sup = (DeclaredType) element.getSuperclass(); if (!isJsonPrimitive(sup)) { buildTypeContents(o, (TypeElement) sup.asElement()); } } for (Element e : element.getEnclosedElements()) { if (e instanceof ExecutableElement) { addFieldFromBeanMethod(o, (ExecutableElement) e); } } }
private RequestMethod getRequestMethod( ExecutableElement executableElement, TypeElement cls, RequestMapping anno) { if (anno.method().length != 1) { throw new IllegalStateException( String.format( "The RequestMapping annotation for %s.%s is not parseable. Exactly one request method (GET/POST/etc) is required.", cls.getQualifiedName(), executableElement.getSimpleName())); } else { return anno.method()[0]; } }
private void addFieldFromBeanMethod(JsonObject o, ExecutableElement executableElement) { if (!isJsonBeanGetter(executableElement)) { return; } TypeMirror type = executableElement.getReturnType(); String methodName = executableElement.getSimpleName().toString(); int trimLength = methodName.startsWith("is") ? 2 : 3; String beanName = methodName.substring(trimLength + 1, methodName.length()); beanName = methodName.substring(trimLength, trimLength + 1).toLowerCase() + beanName; // loop over the element's generic types, and build a concrete list from the owning context List<DeclaredType> concreteTypes = new ArrayList(); // replace variables with the current concrete manifestation if (type instanceof TypeVariable) { type = getDeclaredTypeForTypeVariable((TypeVariable) type); if (type == null) { return; // couldn't find a replacement -- must be a generics-capable type with no generics // info } } String docComment = processingEnv.getElementUtils().getDocComment(executableElement); if (type instanceof DeclaredType) { TypeElement element = (TypeElement) ((DeclaredType) type).asElement(); for (TypeParameterElement generic : element.getTypeParameters()) { concreteTypes.add(_typeArguments.get(generic.getSimpleName())); } Collection<DeclaredType> types = new HashSet<DeclaredType>(_typeRecursionGuard); types.add(_type); o.addField(beanName, newJsonType((DeclaredType) type, concreteTypes, types)) .setCommentText(docComment); } else { o.addField(beanName, newJsonType(type)).setCommentText(docComment); } }
private void checkSupertype(TypeElement te, String expect) { System.err.println("check supertype: " + te + " -- " + expect); String found = asString(te.getSuperclass()); checkEqual("supertype", te, found, expect); }
private void checkInterfaces(TypeElement te, String expect) { System.err.println("check interfaces: " + te + " -- " + expect); String found = asString(te.getInterfaces(), ", "); checkEqual("interfaces", te, found, expect); }