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

The following examples show how to use com.github.javaparser.ast.body.MethodDeclaration#getParameters() . 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: ParameterNameVisitor.java    From meghanada-server with GNU General Public License v3.0 6 votes vote down vote up
private void getParameterNames(MethodDeclaration methodDeclaration, boolean isInterface) {
  NodeList<Modifier> modifiers = methodDeclaration.getModifiers();
  if (isInterface || modifiers.contains(Modifier.publicModifier())) {
    String methodName = methodDeclaration.getName().getIdentifier();
    List<Parameter> parameters = methodDeclaration.getParameters();
    names.className = this.className;
    List<List<ParameterName>> parameterNames =
        names.names.computeIfAbsent(methodName, k -> new ArrayList<>(4));

    final List<ParameterName> temp = new ArrayList<>(8);
    for (final Parameter parameter : parameters) {
      ParameterName parameterName = new ParameterName();
      String type = parameter.getType().toString();
      String name = parameter.getName().getIdentifier();
      if (name.contains("[]")) {
        type = type + "[]";
        name = COMPILE.matcher(name).replaceAll(Matcher.quoteReplacement(""));
      }
      parameterName.type = type;
      parameterName.name = name;
      temp.add(parameterName);
    }
    parameterNames.add(temp);
  }
}
 
Example 2
Source File: ParserTypeUtil.java    From JRemapper with MIT License 6 votes vote down vote up
/**
 * @param md
 *            JavaParser method declaration.
 * @return Internal descriptor from declaration, or {@code null} if any parsing
 *         failures occured.
 */
private static String getMethodDesc(MethodDeclaration md) {
	StringBuilder sbDesc = new StringBuilder("(");
	// Append the method parameters for the descriptor
	NodeList<Parameter> params = md.getParameters();
	for (Parameter param : params) {
		Type pType = param.getType();
		String pDesc = getDescriptor(pType);
		if (pDesc == null)
			return null;
		sbDesc.append(pDesc);
	}
	// Append the return type for the descriptor
	Type typeRet = md.getType();
	String retDesc = getDescriptor(typeRet);
	if (retDesc == null)
		return null;
	sbDesc.append(")");
	sbDesc.append(retDesc);
	return sbDesc.toString();
}
 
Example 3
Source File: AutoDoc.java    From joyqueue with Apache License 2.0 5 votes vote down vote up
/**
 * Write the method parameters and it's doc to target api doc
 *
 * @param doc     target doc
 * @param comment parameters and method doc
 **/
private void fillParamDoc(APIDoc doc, JavadocComment comment, StringBuilder methodDesBuilder) {
    Javadoc javadoc = comment.parse();
    toString(javadoc.getDescription(), methodDesBuilder);
    doc.setDesc(methodDesBuilder.toString());
    methodDesBuilder.setLength(0);
    List<JavadocBlockTag> tags = javadoc.getBlockTags();
    if (comment.getCommentedNode().isPresent()) {
        Node node = comment.getCommentedNode().get();
        if (node instanceof MethodDeclaration) {
            MethodDeclaration method = (MethodDeclaration) node;
            for (Parameter p : method.getParameters()) {
                String type = p.getType().asString();
                String name = p.getName().asString();
                List<Param> params = doc.getParams();
                Param param = new Param();
                param.setName(name);
                param.setType(type);
                for (JavadocBlockTag t : tags) {
                    if (t.getName().isPresent()) {
                        if (name.endsWith(t.getName().get())) {
                            toString(t.getContent(), methodDesBuilder);
                            param.setComment(methodDesBuilder.toString());
                            methodDesBuilder.setLength(0);
                        }
                    }
                }
                if (params == null) {
                    params = new ArrayList<>();
                    doc.setParams(params);
                }
                params.add(param);
            }
        }
    }
}
 
Example 4
Source File: SpringParser.java    From apigcc with MIT License 5 votes vote down vote up
/**
 * 解析PathVariable
 *
 * @param n
 * @param section
 */
