Example usage for javax.lang.model.element ExecutableElement getThrownTypes

List of usage examples for javax.lang.model.element ExecutableElement getThrownTypes

Introduction

In this page you can find the example usage for javax.lang.model.element ExecutableElement getThrownTypes.

Prototype

List<? extends TypeMirror> getThrownTypes();

Source Link

Document

Returns the exceptions and other throwables listed in this method or constructor's throws clause in declaration order.

Usage

From source file:Main.java

public static String getThrows(ExecutableElement d) {
    Collection<? extends TypeMirror> params = d.getThrownTypes();
    if (params.size() == 0)
        return "";
    StringBuilder sbuf = new StringBuilder(params.size() * 20);
    sbuf.append("throws ");
    for (TypeMirror param : params) {
        sbuf.append(param.toString());//  w  w w .  j  ava  2s  . c o  m
        sbuf.append(',');
    }
    sbuf.setLength(sbuf.length() - 1);
    return sbuf.toString();
}

From source file:org.mule.devkit.validation.RestValidator.java

@Override
public void validate(DevKitTypeElement typeElement, GeneratorContext context) throws ValidationException {

    for (ExecutableElement method : typeElement.getMethodsAnnotatedWith(RestCall.class)) {

        if (!method.getModifiers().contains(Modifier.ABSTRACT)) {
            throw new ValidationException(method, "@RestCall can only be applied to abstract methods");
        }/*w w w .  ja v a 2 s. c  om*/

        if (method.getThrownTypes().size() != 1) {
            throw new ValidationException(method, "@RestCall abstract method must throw IOException");
        }

        RestExceptionOn restExceptionOn = method.getAnnotation(RestExceptionOn.class);
        if (restExceptionOn != null) {
            if (restExceptionOn.statusCodeIs().length != 0 && restExceptionOn.statusCodeIsNot().length != 0) {
                throw new ValidationException(method,
                        "@RestExceptionOn can only be used with statusCodeIs or statusCodeIsNot. Not both.");
            }
            if (restExceptionOn.statusCodeIs().length == 0 && restExceptionOn.statusCodeIsNot().length == 0) {
                throw new ValidationException(method,
                        "@RestExceptionOn must have either statusCodeIs or statusCodeIsNot.");
            }
        }

        int nonAnnotatedParameterCount = 0;
        for (VariableElement parameter : method.getParameters()) {
            if (parameter.getAnnotation(RestUriParam.class) == null
                    && parameter.getAnnotation(RestHeaderParam.class) == null
                    && parameter.getAnnotation(RestQueryParam.class) == null) {
                nonAnnotatedParameterCount++;
            }
        }

        if (nonAnnotatedParameterCount > 1) {
            throw new ValidationException(method,
                    "Only one parameter can be used as payload, everything else must be annotated with @RestUriParam, @RestQueryParam or @RestHeaderParam.");
        }
    }

    for (VariableElement field : typeElement.getFieldsAnnotatedWith(RestUriParam.class)) {
        boolean getterFound = false;
        for (ExecutableElement method : typeElement.getMethods()) {
            if (method.getSimpleName().toString()
                    .equals("get" + StringUtils.capitalize(field.getSimpleName().toString()))) {
                getterFound = true;
                break;
            }
        }
        if (!getterFound) {
            throw new ValidationException(field,
                    "Cannot find a getter method for " + field.getSimpleName().toString()
                            + " but its being marked as URI parameter of a REST call.");
        }
    }

    if (typeElement.getFieldsAnnotatedWith(RestHttpClient.class).size() > 1) {
        throw new ValidationException(typeElement,
                "There can only be one field annotated with @RestHttpClient.");
    }

    if (typeElement.getFieldsAnnotatedWith(RestHttpClient.class).size() > 0) {
        if (!typeElement.getFieldsAnnotatedWith(RestHttpClient.class).get(0).asType().toString()
                .equals(HttpClient.class.getName())) {
            throw new ValidationException(typeElement.getFieldsAnnotatedWith(RestHttpClient.class).get(0),
                    "A field annotated with @RestHttpClient must be of type " + HttpClient.class.getName());
        }
    }

    for (VariableElement field : typeElement.getFieldsAnnotatedWith(RestHttpClient.class)) {
        boolean getterFound = false;
        for (ExecutableElement method : typeElement.getMethods()) {
            if (method.getSimpleName().toString()
                    .equals("get" + StringUtils.capitalize(field.getSimpleName().toString()))) {
                getterFound = true;
                break;
            }
        }
        if (!getterFound) {
            throw new ValidationException(field, "Cannot find a getter method for "
                    + field.getSimpleName().toString() + " but its being marked with @RestHttpClient.");
        }
    }

}

From source file:org.androidtransfuse.adapter.element.ASTElementFactory.java

/**
 * Build an ASTConstructor from the input ExecutableElement
 *
 * @param executableElement require input element
 * @return ASTConstructor//from w w  w. j  av a2 s. c  om
 */
public ASTConstructor getConstructor(ExecutableElement executableElement) {
    ImmutableList<ASTParameter> parameters = getParameters(executableElement.getParameters());
    ASTAccessModifier modifier = buildAccessModifier(executableElement);
    ImmutableSet<ASTType> throwsTypes = buildASTElementTypes(executableElement.getThrownTypes());

    return new ASTElementConstructor(executableElement, parameters, modifier, getAnnotations(executableElement),
            throwsTypes);
}

From source file:org.androidtransfuse.adapter.element.ASTElementFactory.java

/**
 * Build an ASTMethod from the provided ExecutableElement
 *
 * @param executableElement required input element
 * @return ASTMethod// w  w w . j a  va 2s . co m
 */
public ASTMethod getMethod(ExecutableElement executableElement) {

    ImmutableList<ASTParameter> parameters = getParameters(executableElement.getParameters());
    ASTAccessModifier modifier = buildAccessModifier(executableElement);
    ImmutableSet<ASTType> throwsTypes = buildASTElementTypes(executableElement.getThrownTypes());

    return new ASTElementMethod(executableElement, astTypeBuilderVisitor, parameters, modifier,
            getAnnotations(executableElement), throwsTypes);
}

From source file:com.googlecode.androidannotations.helper.ValidatorHelper.java

public void doesntThrowException(Element element, IsValid valid) {
    ExecutableElement executableElement = (ExecutableElement) element;

    if (executableElement.getThrownTypes().size() > 0) {
        valid.invalidate();//from ww  w  . jav a2 s  . c o m
        annotationHelper.printAnnotationError(element,
                "%s annotated methods should not declare throwing any exception");
    }
}

From source file:com.googlecode.androidannotations.helper.ValidatorHelper.java

public void throwsOnlyRestClientException(ExecutableElement element, IsValid valid) {
    List<? extends TypeMirror> thrownTypes = element.getThrownTypes();
    if (thrownTypes.size() > 0) {
        if (thrownTypes.size() > 1 || !thrownTypes.get(0).toString()
                .equals("org.springframework.web.client.RestClientException")) {
            valid.invalidate();/*from ww w .j  ava  2s  . com*/
            annotationHelper.printAnnotationError(element,
                    "%s annotated methods can only declare throwing a RestClientException");
        }
    }
}