11

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 doubt it would be available)?

Edit: This link shows how to do it with maven, but I would still like to know how it's done in Idea Run Eclipse with M2 Maven build ignores "Store method parameter names" definition

Community
  • 1
  • 1
near.ethiC
  • 186
  • 1
  • 3
  • 9

1 Answers1

18

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) {

    }
}
zpontikas
  • 5,445
  • 2
  • 37
  • 41
  • I tried this, but I get: Error: Could not create the Java Virtual Machine. Error: A fatal exception has occurred. Program will exit. Unrecognized option: -g:vars – near.ethiC Jul 10 '14 at 07:34
  • U r right. give me a second and I will correct this – zpontikas Jul 10 '14 at 09:47
  • This should work now. The **-parameters** option needs to be added in compile time so that the parameter names are added in the compiled code – zpontikas Jul 10 '14 at 10:12
  • It still doesn't work for me - using this setting I get the same as without it - arg0 instead of parameter name. With this+ your previous solution, the result is the same as before: error. I will try to improvise – near.ethiC Jul 10 '14 at 12:03
  • Hi there. I was using Java 8 update 1 and it didnt work. When I updated to the last one (5 atm) it worked – zpontikas Jul 10 '14 at 12:46
  • I am using java 1.8.0_05 – near.ethiC Jul 10 '14 at 13:34
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/57095/discussion-between-zpontikas-and-near-ethic). – zpontikas Jul 10 '14 at 13:46
  • Thank you for your effort. The problem was actually with the particular project I was working on. I started it from scratch and it worked without any trouble so I went back to investigate - it had a run configuration still running (a server that was left running). Once I stopped the server, everything worked perfectly, so Thank you very much for this. It was driving me crazy. – near.ethiC Jul 11 '14 at 13:54