Java Code Examples for com.github.javaparser.ast.body.MethodDeclaration#isAnnotationPresent()

The following examples show how to use com.github.javaparser.ast.body.MethodDeclaration#isAnnotationPresent() . 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
Source File: RequestMappingHelper.java    From apigcc with MIT License 6 votes vote down vote up
/**
 * 判断是否为Rest接口
 * @param n
 * @return
 */
public static boolean isRest(MethodDeclaration n){
    if(n.isAnnotationPresent(ANNOTATION_RESPONSE_BODY)){
        return true;
    }
    Optional<Node> parentOptional = n.getParentNode();
    if(parentOptional.isPresent()){
        Node parentNode = parentOptional.get();
        if(parentNode instanceof ClassOrInterfaceDeclaration){
            if (((ClassOrInterfaceDeclaration) parentNode).isAnnotationPresent(SpringParser.ANNOTATION_REST_CONTROLLER)) {
                return true;
            }
        }
    }
    return false;
}
 
Example 2
Source File: RequestMappingHelper.java    From apigcc with MIT License 5 votes vote down vote up
/**
 * 解析HTTP Method
 * @param n
 * @return
 */
public static Method pickMethod(MethodDeclaration n){
    if(n.isAnnotationPresent(ANNOTATION_GET_MAPPING)){
        return Method.GET;
    }
    if(n.isAnnotationPresent(ANNOTATION_POST_MAPPING)){
        return Method.POST;
    }
    if(n.isAnnotationPresent(ANNOTATION_PUT_MAPPING)){
        return Method.PUT;
    }
    if(n.isAnnotationPresent(ANNOTATION_PATCH_MAPPING)){
        return Method.PATCH;
    }
    if(n.isAnnotationPresent(ANNOTATION_DELETE_MAPPING)){
        return Method.DELETE;
    }
    if(n.isAnnotationPresent(ANNOTATION_REQUEST_MAPPING)){
        AnnotationExpr annotationExpr = n.getAnnotationByName(ANNOTATION_REQUEST_MAPPING).get();
        Optional<Expression> expressionOptional = AnnotationHelper.attr(annotationExpr, "method");
        if (expressionOptional.isPresent()) {
            Expression expression = expressionOptional.get();
            if(expression.isArrayInitializerExpr()){
                NodeList<Expression> values = expression.asArrayInitializerExpr().getValues();
                if(values!=null&&values.size()>0){
                    return Method.valueOf(values.get(0).toString().replaceAll("RequestMethod.",""));
                }
            }
            return Method.of(expression.toString().replaceAll("RequestMethod.",""));
        }
    }
    return Method.GET;
}
 
Example 3
Source File: OpenApiObjectGenerator.java    From flow with Apache License 2.0 5 votes vote down vote up
private boolean isAccessForbidden(
        ClassOrInterfaceDeclaration typeDeclaration,
        MethodDeclaration methodDeclaration) {
    return !methodDeclaration.isPublic()
            || (hasSecurityAnnotation(methodDeclaration)
                    ? methodDeclaration.isAnnotationPresent(DenyAll.class)
                    : typeDeclaration.isAnnotationPresent(DenyAll.class));
}
 
Example 4
Source File: OpenApiObjectGenerator.java    From flow with Apache License 2.0 5 votes vote down vote up
private MediaType createReturnMediaType(
        MethodDeclaration methodDeclaration) {
    MediaType mediaItem = new MediaType();
    Type methodReturnType = methodDeclaration.getType();
    Schema schema = parseTypeToSchema(methodReturnType, "");
    if (methodDeclaration.isAnnotationPresent(Nullable.class)) {
        schema = schemaResolver.createNullableWrapper(schema);
    }
    usedTypes.putAll(collectUsedTypesFromSchema(schema));
    mediaItem.schema(schema);
    return mediaItem;
}
 
Example 5
Source File: OpenApiObjectGenerator.java    From flow with Apache License 2.0 4 votes vote down vote up
private boolean hasSecurityAnnotation(MethodDeclaration method) {
    return method.isAnnotationPresent(AnonymousAllowed.class)
            || method.isAnnotationPresent(PermitAll.class)
            || method.isAnnotationPresent(DenyAll.class)
            || method.isAnnotationPresent(RolesAllowed.class);
}