Хранить информацию о параметрах метода (можно использовать для отражения) в intellij 13
Кто-нибудь знает эквивалентную для Eclipse 4.4 Store информацию о параметрах метода (которую можно использовать через отражение) в свойстве компилятора Intellij Idea 13 (или более ранней версии, но я сомневаюсь, что это будет доступно)?
Изменить: Эта ссылка показывает, как сделать это с Maven, но я все же хотел бы знать, как это делается в Idea Run Eclipse со сборкой M2 Maven игнорирует определение "Имена параметров метода хранилища"
1 ответ
Решение
- Перейти к редактированию настроек (собственно настройки проектов) Alt+Ctrl+S
- Поиск компилятора Java
- Добавьте опцию "-parameters" в окне компилятора. Это должно сделать свое дело
ПРИМЕЧАНИЕ: я пробовал его с JDK 8 версии 1.8.0, и он не работал. JDK 8 версия 1.8.0_05 работает.
Смотрите также - Усовершенствования в Reflection API
- Метаданные параметров конструктора / метода, доступные через отражение в JDK 8
- Отражение имени параметра
Попробуйте это проверить. Я скопировал это из статьи.
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) {
}
}