Java Code Examples for javax.lang.model.element.ExecutableElement#getThrownTypes()

The following examples show how to use javax.lang.model.element.ExecutableElement#getThrownTypes() . 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: WebServiceWrapperGenerator.java    From jdk8u60 with GNU General Public License v2.0 8 votes vote down vote up
private boolean generateExceptionBeans(ExecutableElement method) {
    String beanPackage = packageName + PD_JAXWS_PACKAGE_PD.getValue();
    if (packageName.length() == 0)
        beanPackage = JAXWS_PACKAGE_PD.getValue();
    boolean beanGenerated = false;
    for (TypeMirror thrownType : method.getThrownTypes()) {
        TypeElement typeDecl = (TypeElement) ((DeclaredType) thrownType).asElement();
        if (typeDecl == null) {
            builder.processError(WebserviceapMessages.WEBSERVICEAP_COULD_NOT_FIND_TYPEDECL(
                    thrownType.toString(), context.getRound()));
            return false;
        }
        boolean tmp = generateExceptionBean(typeDecl, beanPackage);
        beanGenerated = beanGenerated || tmp;
    }
    return beanGenerated;
}
 
Example 2
Source File: IntimateProcesser.java    From Intimate with Apache License 2.0 6 votes vote down vote up
private void processGetField(RoundEnvironment roundEnv) {
    for (Element element : roundEnv.getElementsAnnotatedWith(GetField.class)) {
        RefTargetModel targetModel = getTargetModel(element);
        if (targetModel == null) {
            continue;
        }
        ExecutableElement executableElement = (ExecutableElement) element;
        GetField field = executableElement.getAnnotation(GetField.class);
        List<CName> parameterTypes = getParameterTypes(executableElement);
        if (parameterTypes.size() > 0) {
            Throw.error("@GetField Don't need parameter.  method:"
                    + executableElement.getSimpleName().toString());
        }

        RefFieldModel fieldModel = new RefFieldModel(field.value(),
                executableElement.getSimpleName().toString(),
                new CName(executableElement.getReturnType()),
                false,
                executableElement.getReturnType(),
                executableElement.getThrownTypes());
        targetModel.addField(fieldModel);
    }

}
 
Example 3
Source File: SignatureGenerator.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * MethodTypeSignature ::= [FormalTypeParameters]
 *         "(" {TypeSignature} ")" ReturnType {ThrowsSignature}.
 */
private void genMethodTypeSignature(ExecutableElement method, StringBuilder sb) {
  genOptFormalTypeParameters(method.getTypeParameters(), sb);
  sb.append('(');
  for (VariableElement param : method.getParameters()) {
    genTypeSignature(param.asType(), sb);
  }
  sb.append(')');
  genTypeSignature(method.getReturnType(), sb);
  List<? extends TypeMirror> thrownTypes = method.getThrownTypes();
  if (hasGenericSignature(thrownTypes)) {
    for (TypeMirror thrownType : thrownTypes) {
      sb.append('^');
      genTypeSignature(thrownType, sb);
    }
  }
}
 
Example 4
Source File: PubapiVisitor.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Creates a String representation of a method element with everything
 * necessary to track all public aspects of it in an API.
 * @param e Element to create String for.
 * @return String representation of element.
 */
protected String makeMethodString(ExecutableElement e) {
    StringBuilder result = new StringBuilder();
    for (Modifier modifier : e.getModifiers()) {
        result.append(modifier.toString());
        result.append(" ");
    }
    result.append(e.getReturnType().toString());
    result.append(" ");
    result.append(e.toString());

    List<? extends TypeMirror> thrownTypes = e.getThrownTypes();
    if (!thrownTypes.isEmpty()) {
        result.append(" throws ");
        for (Iterator<? extends TypeMirror> iterator = thrownTypes
                .iterator(); iterator.hasNext();) {
            TypeMirror typeMirror = iterator.next();
            result.append(typeMirror.toString());
            if (iterator.hasNext()) {
                result.append(", ");
            }
        }
    }
    return result.toString();
}
 
