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

The following examples show how to use javax.lang.model.element.ExecutableElement#getParameters() . These examples are extracted from open source projects. 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 Project: netbeans   File: JavaElementHandler.java    License: Apache License 2.0 6 votes vote down vote up
private List<String> getParameterListForMethod(ExecutableElement exe) {
    List<String> parameters = new ArrayList<String>();

    if (exe != null) {
        // generate a list of parameters
        // unfortunately, we have to work around # 139695 in an ugly fashion

        try {
            List<? extends VariableElement> params = exe.getParameters(); // this can cause NPE's

            for (VariableElement variableElement : params) {
                TypeMirror tm = variableElement.asType();

                if (tm.getKind() == TypeKind.DECLARED || tm.getKind() == TypeKind.ARRAY) {
                    parameters.add(GroovyUtils.stripPackage(tm.toString()));
                } else {
                    parameters.add(tm.toString());
                }
            }
        } catch (NullPointerException e) {
            // simply do nothing.
        }
    }
    return parameters;
}
 
Example 2
protected int getModeParameterCount(ExecutableElement method, WebParam.Mode mode) {
    WebParam webParam;
    int cnt = 0;
    for (VariableElement param : method.getParameters()) {
        webParam = param.getAnnotation(WebParam.class);
        if (webParam != null) {
            if (webParam.header())
                continue;
            if (isEquivalentModes(mode, webParam.mode()))
                cnt++;
        } else {
            if (isEquivalentModes(mode, WebParam.Mode.IN)) {
                cnt++;
            }
        }
    }
    return cnt;
}
 
Example 3
Source Project: openjdk-jdk8u   File: LLNI.java    License: GNU General Public License v2.0 6 votes vote down vote up
protected final String jniMethodName(ExecutableElement method, String cname,
                                     boolean longName)
            throws TypeSignature.SignatureException {
    String res = "Java_" + cname + "_" + method.getSimpleName();

    if (longName) {
        TypeMirror mType =  types.erasure(method.getReturnType());
        List<? extends VariableElement> params = method.getParameters();
        List<TypeMirror> argTypes = new ArrayList<TypeMirror>();
        for (VariableElement param: params) {
            argTypes.add(types.erasure(param.asType()));
        }

        res = res + "__";
        for (TypeMirror t: argTypes) {
            String tname = t.toString();
            TypeSignature newTypeSig = new TypeSignature(elems);
            String sig = newTypeSig.getTypeSignature(tname);
            res = res + nameToIdentifier(sig);
        }
    }
    return res;
}
 
Example 4
Source Project: SimpleWeibo   File: RetroWeiboProcessor.java    License: Apache License 2.0 6 votes vote down vote up
public String buildCallbackType(ExecutableElement method) {
  Types typeUtils = processingEnv.getTypeUtils();
  TypeMirror callback = getTypeMirror(processingEnv, RetroWeibo.Callback.class);

  List<? extends VariableElement> parameters = method.getParameters();
  for (VariableElement parameter : parameters) {
    TypeMirror type = parameter.asType();
    if (type instanceof DeclaredType) {
      List<? extends TypeMirror> params = ((DeclaredType) type).getTypeArguments();
      if (params.size() == 1) {
        callback = typeUtils.getDeclaredType((TypeElement) typeUtils
                .asElement(callback), new TypeMirror[] {params.get(0)});

        if (typeUtils.isSubtype(type, callback)) {
          return typeSimplifier.simplify(params.get(0));
        }
      }
    }
  }
  return "";
}
 
Example 5
private String signature(ExecutableElement e) {
    StringBuilder sb = new StringBuilder();
    String sep = "(";
    for (VariableElement p: e.getParameters()) {
        sb.append(sep);
        sb.append(types.erasure(p.asType()).toString());
        sep = ",";
    }
    sb.append(")");
    return sb.toString();
}
 
Example 6
Source Project: JReFrameworker   File: ParameterSpec.java    License: MIT License 5 votes vote down vote up
static List<ParameterSpec> parametersOf(ExecutableElement method) {
  List<ParameterSpec> result = new ArrayList<>();
  for (VariableElement parameter : method.getParameters()) {
    result.add(ParameterSpec.get(parameter));
  }
  return result;
}
 