private void visitPathVariable(MethodDeclaration n, Section section) {
    for (Parameter parameter : n.getParameters()) {
        if (ParameterHelper.isPathVariable(parameter)) {
            section.getPathVariable().put(parameter.getNameAsString(), "");
            Row row = new Row();
            row.setKey(parameter.getNameAsString());
            row.setType(parameter.getType().toString());
            section.param(row.getKey()).ifPresent(tag -> row.setRemark(tag.getContent()));
            section.addRequestRow(row);
        }
    }
}
 
Example 5
Source File: RemoveMethodParameter.java    From Refactoring-Bot with MIT License 5 votes vote down vote up
/**
 * @param methodDeclaration
 * @param parameterName
 * @return index of the parameter with the given name, or null if not found
 */
private Integer getMethodParameterIndex(MethodDeclaration methodDeclaration, String parameterName) {
	NodeList<Parameter> parameters = methodDeclaration.getParameters();
	for (int i = 0; i < parameters.size(); i++) {
		if (parameters.get(i).getName().asString().equals(parameterName)) {
			return i;
		}
	}
	return null;
}
 
Example 6
Source File: RemoveMethodParameterTest.java    From Refactoring-Bot with MIT License 5 votes vote down vote up
/**
 * Asserts that all method calls in the body of methodWithTargetMethodCalls have
 * the same argument size as the refactoredMethod has arguments
 * 
 * @param methodWithTargetMethodCalls
 * @param refactoredMethod
 */
private void assertAllMethodCallsArgumentSizeEqualToRefactoredMethodParameterCount(
		MethodDeclaration methodWithTargetMethodCalls, MethodDeclaration refactoredMethod) {
	for (MethodCallExpr methodCall : methodWithTargetMethodCalls.getBody().get().findAll(MethodCallExpr.class)) {
		if (methodCall.getNameAsString().equals(refactoredMethod.getNameAsString())) {
			NodeList<Expression> callerMethodArguments = methodCall.getArguments();
			NodeList<Parameter> refactoredMethodParameters = refactoredMethod.getParameters();

			assertThat(callerMethodArguments).hasSameSizeAs(refactoredMethodParameters);
		}
	}
}
 
Example 7
Source File: MethodIndexer.java    From jql with MIT License 5 votes vote down vote up
public void index(MethodDeclaration methodDeclaration, int typeId) {
    List<Parameter> parameters = methodDeclaration.getParameters();
    String name = methodDeclaration.getNameAsString();
    int methodId = methodDao.save(new Method(name,
            methodDeclaration.isPublic(), methodDeclaration.isStatic(), methodDeclaration.isFinal(), methodDeclaration.isAbstract(), false, typeId));
    for (Parameter parameter : parameters) {
        parameterIndexer.index(parameter, methodId);
    }
}
 
Example 8
Source File: SpringParser.java    From apigcc with MIT License 4 votes vote down vote up
/**
 * 解析RequestHeader
 *
 * @param n
 * @param section
 */
private void visitHeaders(MethodDeclaration n, Section section) {

    List<String> headers = RequestMappingHelper.pickHeaders(n.getAnnotations());
    for (String text : headers) {
        section.addInHeader(Header.valueOf(text));
    }

    List<String> consumers = RequestMappingHelper.pickConsumers(n.getAnnotations());
    if (!consumers.isEmpty()) {
        section.addInHeader(new Header("Content-Type", String.join(",", consumers)));
    }

    List<String> produces = RequestMappingHelper.pickProduces(n.getAnnotations());
    if (!produces.isEmpty()) {
        section.addOutHeader(new Header("Content-Type", String.join(",", produces)));
    }

    for (Parameter parameter : n.getParameters()) {
        if (ParameterHelper.isRequestHeader(parameter)) {
            String key = parameter.getNameAsString();
            String defaultValue = "{value}";
            AnnotationExpr annotationExpr = parameter.getAnnotationByName(ParameterHelper.ANNOTATION_REQUEST_HEADER).get();
            Optional<Expression> valueOptional = AnnotationHelper.attr(annotationExpr, "value", "name");
            if (valueOptional.isPresent()) {
                key = String.valueOf(ExpressionHelper.getValue(valueOptional.get()));
            }
            Optional<Expression> defaultValueOptional = AnnotationHelper.attr(annotationExpr, "defaultValue");
            if (defaultValueOptional.isPresent()) {
                defaultValue = String.valueOf(ExpressionHelper.getValue(defaultValueOptional.get()));
            }
            TypeDescription description = Apigcc.getInstance().getTypeResolvers().resolve(parameter.getType());
            if (description.isAvailable()) {
                Object value = description.getValue();
                if (StringHelper.isBlank(defaultValue) && StringHelper.nonBlank(value)) {
                    defaultValue = String.valueOf(value);
                }
                section.addInHeader(new Header(key, defaultValue));
            }
        }
    }
}
 