Example 5
Source File: WebServiceWrapperGenerator.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
private boolean generateExceptionBeans(ExecutableElement method) {
    String beanPackage = packageName + PD_JAXWS_PACKAGE_PD.getValue();
    if (packageName.length() == 0)
        beanPackage = JAXWS_PACKAGE_PD.getValue();
    boolean beanGenerated = false;
    for (TypeMirror thrownType : method.getThrownTypes()) {
        TypeElement typeDecl = (TypeElement) ((DeclaredType) thrownType).asElement();
        if (typeDecl == null) {
            builder.processError(WebserviceapMessages.WEBSERVICEAP_COULD_NOT_FIND_TYPEDECL(
                    thrownType.toString(), context.getRound()));
            return false;
        }
        boolean tmp = generateExceptionBean(typeDecl, beanPackage);
        beanGenerated = beanGenerated || tmp;
    }
    return beanGenerated;
}
 
Example 6
Source File: WebServiceWrapperGenerator.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
private boolean generateExceptionBeans(ExecutableElement method) {
    String beanPackage = packageName + PD_JAXWS_PACKAGE_PD.getValue();
    if (packageName.length() == 0)
        beanPackage = JAXWS_PACKAGE_PD.getValue();
    boolean beanGenerated = false;
    for (TypeMirror thrownType : method.getThrownTypes()) {
        TypeElement typeDecl = (TypeElement) ((DeclaredType) thrownType).asElement();
        if (typeDecl == null) {
            builder.processError(WebserviceapMessages.WEBSERVICEAP_COULD_NOT_FIND_TYPEDECL(
                    thrownType.toString(), context.getRound()));
            return false;
        }
        boolean tmp = generateExceptionBean(typeDecl, beanPackage);
        beanGenerated = beanGenerated || tmp;
    }
    return beanGenerated;
}
 
Example 7
Source File: PubapiVisitor.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Creates a String representation of a method element with everything
 * necessary to track all public aspects of it in an API.
 * @param e Element to create String for.
 * @return String representation of element.
 */
protected String makeMethodString(ExecutableElement e) {
    StringBuilder result = new StringBuilder();
    for (Modifier modifier : e.getModifiers()) {
        result.append(modifier.toString());
        result.append(" ");
    }
    result.append(e.getReturnType().toString());
    result.append(" ");
    result.append(e.toString());

    List<? extends TypeMirror> thrownTypes = e.getThrownTypes();
    if (!thrownTypes.isEmpty()) {
        result.append(" throws ");
        for (Iterator<? extends TypeMirror> iterator = thrownTypes
                .iterator(); iterator.hasNext();) {
            TypeMirror typeMirror = iterator.next();
            result.append(typeMirror.toString());
            if (iterator.hasNext()) {
                result.append(", ");
            }
        }
    }
    return result.toString();
}
 
Example 8
Source File: IntimateProcesser.java    From Intimate with Apache License 2.0 6 votes vote down vote up
private void processMethod(RoundEnvironment roundEnv) {
    for (Element element : roundEnv.getElementsAnnotatedWith(Method.class)) {
        RefTargetModel targetModel = getTargetModel(element);
        if (targetModel == null) {
            continue;
        }
        ExecutableElement executableElement = (ExecutableElement) element;
        Method method = executableElement.getAnnotation(Method.class);

        List<CName> parameterTypes = getParameterTypes(executableElement);
        String name = method.value();
        if ("".equals(name)) {
            name = executableElement.getSimpleName().toString();
        }

        RefMethodModel methodModel = new RefMethodModel(name,
                executableElement.getReturnType(),
                parameterTypes,
                executableElement.getThrownTypes());
        targetModel.addMethod(methodModel);
    }
}
 