Example 7
Source Project: jackdaw   File: TypeUtils.java    License: Apache License 2.0 5 votes vote down vote up
public static boolean isSimpleMethod(final Element element) {
    if (element instanceof ExecutableElement) {
        final ExecutableElement ee = (ExecutableElement) element;
        final List<? extends VariableElement> parameters = ee.getParameters();

        return !hasAnyModifier(ee, Modifier.STATIC) && parameters.isEmpty();
    }
    return false;
}
 
Example 8
Source Project: TencentKona-8   File: Gen.java    License: GNU General Public License v2.0 5 votes vote down vote up
String signature(ExecutableElement e) {
    StringBuilder sb = new StringBuilder("(");
    String sep = "";
    for (VariableElement p: e.getParameters()) {
        sb.append(sep);
        sb.append(types.erasure(p.asType()).toString());
        sep = ",";
    }
    sb.append(")");
    return sb.toString();
}
 
Example 9
Source Project: j2objc   File: ElementUtil.java    License: Apache License 2.0 5 votes vote down vote up
private static boolean paramsMatch(ExecutableElement method, String[] paramTypes) {
  List<? extends VariableElement> params = method.getParameters();
  int size = params.size();
  if (size != paramTypes.length) {
    return false;
  }
  for (int i = 0; i < size; i++) {
    if (!TypeUtil.getQualifiedName(params.get(i).asType()).equals(paramTypes[i])) {
      return false;
    }
  }
  return true;
}
 
Example 10
Source Project: revapi   File: ClasspathScanner.java    License: Apache License 2.0 5 votes vote down vote up
void scanMethod(TypeRecord owningType, ExecutableElement method) {
    if (shouldBeIgnored(method)) {
        owningType.inaccessibleDeclaredNonClassMembers.add(method);
        return;
    }

    owningType.accessibleDeclaredNonClassMembers.add(method);

    TypeElement returnType = method.getReturnType().accept(getTypeElement, null);
    if (returnType != null) {
        addUse(owningType, method, returnType, UseSite.Type.RETURN_TYPE);
        addType(returnType, false);
        addTypeParamUses(owningType, method, method.getReturnType());
    }

    int idx = 0;
    for (VariableElement p : method.getParameters()) {
        TypeElement pt = p.asType().accept(getTypeElement, null);
        if (pt != null) {
            addUse(owningType, method, pt, UseSite.Type.PARAMETER_TYPE, idx++);
            addType(pt, false);
            addTypeParamUses(owningType, method, p.asType());
        }

        p.getAnnotationMirrors().forEach(a -> scanAnnotation(owningType, p, a));
    }

    method.getThrownTypes().forEach(t -> {
        TypeElement ex = t.accept(getTypeElement, null);
        if (ex != null) {
            addUse(owningType, method, ex, UseSite.Type.IS_THROWN);
            addType(ex, false);
            addTypeParamUses(owningType, method, t);
        }

        t.getAnnotationMirrors().forEach(a -> scanAnnotation(owningType, method, a));
    });

    method.getAnnotationMirrors().forEach(a -> scanAnnotation(owningType, method, a));
}
 
Example 11
private void checkMethodParameters(Set<TypeElement> classes) {
    Types types = processingEnv.getTypeUtils();
    for (TypeElement te: classes) {
        for (ExecutableElement ee: ElementFilter.methodsIn(te.getEnclosedElements())) {
            for (VariableElement ve: ee.getParameters()) {
                TypeMirror tm = ve.asType();
                checkMethodParametersVisitor.visit(tm, types);
            }
        }
    }
}
 
Example 12
Source Project: pocketknife   File: BuilderProcessor.java    License: Apache License 2.0 5 votes vote down vote up
private FragmentMethodBinding getFragmentMethodBinding(ExecutableElement element) throws InvalidTypeException {
    FragmentMethodBinding binding = new FragmentMethodBinding(element.getSimpleName().toString(), element.getReturnType());
    for (Element parameter : element.getParameters()) {
        binding.addField(getBundleFieldBinding(parameter));
    }
    return binding;
}
 
