private void parseMethod(Api api, ExecutableElement executableElement) throws IOException { Element actionElement = generator .getProcessingEnvironment() .getElementUtils() .getTypeElement(ApiMethodDoc.class.getName()); TypeMirror apiMethodDocType = actionElement.asType(); for (AnnotationMirror am : executableElement.getAnnotationMirrors()) { if (am.getAnnotationType().equals(apiMethodDocType)) { api.apiMethodDoc = Maps.newHashMap(); for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> ee : am.getElementValues().entrySet()) { api.apiMethodDoc.put(ee.getKey().getSimpleName().toString(), ee.getValue()); } break; } } // generator.log("apiMethodDoc: " + api.apiMethodDoc); if (null == api.apiMethodDoc) { generator.log("Method miss @ApiMethodDoc. " + executableElement); return; } api.methodName = executableElement.getSimpleName().toString(); api.methodMapping = executableElement.getAnnotation(RequestMapping.class); api.parameters = Lists.newArrayList(); for (VariableElement var : executableElement.getParameters()) { api.parameters.add(var); } }
protected boolean isExplicitTypeDefinition(Element declaration) { if (declaration.getKind() != ElementKind.CLASS) { debug("%s isn't a potential JAXB type because it's not a class.", declaration); return false; } PackageElement pckg = this.context.getProcessingEnvironment().getElementUtils().getPackageOf(declaration); if ((pckg != null) && (pckg.getAnnotation(Ignore.class) != null)) { debug( "%s isn't a potential JAXB type because its package is annotated as to be ignored.", declaration); return false; } if (isThrowable(declaration)) { debug( "%s isn't a potential JAXB type because it's an instance of java.lang.Throwable.", declaration); return false; } List<? extends AnnotationMirror> annotationMirrors = declaration.getAnnotationMirrors(); boolean explicitXMLTypeOrElement = false; for (AnnotationMirror mirror : annotationMirrors) { Element annotationDeclaration = mirror.getAnnotationType().asElement(); if (annotationDeclaration != null) { String fqn = annotationDeclaration instanceof TypeElement ? ((TypeElement) annotationDeclaration).getQualifiedName().toString() : ""; // exclude all XmlTransient types and all jaxws types. if (XmlTransient.class.getName().equals(fqn) || fqn.startsWith("javax.xml.ws") || fqn.startsWith("javax.ws.rs") || fqn.startsWith("javax.jws")) { debug("%s isn't a potential JAXB type because of annotation %s.", declaration, fqn); return false; } else { explicitXMLTypeOrElement = (XmlType.class.getName().equals(fqn)) || (XmlRootElement.class.getName().equals(fqn)); } } if (explicitXMLTypeOrElement) { break; } } return explicitXMLTypeOrElement; }
@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; }
public String getAutoTypeFromAnnotation(AnnotationMirror annotation) { Class annotation_class = NativeTypeTranslator.getClassFromType(annotation.getAnnotationType()); if (annotation_class.equals(GLint.class)) return "GL11.GL_INT"; else if (annotation_class.equals(GLbyte.class)) return "GL11.GL_BYTE"; else if (annotation_class.equals(GLshort.class)) return "GL11.GL_SHORT"; if (annotation_class.equals(GLuint.class)) return "GL11.GL_UNSIGNED_INT"; else if (annotation_class.equals(GLubyte.class)) return "GL11.GL_UNSIGNED_BYTE"; else if (annotation_class.equals(GLushort.class)) return "GL11.GL_UNSIGNED_SHORT"; else if (annotation_class.equals(GLfloat.class)) return "GL11.GL_FLOAT"; else if (annotation_class.equals(GLdouble.class)) return "GL11.GL_DOUBLE"; else return null; }
private static String wrapText(Element element, AnnotationMirror mirror, String text) { StringBuilder b = new StringBuilder(); if (element != null) { b.append("Element " + element.toString()); } if (mirror != null) { b.append(" at annotation @" + Utils.getSimpleName(mirror.getAnnotationType())); } if (b.length() > 0) { b.append(" is erroneous: ").append(text); return b.toString(); } else { return text; } }
private String getValue(AnnotationMirror am) { Map<? extends ExecutableElement, ? extends AnnotationValue> map = am.getElementValues(); if (map.size() != 1) throw new IllegalArgumentException(); AnnotationValue v = map.values().iterator().next(); return (String) v.getValue(); }
public ResourceMethod(MethodDeclaration delegate, Resource parent) { super(delegate); Set<String> httpMethods = new TreeSet<String>(); Collection<AnnotationMirror> mirrors = delegate.getAnnotationMirrors(); for (AnnotationMirror mirror : mirrors) { AnnotationTypeDeclaration annotationDeclaration = mirror.getAnnotationType().getDeclaration(); HttpMethod httpMethodInfo = annotationDeclaration.getAnnotation(HttpMethod.class); if (httpMethodInfo != null) { // request method designator found. httpMethods.add(httpMethodInfo.value()); } } if (httpMethods.isEmpty()) { throw new IllegalStateException( "A resource method must specify an HTTP method by using a request method designator annotation."); } this.httpMethods = httpMethods; Set<String> consumes; Consumes consumesInfo = delegate.getAnnotation(Consumes.class); if (consumesInfo != null) { consumes = new TreeSet<String>(Arrays.asList(JAXRSUtils.value(consumesInfo))); } else { consumes = new TreeSet<String>(parent.getConsumesMime()); } this.consumesMime = consumes; Set<String> produces; Produces producesInfo = delegate.getAnnotation(Produces.class); if (producesInfo != null) { produces = new TreeSet<String>(Arrays.asList(JAXRSUtils.value(producesInfo))); } else { produces = new TreeSet<String>(parent.getProducesMime()); } this.producesMime = produces; String subpath = null; Path pathInfo = delegate.getAnnotation(Path.class); if (pathInfo != null) { subpath = pathInfo.value(); } ResourceEntityParameter entityParameter; List<ResourceEntityParameter> declaredEntityParameters = new ArrayList<ResourceEntityParameter>(); List<ResourceParameter> resourceParameters; ResourceRepresentationMetadata outputPayload; ResourceMethodSignature signatureOverride = delegate.getAnnotation(ResourceMethodSignature.class); if (signatureOverride == null) { entityParameter = null; resourceParameters = new ArrayList<ResourceParameter>(); // if we're not overriding the signature, assume we use the real method signature. for (ParameterDeclaration parameterDeclaration : getParameters()) { if (ResourceParameter.isResourceParameter(parameterDeclaration)) { resourceParameters.add(new ResourceParameter(parameterDeclaration)); } else if (ResourceParameter.isFormBeanParameter(parameterDeclaration)) { resourceParameters.addAll(ResourceParameter.getFormBeanParameters(parameterDeclaration)); } else if (parameterDeclaration.getAnnotation(Context.class) == null) { entityParameter = new ResourceEntityParameter(this, parameterDeclaration); declaredEntityParameters.add(entityParameter); } } DecoratedTypeMirror returnTypeMirror; TypeHint hintInfo = getAnnotation(TypeHint.class); if (hintInfo != null) { try { Class hint = hintInfo.value(); AnnotationProcessorEnvironment env = net.sf.jelly.apt.Context.getCurrentEnvironment(); if (TypeHint.NO_CONTENT.class.equals(hint)) { returnTypeMirror = (DecoratedTypeMirror) TypeMirrorDecorator.decorate(env.getTypeUtils().getVoidType()); } else { String hintName = hint.getName(); if (TypeHint.NONE.class.equals(hint)) { hintName = hintInfo.qualifiedName(); } if (!"##NONE".equals(hintName)) { TypeDeclaration type = env.getTypeDeclaration(hintName); returnTypeMirror = (DecoratedTypeMirror) TypeMirrorDecorator.decorate(env.getTypeUtils().getDeclaredType(type)); } else { returnTypeMirror = (DecoratedTypeMirror) getReturnType(); } } } catch (MirroredTypeException e) { returnTypeMirror = (DecoratedTypeMirror) TypeMirrorDecorator.decorate(e.getTypeMirror()); } returnTypeMirror.setDocComment(((DecoratedTypeMirror) getReturnType()).getDocComment()); } else { returnTypeMirror = (DecoratedTypeMirror) getReturnType(); if (getJavaDoc().get("returnWrapped") != null) { // support jax-doclets. see http://jira.codehaus.org/browse/ENUNCIATE-690 String fqn = getJavaDoc().get("returnWrapped").get(0); AnnotationProcessorEnvironment env = net.sf.jelly.apt.Context.getCurrentEnvironment(); TypeDeclaration type = env.getTypeDeclaration(fqn); if (type != null) { returnTypeMirror = (DecoratedTypeMirror) TypeMirrorDecorator.decorate(env.getTypeUtils().getDeclaredType(type)); } } // in the case where the return type is com.sun.jersey.api.JResponse, // we can use the type argument to get the entity type if (returnTypeMirror.isClass() && returnTypeMirror.isInstanceOf("com.sun.jersey.api.JResponse")) { DecoratedClassType jresponse = (DecoratedClassType) returnTypeMirror; if (!jresponse.getActualTypeArguments().isEmpty()) { DecoratedTypeMirror responseType = (DecoratedTypeMirror) TypeMirrorDecorator.decorate( jresponse.getActualTypeArguments().iterator().next()); if (responseType.isDeclared()) { responseType.setDocComment(returnTypeMirror.getDocComment()); returnTypeMirror = responseType; } } } } outputPayload = returnTypeMirror.isVoid() ? null : new ResourceRepresentationMetadata(returnTypeMirror); } else { entityParameter = loadEntityParameter(signatureOverride); declaredEntityParameters.add(entityParameter); resourceParameters = loadResourceParameters(signatureOverride); outputPayload = loadOutputPayload(signatureOverride); } JavaDoc.JavaDocTagList doclets = getJavaDoc() .get( "RequestHeader"); // support jax-doclets. see // http://jira.codehaus.org/browse/ENUNCIATE-690 if (doclets != null) { for (String doclet : doclets) { int firstspace = doclet.indexOf(' '); String header = firstspace > 0 ? doclet.substring(0, firstspace) : doclet; String doc = ((firstspace > 0) && (firstspace + 1 < doclet.length())) ? doclet.substring(firstspace + 1) : ""; resourceParameters.add( new ExplicitResourceParameter(this, doc, header, ResourceParameterType.HEADER)); } } ArrayList<ResponseCode> statusCodes = new ArrayList<ResponseCode>(); ArrayList<ResponseCode> warnings = new ArrayList<ResponseCode>(); StatusCodes codes = getAnnotation(StatusCodes.class); if (codes != null) { for (org.codehaus.enunciate.jaxrs.ResponseCode code : codes.value()) { ResponseCode rc = new ResponseCode(); rc.setCode(code.code()); rc.setCondition(code.condition()); statusCodes.add(rc); } } doclets = getJavaDoc() .get("HTTP"); // support jax-doclets. see http://jira.codehaus.org/browse/ENUNCIATE-690 if (doclets != null) { for (String doclet : doclets) { int firstspace = doclet.indexOf(' '); String code = firstspace > 0 ? doclet.substring(0, firstspace) : doclet; String doc = ((firstspace > 0) && (firstspace + 1 < doclet.length())) ? doclet.substring(firstspace + 1) : ""; try { ResponseCode rc = new ResponseCode(); rc.setCode(Integer.parseInt(code)); rc.setCondition(doc); statusCodes.add(rc); } catch (NumberFormatException e) { // fall through... } } } Warnings warningInfo = getAnnotation(Warnings.class); if (warningInfo != null) { for (org.codehaus.enunciate.jaxrs.ResponseCode code : warningInfo.value()) { ResponseCode rc = new ResponseCode(); rc.setCode(code.code()); rc.setCondition(code.condition()); warnings.add(rc); } } codes = parent.getAnnotation(StatusCodes.class); if (codes != null) { for (org.codehaus.enunciate.jaxrs.ResponseCode code : codes.value()) { ResponseCode rc = new ResponseCode(); rc.setCode(code.code()); rc.setCondition(code.condition()); statusCodes.add(rc); } } warningInfo = parent.getAnnotation(Warnings.class); if (warningInfo != null) { for (org.codehaus.enunciate.jaxrs.ResponseCode code : warningInfo.value()) { ResponseCode rc = new ResponseCode(); rc.setCode(code.code()); rc.setCondition(code.condition()); warnings.add(rc); } } ResponseHeaders responseHeaders = parent.getAnnotation(ResponseHeaders.class); if (responseHeaders != null) { for (ResponseHeader header : responseHeaders.value()) { this.responseHeaders.put(header.name(), header.description()); } } responseHeaders = getAnnotation(ResponseHeaders.class); if (responseHeaders != null) { for (ResponseHeader header : responseHeaders.value()) { this.responseHeaders.put(header.name(), header.description()); } } doclets = getJavaDoc() .get( "ResponseHeader"); // support jax-doclets. see // http://jira.codehaus.org/browse/ENUNCIATE-690 if (doclets != null) { for (String doclet : doclets) { int firstspace = doclet.indexOf(' '); String header = firstspace > 0 ? doclet.substring(0, firstspace) : doclet; String doc = ((firstspace > 0) && (firstspace + 1 < doclet.length())) ? doclet.substring(firstspace + 1) : ""; this.responseHeaders.put(header, doc); } } this.entityParameter = entityParameter; this.resourceParameters = resourceParameters; this.subpath = subpath; this.parent = parent; this.statusCodes = statusCodes; this.warnings = warnings; this.representationMetadata = outputPayload; this.declaredEntityParameters = declaredEntityParameters; }