gpt4 book ai didi

java - 在 intellij 13 中存储有关方法参数的信息(可通过反射使用)

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:11:04 24 4
gpt4 key购买 nike

有谁知道 Intellij Idea 13(或更早版本,但我怀疑它是否可用)中 Eclipse 4.4 的有关方法参数的存储信息(可通过反射使用)编译器属性的等效项?

编辑:这个链接显示了如何用 maven 来做,但我仍然想知道它是如何在 Idea 中完成的 Run Eclipse with M2 Maven build ignores "Store method parameter names" definition

最佳答案

试试这个来验证。我从文章中复制了它。

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Parameter;

import static java.lang.System.out;

/**
* Uses JDK 8 Parameter class to demonstrate metadata related to the parameters
* of the methods and constructors of the provided class (includes private,
* protected, and public methods, but does not include methods inherited from
* parent classes; those classes should be individually submitted).
*
* @author Dustin
*/
public class Main {
private static void displayParametersMetadata(final String className) {
try {
final Class clazz = Class.forName(className);

// Get all class's declared methods (does not get inherited methods)
final Method[] declaredMethods = clazz.getDeclaredMethods();
for (final Method method : declaredMethods) {
writeHeader(
"Method " + method.toGenericString()
+ " has " + method.getParameterCount() + " Parameters:");
int parameterCount = 0;
final Parameter[] parameters = method.getParameters();
for (final Parameter parameter : parameters) {
out.println(
"\targ" + parameterCount++ + ": "
+ (parameter.isNamePresent() ? parameter.getName() : "Parameter Name not provided,")
+ (isParameterFinal(parameter) ? " IS " : " is NOT ")
+ "final, type " + parameter.getType().getCanonicalName()
+ ", and parameterized type of " + parameter.getParameterizedType()
+ " and " + (parameter.isVarArgs() ? "IS " : "is NOT ")
+ "variable.");
}
}
} catch (ClassNotFoundException cnfEx) {
out.println("Unable to find class " + className);
}
}

private static void writeHeader(final String headerText) {
out.println("\n==========================================================");
out.println("= " + headerText);
out.println("==========================================================");
}

/**
* Indicate whether provided Parameter is final.
*
* @param parameter Parameter to be tested for 'final' modifier.
* @return {@code true} if provided Parameter is 'final'.
*/
private static boolean isParameterFinal(final Parameter parameter) {
return Modifier.isFinal(parameter.getModifiers());
}

public static void main(final String[] arguments) {
displayParametersMetadata("TestProperties");
}
}

和测试类:

import java.util.List;

/**
* Created with IntelliJ IDEA.
* User: zpontikas
* Date: 7/10/2014
* Time: 17:02
*/
public class TestProperties {

public String getSomeStringValue(String thisIsAProperty) {
return thisIsAProperty;
}

public static List<Integer> getSomeLists(final List<Integer> anotherProName) {
return anotherProName;
}

public void manyProperties(final String onePropertyString,final int anotherIntProperty, final boolean thisIsBool) {

}
}

关于java - 在 intellij 13 中存储有关方法参数的信息(可通过反射使用),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24648640/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com