Example 9
Source File: WebServiceWrapperGenerator.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private boolean generateExceptionBeans(ExecutableElement method) {
    String beanPackage = packageName + PD_JAXWS_PACKAGE_PD.getValue();
    if (packageName.length() == 0)
        beanPackage = JAXWS_PACKAGE_PD.getValue();
    boolean beanGenerated = false;
    for (TypeMirror thrownType : method.getThrownTypes()) {
        TypeElement typeDecl = (TypeElement) ((DeclaredType) thrownType).asElement();
        if (typeDecl == null) {
            builder.processError(WebserviceapMessages.WEBSERVICEAP_COULD_NOT_FIND_TYPEDECL(
                    thrownType.toString(), context.getRound()));
            return false;
        }
        boolean tmp = generateExceptionBean(typeDecl, beanPackage);
        beanGenerated = beanGenerated || tmp;
    }
    return beanGenerated;
}
 
Example 10
Source File: PubapiVisitor.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Creates a String representation of a method element with everything
 * necessary to track all public aspects of it in an API.
 * @param e Element to create String for.
 * @return String representation of element.
 */
protected String makeMethodString(ExecutableElement e) {
    StringBuilder result = new StringBuilder();
    for (Modifier modifier : e.getModifiers()) {
        result.append(modifier.toString());
        result.append(" ");
    }
    result.append(e.getReturnType().toString());
    result.append(" ");
    result.append(e.toString());

    List<? extends TypeMirror> thrownTypes = e.getThrownTypes();
    if (!thrownTypes.isEmpty()) {
        result.append(" throws ");
        for (Iterator<? extends TypeMirror> iterator = thrownTypes
                .iterator(); iterator.hasNext();) {
            TypeMirror typeMirror = iterator.next();
            result.append(typeMirror.toString());
            if (iterator.hasNext()) {
                result.append(", ");
            }
        }
    }
    return result.toString();
}
 
Example 11
Source File: MethodGenerator.java    From OkDeepLink with Apache License 2.0 5 votes vote down vote up
void addExceptions(ExecutableElement annotatedMtd, MethodSpec.Builder builder) {
    List<? extends TypeMirror> exceptions = annotatedMtd.getThrownTypes();
    if (exceptions != null){
        for (TypeMirror exc : exceptions) {
            builder.addException(ClassName.get(exc));
        }
    }
}
 
Example 12
Source File: ElementTo.java    From sundrio with Apache License 2.0 5 votes vote down vote up
public Method apply(ExecutableElement executableElement) {
    Map<AttributeKey, Object> attributes = new HashMap<>();
    if (executableElement.getDefaultValue() != null) {
       attributes.put(Attributeable.DEFAULT_VALUE, executableElement.getDefaultValue().getValue());
    }
    MethodBuilder methodBuilder = new MethodBuilder()
            .withModifiers(TypeUtils.modifiersToInt(executableElement.getModifiers()))
            .withName(executableElement.getSimpleName().toString())
            .withReturnType(MIRROR_TO_TYPEREF.apply(executableElement.getReturnType()))
            .withAttributes(attributes);



    //Populate constructor parameters
    for (VariableElement variableElement : executableElement.getParameters()) {
        methodBuilder = methodBuilder.addToArguments(PROPERTY.apply(variableElement));

        List<ClassRef> exceptionRefs = new ArrayList<ClassRef>();
        for (TypeMirror thrownType : executableElement.getThrownTypes()) {
            if (thrownType instanceof ClassRef) {
                exceptionRefs.add((ClassRef) thrownType);
            }
        }
        methodBuilder = methodBuilder.withExceptions(exceptionRefs);
    }

    List<ClassRef> annotationRefs = new ArrayList<ClassRef>();
    for (AnnotationMirror annotationMirror : executableElement.getAnnotationMirrors()) {
        methodBuilder.withAnnotations(ANNOTATION_REF.apply(annotationMirror));
    }
    return methodBuilder.build();
}
 