Example 13
protected VariableElement getOutParameter(ExecutableElement method) {
    WebParam webParam;
    for (VariableElement param : method.getParameters()) {
        webParam = param.getAnnotation(WebParam.class);
        if (webParam != null && webParam.mode() != WebParam.Mode.IN) {
            return param;
        }
    }
    return null;
}
 
Example 14
Source Project: openjdk-jdk8u   File: LLNI.java    License: GNU General Public License v2.0 5 votes vote down vote up
protected String methodDecl(ExecutableElement method,
                            TypeElement clazz, String cname)
        throws TypeSignature.SignatureException, Util.Exit {
    String res = null;

    TypeMirror retType = types.erasure(method.getReturnType());
    String typesig = signature(method);
    TypeSignature newTypeSig = new TypeSignature(elems);
    String sig = newTypeSig.getTypeSignature(typesig, retType);
    boolean longName = needLongName(method, clazz);

    if (sig.charAt(0) != '(')
        util.error("invalid.method.signature", sig);


    res = "JNIEXPORT " + jniType(retType) + " JNICALL" + lineSep + jniMethodName(method, cname, longName)
        + "(JNIEnv *, " + cRcvrDecl(method, cname);
    List<? extends VariableElement> params = method.getParameters();
    List<TypeMirror> argTypes = new ArrayList<TypeMirror>();
    for (VariableElement p: params){
        argTypes.add(types.erasure(p.asType()));
    }

    /* It would have been nice to include the argument names in the
       declaration, but there seems to be a bug in the "BinaryField"
       class, causing the getArguments() method to return "null" for
       most (non-constructor) methods. */
    for (TypeMirror argType: argTypes)
        res = res + ", " + jniType(argType);
    res = res + ");" + lineSep;
    return res;
}
 
Example 15
Source Project: Aria   File: ParamObtainUtil.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * 查找并保存扫描到的方法
 */
void saveMethod(TaskEnum taskEnum, RoundEnvironment roundEnv,
    Class<? extends Annotation> annotationClazz, int annotationType) {
  for (Element element : roundEnv.getElementsAnnotatedWith(annotationClazz)) {
    ElementKind kind = element.getKind();
    if (kind == ElementKind.METHOD) {
      ExecutableElement method = (ExecutableElement) element;
      TypeElement classElement = (TypeElement) method.getEnclosingElement();
      PackageElement packageElement = mElementUtil.getPackageOf(classElement);

      String methodName = method.getSimpleName().toString();
      String className = method.getEnclosingElement().toString(); //全类名
      String key = className + taskEnum.proxySuffix;
      ProxyClassParam proxyEntity = mMethodParams.get(key);
      MethodInfo methodInfo = new MethodInfo();
      methodInfo.methodName = methodName;
      methodInfo.params = (List<VariableElement>) method.getParameters();
      if (taskEnum == TaskEnum.M3U8_PEER) {
        checkM3U8PeerMethod(method, methodInfo.params);
      } else {
        checkTaskMethod(taskEnum, method, annotationClazz, methodInfo.params);
      }

      if (proxyEntity == null) {
        proxyEntity = new ProxyClassParam();
        proxyEntity.taskEnums = new HashSet<>();
        proxyEntity.packageName = packageElement.getQualifiedName().toString();
        proxyEntity.className = classElement.getSimpleName().toString();
        proxyEntity.proxyClassName = proxyEntity.className + taskEnum.proxySuffix;
        proxyEntity.mainTaskEnum = taskEnum;
        if (taskEnum == TaskEnum.DOWNLOAD_GROUP_SUB || taskEnum == TaskEnum.DOWNLOAD_GROUP) {
          proxyEntity.subTaskEnum = EntityInfo.DOWNLOAD;
        }
        mMethodParams.put(key, proxyEntity);
      }
      proxyEntity.taskEnums.add(taskEnum);
      if (proxyEntity.methods.get(taskEnum) == null) {
        proxyEntity.methods.put(taskEnum, new HashMap<Class<? extends Annotation>, MethodInfo>());
      }

      proxyEntity.methods.get(taskEnum).put(annotationClazz, methodInfo);
      proxyEntity.keyMappings.put(methodName, getValues(taskEnum, method, annotationType));
    }
  }
}
 
