Store information about method parameters (usable via reflection) in intellij 13

后端 未结 1 449
野趣味
野趣味 2021-01-02 04:38

Does anyone know of an equivalent for Eclipse 4.4\'s Store information about method parameters (usable via reflection) compiler property in Intellij Idea 13 (or older, but I

相关标签:
1条回答
  • 2021-01-02 05:12
    • Go to edit Settings (Projects setting actually) Alt+Ctrl+S
    • Search for Java Compiler
    • Add the "-parameters" option in the compiler window. This should do the trick
      NOTE: I tried it with JDK 8 version 1.8.0 and it DIDN'T WORK. JDK 8 version 1.8.0_05 works.

      enter image description here See also
    • Enhancements to the Reflection API
    • Constructor/Method Parameters Metadata Available Via Reflection in JDK 8
    • Parameter name reflection

    Try this to validate. I have copied it from the article.

    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");
        }
    }
    

    And the test class:

    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) {
    
        }
    }
    
    0 讨论(0)
提交回复
热议问题