Example 13
Source File: JavaSourceParserUtil.java    From jeddict with Apache License 2.0 5 votes vote down vote up
public static List<String> getExceptionsThrown(WorkingCopy workingCopy, String fqClass, String methodName, List<String> formalParamFqTypes) {
    if (formalParamFqTypes == null) {
        formalParamFqTypes = Collections.<String>emptyList();
    }
    ExecutableElement desiredMethodElement = null;
    TypeElement suppliedTypeElement = workingCopy.getElements().getTypeElement(fqClass);
    TypeElement typeElement = suppliedTypeElement;
    whileloop:
    while (typeElement != null) {
        for (ExecutableElement methodElement : ElementFilter.methodsIn(typeElement.getEnclosedElements())) {
            if (methodElement.getSimpleName().contentEquals(methodName)) {
                List<? extends VariableElement> formalParamElements = methodElement.getParameters();
                //for now, just check sizes
                if (formalParamElements.size() == formalParamFqTypes.size()) {
                    desiredMethodElement = methodElement;
                    break whileloop;
                }
            }
        }
        typeElement = getSuperclassTypeElement(typeElement);
    }
    if (desiredMethodElement == null) {
        throw new IllegalArgumentException("Could not find " + methodName + " in " + fqClass);
    }
    List<String> result = new ArrayList<>();
    List<? extends TypeMirror> thrownTypes = desiredMethodElement.getThrownTypes();
    for (TypeMirror thrownType : thrownTypes) {
        if (thrownType.getKind() == TypeKind.DECLARED) {
            DeclaredType thrownDeclaredType = (DeclaredType) thrownType;
            TypeElement thrownElement = (TypeElement) thrownDeclaredType.asElement();
            String thrownFqClass = thrownElement.getQualifiedName().toString();
            result.add(thrownFqClass);
        } else {
            result.add(null);
        }
    }
    return result;
}
 
Example 14
Source File: WebServiceVisitor.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
protected boolean isValidOneWayMethod(ExecutableElement method, TypeElement typeElement) {
    boolean valid = true;
    if (!(method.getReturnType().accept(NO_TYPE_VISITOR, null))) {
        // this is an error, cannot be OneWay and have a return type
        builder.processError(WebserviceapMessages.WEBSERVICEAP_ONEWAY_OPERATION_CANNOT_HAVE_RETURN_TYPE(typeElement.getQualifiedName(), method.toString()), method);
        valid = false;
    }
    VariableElement outParam = getOutParameter(method);
    if (outParam != null) {
        builder.processError(WebserviceapMessages.WEBSERVICEAP_ONEWAY_AND_OUT(typeElement.getQualifiedName(), method.toString()), outParam);
        valid = false;
    }
    if (!isDocLitWrapped() && soapStyle.equals(SOAPStyle.DOCUMENT)) {
        int inCnt = getModeParameterCount(method, WebParam.Mode.IN);
        if (inCnt != 1) {
            builder.processError(WebserviceapMessages.WEBSERVICEAP_ONEWAY_AND_NOT_ONE_IN(typeElement.getQualifiedName(), method.toString()), method);
            valid = false;
        }
    }
    for (TypeMirror thrownType : method.getThrownTypes()) {
        TypeElement thrownElement = (TypeElement) ((DeclaredType) thrownType).asElement();
        if (builder.isServiceException(thrownType)) {
            builder.processError(WebserviceapMessages.WEBSERVICEAP_ONEWAY_OPERATION_CANNOT_DECLARE_EXCEPTIONS(
                    typeElement.getQualifiedName(), method.toString(), thrownElement.getQualifiedName()), method);
            valid = false;
        }
    }
    return valid;
}
 
Example 15
Source File: MethodSpec.java    From JReFrameworker with MIT License 5 votes vote down vote up
/**
 * Returns a new method spec builder that overrides {@code method}.
 *
 * <p>This will copy its visibility modifiers, type parameters, return type, name, parameters, and
 * throws declarations. An {@link Override} annotation will be added.
 *
 * <p>Note that in JavaPoet 1.2 through 1.7 this method retained annotations from the method and
 * parameters of the overridden method. Since JavaPoet 1.8 annotations must be added separately.
 */