Example 16
protected boolean isLegalMethod(ExecutableElement method, TypeElement typeElement) {
    WebMethod webMethod = method.getAnnotation(WebMethod.class);
    //SEI cannot have methods with @WebMethod(exclude=true)
    if (typeElement.getKind().equals(ElementKind.INTERFACE) && webMethod != null && webMethod.exclude())
        builder.processError(WebserviceapMessages.WEBSERVICEAP_INVALID_SEI_ANNOTATION_ELEMENT_EXCLUDE("exclude=true", typeElement.getQualifiedName(), method.toString()), method);
    // With https://jax-ws.dev.java.net/issues/show_bug.cgi?id=577, hasWebMethods has no effect
    if (hasWebMethods && webMethod == null) // backwards compatibility (for legacyWebMethod computation)
        return true;

    if ((webMethod != null) && webMethod.exclude()) {
        return true;
    }
    /*
    This check is not needed as Impl class is already checked that it is not abstract.
    if (typeElement instanceof TypeElement && method.getModifiers().contains(Modifier.ABSTRACT)) {  // use Kind.equals instead of instanceOf
        builder.processError(method.getPosition(), WebserviceapMessages.WEBSERVICEAP_WEBSERVICE_METHOD_IS_ABSTRACT(typeElement.getQualifiedName(), method.getSimpleName()));
        return false;
    }
    */
    TypeMirror returnType = method.getReturnType();
    if (!isLegalType(returnType)) {
        builder.processError(WebserviceapMessages.WEBSERVICEAP_METHOD_RETURN_TYPE_CANNOT_IMPLEMENT_REMOTE(typeElement.getQualifiedName(),
                method.getSimpleName(),
                returnType), method);
    }
    boolean isOneWay = method.getAnnotation(Oneway.class) != null;
    if (isOneWay && !isValidOneWayMethod(method, typeElement))
        return false;

    SOAPBinding soapBinding = method.getAnnotation(SOAPBinding.class);
    if (soapBinding != null) {
        if (soapBinding.style().equals(SOAPBinding.Style.RPC)) {
            builder.processError(WebserviceapMessages.WEBSERVICEAP_RPC_SOAPBINDING_NOT_ALLOWED_ON_METHOD(typeElement.getQualifiedName(), method.toString()), method);
        }
    }

    int paramIndex = 0;
    for (VariableElement parameter : method.getParameters()) {
        if (!isLegalParameter(parameter, method, typeElement, paramIndex++))
            return false;
    }

    if (!isDocLitWrapped() && soapStyle.equals(SOAPStyle.DOCUMENT)) {
        VariableElement outParam = getOutParameter(method);
        int inParams = getModeParameterCount(method, WebParam.Mode.IN);
        int outParams = getModeParameterCount(method, WebParam.Mode.OUT);
        if (inParams != 1) {
            builder.processError(WebserviceapMessages.WEBSERVICEAP_DOC_BARE_AND_NO_ONE_IN(typeElement.getQualifiedName(), method.toString()), method);
        }
        if (returnType.accept(NO_TYPE_VISITOR, null)) {
            if (outParam == null && !isOneWay) {
                builder.processError(WebserviceapMessages.WEBSERVICEAP_DOC_BARE_NO_OUT(typeElement.getQualifiedName(), method.toString()), method);
            }
            if (outParams != 1) {
                if (!isOneWay && outParams != 0)
                    builder.processError(WebserviceapMessages.WEBSERVICEAP_DOC_BARE_NO_RETURN_AND_NO_OUT(typeElement.getQualifiedName(), method.toString()), method);
            }
        } else {
            if (outParams > 0) {
                builder.processError(WebserviceapMessages.WEBSERVICEAP_DOC_BARE_RETURN_AND_OUT(typeElement.getQualifiedName(), method.toString()), outParam);
            }
        }
    }
    return true;
}
 
Example 17
Source Project: j2objc   File: Functionizer.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Declare any inherited constructors that aren't allowed to be accessed in Java
 * with a NS_UNAVAILABLE macro, so that clang will flag such access from native
 * code as an error.
 */