Example 9
Source File: SpringParser.java    From apigcc with MIT License 4 votes vote down vote up
/**
 * 解析RequestParameter
 *
 * @param n
 * @param section
 */
private void visitParameter(MethodDeclaration n, Section section) {
    ObjectTypeDescription objectTypeDescription = new ObjectTypeDescription();
    for (Parameter parameter : n.getParameters()) {
        if (ParameterHelper.isRequestParam(parameter)) {
            String key = parameter.getNameAsString();

            Object defaultValue = null;
            Boolean required = null;

            Optional<AnnotationExpr> optional = parameter.getAnnotationByName(ParameterHelper.ANNOTATION_REQUEST_PARAM);
            if (optional.isPresent()) {
                Optional<Expression> valueOptional = AnnotationHelper.attr(optional.get(), "value", "name");
                if (valueOptional.isPresent()) {
                    key = String.valueOf(ExpressionHelper.getValue(valueOptional.get()));
                }
                Optional<Expression> defaultValueOptional = AnnotationHelper.attr(optional.get(), "defaultValue");
                if (defaultValueOptional.isPresent()) {
                    defaultValue = ExpressionHelper.getValue(defaultValueOptional.get());
                }
                Optional<Expression> requiredOptional = AnnotationHelper.attr(optional.get(), "required");
                if (requiredOptional.isPresent() && requiredOptional.get().isBooleanLiteralExpr()) {
                    required = requiredOptional.get().asBooleanLiteralExpr().getValue();
                }
            }

            TypeDescription description = Apigcc.getInstance().getTypeResolvers().resolve(parameter.getType());
            if (description.isAvailable()) {
                section.param(key).ifPresent(tag -> description.addRemark(tag.getContent()));
                if (required != null) {
                    description.setRequired(required);
                }
                if (description.isObject()) {
                    objectTypeDescription.merge(description.asObject());
                } else {
                    description.setKey(key);
                    if (defaultValue != null && (description.isPrimitive() || description.isString())) {
                        description.setDefaultValue(defaultValue);
                    }
                    objectTypeDescription.add(description);
                }
            }
        }
    }
    section.setParameter(objectTypeDescription.getValue());
    section.addRequestRows(objectTypeDescription.rows());
}
 
Example 10
Source File: JavaParserDocTagResolver.java    From xDoc with MIT License 4 votes vote down vote up
/**
 * 获取指定方法的所有入参类型,便于反射
 *
 * @param declaration
 * @return
 */
private static Method parseToMenthod(Class type, MethodDeclaration declaration) {
    List<Parameter> parameters = declaration.getParameters();
    parameters = parameters == null ? new ArrayList<Parameter>(0) : parameters;
    Method[] methods = type.getDeclaredMethods();
    for (Method m : methods) {
        if (!m.getName().equals(declaration.getNameAsString())) {
            continue;
        }
        if (m.getParameterTypes().length != parameters.size()) {
            continue;
        }

        boolean b = true;

        for (int j = 0; j < m.getParameterTypes().length; j++) {
            Class<?> paramClass = m.getParameterTypes()[j];
            Type ptype = parameters.get(j).getType();
            if (ptype == null) {
                continue;
            }
            String paranTypeName = ptype.toString();
            int index = paranTypeName.lastIndexOf(".");
            if (index > 0) {
                paranTypeName = paranTypeName.substring(index + 1);
            }
            //处理泛型
            index = paranTypeName.indexOf("<");
            if (index > 0) {
                paranTypeName = paranTypeName.substring(0, index);
            }

            if (!paramClass.getSimpleName().equals(paranTypeName)) {
                b = false;
                break;
            }
        }
        if (b) {
            return m;
        }
    }
    return null;
}