public static Builder overriding(ExecutableElement method) {
  checkNotNull(method, "method == null");

  Set<Modifier> modifiers = method.getModifiers();
  if (modifiers.contains(Modifier.PRIVATE)
      || modifiers.contains(Modifier.FINAL)
      || modifiers.contains(Modifier.STATIC)) {
    throw new IllegalArgumentException("cannot override method with modifiers: " + modifiers);
  }

  String methodName = method.getSimpleName().toString();
  MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder(methodName);

  methodBuilder.addAnnotation(Override.class);

  modifiers = new LinkedHashSet<>(modifiers);
  modifiers.remove(Modifier.ABSTRACT);
  modifiers.remove(Util.DEFAULT); // LinkedHashSet permits null as element for Java 7
  methodBuilder.addModifiers(modifiers);

  for (TypeParameterElement typeParameterElement : method.getTypeParameters()) {
    TypeVariable var = (TypeVariable) typeParameterElement.asType();
    methodBuilder.addTypeVariable(TypeVariableName.get(var));
  }

  methodBuilder.returns(TypeName.get(method.getReturnType()));
  methodBuilder.addParameters(ParameterSpec.parametersOf(method));
  methodBuilder.varargs(method.isVarArgs());

  for (TypeMirror thrownType : method.getThrownTypes()) {
    methodBuilder.addException(TypeName.get(thrownType));
  }

  return methodBuilder;
}
 
Example 16
Source File: WebServiceVisitor.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
protected boolean isValidOneWayMethod(ExecutableElement method, TypeElement typeElement) {
    boolean valid = true;
    if (!(method.getReturnType().accept(NO_TYPE_VISITOR, null))) {
        // this is an error, cannot be OneWay and have a return type
        builder.processError(WebserviceapMessages.WEBSERVICEAP_ONEWAY_OPERATION_CANNOT_HAVE_RETURN_TYPE(typeElement.getQualifiedName(), method.toString()), method);
        valid = false;
    }
    VariableElement outParam = getOutParameter(method);
    if (outParam != null) {
        builder.processError(WebserviceapMessages.WEBSERVICEAP_ONEWAY_AND_OUT(typeElement.getQualifiedName(), method.toString()), outParam);
        valid = false;
    }
    if (!isDocLitWrapped() && soapStyle.equals(SOAPStyle.DOCUMENT)) {
        int inCnt = getModeParameterCount(method, WebParam.Mode.IN);
        if (inCnt != 1) {
            builder.processError(WebserviceapMessages.WEBSERVICEAP_ONEWAY_AND_NOT_ONE_IN(typeElement.getQualifiedName(), method.toString()), method);
            valid = false;
        }
    }
    for (TypeMirror thrownType : method.getThrownTypes()) {
        TypeElement thrownElement = (TypeElement) ((DeclaredType) thrownType).asElement();
        if (builder.isServiceException(thrownType)) {
            builder.processError(WebserviceapMessages.WEBSERVICEAP_ONEWAY_OPERATION_CANNOT_DECLARE_EXCEPTIONS(
                    typeElement.getQualifiedName(), method.toString(), thrownElement.getQualifiedName()), method);
            valid = false;
        }
    }
    return valid;
}
 