private void addDisallowedConstructors(TypeDeclaration node) {
  TypeElement typeElement = node.getTypeElement();
  TypeElement superClass = ElementUtil.getSuperclass(typeElement);
  if (ElementUtil.isPrivateInnerType(typeElement)
      || superClass == null
      // If we're not emitting constructors we don't need to disallow anything unless our
      // superclass is NSObject.
      || (!options.emitWrapperMethods()
          && typeUtil.getObjcClass(superClass) != TypeUtil.NS_OBJECT)) {
    return;
  }
  Map<String, ExecutableElement> inheritedConstructors = new HashMap<>();
  // Add super constructors that have unique parameter lists.
  for (ExecutableElement superC : ElementUtil.getConstructors(superClass)) {
    if (ElementUtil.isPrivate(superC)) {
      // Skip private super constructors since they're already unavailable.
      continue;
    }
    String selector = nameTable.getMethodSelector(superC);
    inheritedConstructors.put(selector, superC);
  }
  // Don't disallow this class' constructors if we're emitting wrapper methods.
  if (options.emitWrapperMethods()) {
    for (ExecutableElement constructor : ElementUtil.getConstructors(typeElement)) {
      inheritedConstructors.remove(nameTable.getMethodSelector(constructor));
    }
  }
  for (Map.Entry<String, ExecutableElement> entry : inheritedConstructors.entrySet()) {
    ExecutableElement oldConstructor = entry.getValue();
    GeneratedExecutableElement newConstructor =
        GeneratedExecutableElement.newConstructorWithSelector(
            entry.getKey(), typeElement, typeUtil);
    MethodDeclaration decl = new MethodDeclaration(newConstructor).setUnavailable(true);
    decl.addModifiers(Modifier.ABSTRACT);
    int count = 0;
    for (VariableElement param : oldConstructor.getParameters()) {
      VariableElement newParam = GeneratedVariableElement.newParameter(
          "arg" + count++, param.asType(), newConstructor);
      newConstructor.addParameter(newParam);
      decl.addParameter(new SingleVariableDeclaration(newParam));
    }
    addImplicitParameters(decl, ElementUtil.getDeclaringClass(oldConstructor));
    node.addBodyDeclaration(decl);
  }
}
 
Example 18
private void generateBridge(ExecutableElement fromMethod, ExecutableElement toMethod) {
  ExecutableType toErasure = (ExecutableType) types.erasure(toMethod.asType());
  String bridgeDescriptor = descriptorFactory.getDescriptor(toErasure);
  if (bridgedDescriptors.get(toMethod.getSimpleName()).contains(bridgeDescriptor)) {
    return;
  }

  innerClassesTable.addTypeReferences(toErasure);

  String[] exceptions =
      toErasure.getThrownTypes().stream()
          .map(descriptorFactory::getInternalName)
          .toArray(String[]::new);

  MethodVisitor methodVisitor =
      visitor.visitMethod(
          getBridgeAccessFlags(fromMethod),
          toMethod.getSimpleName().toString(),
          bridgeDescriptor,
          signatureFactory.getSignature(toErasure),
          exceptions);
  if (methodVisitor != null) {
    List<? extends VariableElement> fromMethodParameters = fromMethod.getParameters();
    for (int i = 0; i < fromMethodParameters.size(); i++) {
      VariableElement fromMethodParameter = fromMethodParameters.get(i);
      List<? extends AnnotationMirror> annotations =
          fromMethodParameter.getAnnotationMirrors();
      innerClassesTable.addTypeReferences(annotations);
      visitParameter(
          i,
          fromMethodParameter,
          fromMethodParameter.getSimpleName(),
          accessFlagsUtils.getAccessFlags(fromMethodParameter) | Opcodes.ACC_SYNTHETIC,
          annotations,
          methodVisitor);
    }
    innerClassesTable.addTypeReferences(fromMethod.getAnnotationMirrors());
    visitAnnotations(fromMethod, methodVisitor::visitAnnotation);
    methodVisitor.visitEnd();

    bridgedDescriptors.put(toMethod.getSimpleName(), bridgeDescriptor);
  }
}
 