Example 17
Source File: DebugASTPrinter.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Override
public boolean visit(MethodDeclaration node) {
  sb.printIndent();
  printAnnotations(node.getAnnotations());
  printModifiers(node.getModifiers());
  ExecutableElement meth = node.getExecutableElement();
  printTypeParameters(meth.getTypeParameters());
  if (!node.isConstructor()) {
    sb.print(node.getReturnTypeMirror().toString());
    sb.print(' ');
  }
  sb.print(ElementUtil.getName(meth));
  sb.print("(");
  for (Iterator<SingleVariableDeclaration> it = node.getParameters().iterator(); it.hasNext(); ) {
    it.next().accept(this);
    if (it.hasNext()) {
      sb.print(',');
    }
  }
  sb.print(")");
  List<? extends TypeMirror> exceptions = meth.getThrownTypes();
  if (exceptions.size() > 0) {
    sb.print(" throws ");
    for (int i = 0; i < exceptions.size(); ) {
      sb.print(exceptions.get(i).toString());
      if (++i < exceptions.size()){
        sb.print(',');
      }
    }
    sb.print(' ');
  }
  printMethodBody(node);
  return false;
}
 
Example 18
Source File: ToothpickProcessor.java    From toothpick with Apache License 2.0 5 votes vote down vote up
protected List<TypeElement> getExceptionTypes(ExecutableElement methodElement) {
  List<TypeElement> exceptionClassNames = new ArrayList<>();
  for (TypeMirror thrownTypeMirror : methodElement.getThrownTypes()) {
    DeclaredType thrownDeclaredType = (DeclaredType) thrownTypeMirror;
    TypeElement thrownType = (TypeElement) thrownDeclaredType.asElement();
    exceptionClassNames.add(thrownType);
  }

  return exceptionClassNames;
}
 
Example 19
Source File: DependencyCollector.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Method adds dependencies on its return type, and parameter types
 */
@Override
public Object visitMethod(MethodTree node, Object p) {
    ExecutableElement e = (ExecutableElement) info.getTrees().getElement(getCurrentPath());
    addDependency(e.getReturnType());
    // parameters will be captured by visit(VariableTree).
    for (TypeMirror tm : e.getThrownTypes()) {
        addDependency(tm);
    }
    return super.visitMethod(node, p);
}
 
Example 20
Source File: MethodSpec.java    From javapoet with Apache License 2.0 4 votes vote down vote up
/**
 * Returns a new method spec builder that overrides {@code method}.
 *
 * <p>This will copy its visibility modifiers, type parameters, return type, name, parameters, and
 * throws declarations. An {@link Override} annotation will be added.
 *
 * <p>Note that in JavaPoet 1.2 through 1.7 this method retained annotations from the method and
 * parameters of the overridden method. Since JavaPoet 1.8 annotations must be added separately.
 */
public static Builder overriding(ExecutableElement method) {
  checkNotNull(method, "method == null");

  Element enclosingClass = method.getEnclosingElement();
  if (enclosingClass.getModifiers().contains(Modifier.FINAL)) {
    throw new IllegalArgumentException("Cannot override method on final class " + enclosingClass);
  }

  Set<Modifier> modifiers = method.getModifiers();
  if (modifiers.contains(Modifier.PRIVATE)
      || modifiers.contains(Modifier.FINAL)
      || modifiers.contains(Modifier.STATIC)) {
    throw new IllegalArgumentException("cannot override method with modifiers: " + modifiers);
  }

  String methodName = method.getSimpleName().toString();
  MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder(methodName);

  methodBuilder.addAnnotation(Override.class);

  modifiers = new LinkedHashSet<>(modifiers);
  modifiers.remove(Modifier.ABSTRACT);
  modifiers.remove(Modifier.DEFAULT);
  methodBuilder.addModifiers(modifiers);

  for (TypeParameterElement typeParameterElement : method.getTypeParameters()) {
    TypeVariable var = (TypeVariable) typeParameterElement.asType();
    methodBuilder.addTypeVariable(TypeVariableName.get(var));
  }

  methodBuilder.returns(TypeName.get(method.getReturnType()));
  methodBuilder.addParameters(ParameterSpec.parametersOf(method));
  methodBuilder.varargs(method.isVarArgs());

  for (TypeMirror thrownType : method.getThrownTypes()) {
    methodBuilder.addException(TypeName.get(thrownType));
  }

  return methodBuilder;
}