Example 19
protected boolean isLegalMethod(ExecutableElement method, TypeElement typeElement) {
    WebMethod webMethod = method.getAnnotation(WebMethod.class);
    //SEI cannot have methods with @WebMethod(exclude=true)
    if (typeElement.getKind().equals(ElementKind.INTERFACE) && webMethod != null && webMethod.exclude())
        builder.processError(WebserviceapMessages.WEBSERVICEAP_INVALID_SEI_ANNOTATION_ELEMENT_EXCLUDE("exclude=true", typeElement.getQualifiedName(), method.toString()), method);
    // With https://jax-ws.dev.java.net/issues/show_bug.cgi?id=577, hasWebMethods has no effect
    if (hasWebMethods && webMethod == null) // backwards compatibility (for legacyWebMethod computation)
        return true;

    if ((webMethod != null) && webMethod.exclude()) {
        return true;
    }
    /*
    This check is not needed as Impl class is already checked that it is not abstract.
    if (typeElement instanceof TypeElement && method.getModifiers().contains(Modifier.ABSTRACT)) {  // use Kind.equals instead of instanceOf
        builder.processError(method.getPosition(), WebserviceapMessages.WEBSERVICEAP_WEBSERVICE_METHOD_IS_ABSTRACT(typeElement.getQualifiedName(), method.getSimpleName()));
        return false;
    }
    */
    TypeMirror returnType = method.getReturnType();
    if (!isLegalType(returnType)) {
        builder.processError(WebserviceapMessages.WEBSERVICEAP_METHOD_RETURN_TYPE_CANNOT_IMPLEMENT_REMOTE(typeElement.getQualifiedName(),
                method.getSimpleName(),
                returnType), method);
    }
    boolean isOneWay = method.getAnnotation(Oneway.class) != null;
    if (isOneWay && !isValidOneWayMethod(method, typeElement))
        return false;

    SOAPBinding soapBinding = method.getAnnotation(SOAPBinding.class);
    if (soapBinding != null) {
        if (soapBinding.style().equals(SOAPBinding.Style.RPC)) {
            builder.processError(WebserviceapMessages.WEBSERVICEAP_RPC_SOAPBINDING_NOT_ALLOWED_ON_METHOD(typeElement.getQualifiedName(), method.toString()), method);
        }
    }

    int paramIndex = 0;
    for (VariableElement parameter : method.getParameters()) {
        if (!isLegalParameter(parameter, method, typeElement, paramIndex++))
            return false;
    }

    if (!isDocLitWrapped() && soapStyle.equals(SOAPStyle.DOCUMENT)) {
        VariableElement outParam = getOutParameter(method);
        int inParams = getModeParameterCount(method, WebParam.Mode.IN);
        int outParams = getModeParameterCount(method, WebParam.Mode.OUT);
        if (inParams != 1) {
            builder.processError(WebserviceapMessages.WEBSERVICEAP_DOC_BARE_AND_NO_ONE_IN(typeElement.getQualifiedName(), method.toString()), method);
        }
        if (returnType.accept(NO_TYPE_VISITOR, null)) {
            if (outParam == null && !isOneWay) {
                builder.processError(WebserviceapMessages.WEBSERVICEAP_DOC_BARE_NO_OUT(typeElement.getQualifiedName(), method.toString()), method);
            }
            if (outParams != 1) {
                if (!isOneWay && outParams != 0)
                    builder.processError(WebserviceapMessages.WEBSERVICEAP_DOC_BARE_NO_RETURN_AND_NO_OUT(typeElement.getQualifiedName(), method.toString()), method);
            }
        } else {
            if (outParams > 0) {
                builder.processError(WebserviceapMessages.WEBSERVICEAP_DOC_BARE_RETURN_AND_OUT(typeElement.getQualifiedName(), method.toString()), outParam);
            }
        }
    }
    return true;
}
 
Example 20
public String methodToString(ExecutableElement method) {
    StringBuilder buf = new StringBuilder(method.getSimpleName());
    for (VariableElement param : method.getParameters())
        buf.append(';').append(param.asType());
    return buf.toString();
}