How to pass "-parameters" option to javac compiler, when building my java project with gradle?

How to pass “-parameters” option to javac compiler, when building my java project with gradle? I have asked this on stackoverflow [*], but no response. Hopefully I’ll get help here…

[*] http://stackoverflow.com/questions/29048258/store-method-parameter-names-for-some-classes-when-building-with-gradle-java8

Here’s some ideas:

http://forums.gradle.org/gradle/topics/how_can_i_provide_command_line_args_to_application_started_with_gradle_run

and

http://stackoverflow.com/a/23689696/1316011

I’ve used this (below) in build.gradle

ext {
  mainClass = "com.jthad.Stamper"
}
  task execute(type:JavaExec) {
  main = mainClass
  jvmArgs = ["-Djava.awt.headless=true"]
  if (project.hasProperty("appArgs")) {
      args Eval.me(appArgs)
  }
  classpath = sourceSets.main.runtimeClasspath
}

and run it from the command line with

gradle -PmainClass=com.jthad.Stamper -PappArgs="['multipage0.tif']" execute

Thanks, but that is not answering my question.

I would like to use the new Java 8 feature to obtain the names of method parameters, as described here: http://docs.oracle.com/javase/tutorial/reflect/member/methodparameterreflection.html

To use this new feature, I need to pass the “-parameters” option to the jacac compiler at compile time. (Please note, this is not the same as providing system properties to the JVM runtime environment).

I tried following this suggestion: http://docs.oracle.com/javase/tutorial/reflect/member/methodparameterreflection.html

but it did not work for me. Any other suggestions how to use this new Java 8 feature with gradle?

Does this SO posting help? http://stackoverflow.com/questions/25822884/passing-j-duser-language-to-gradle-not-working .

Thanks, but it did not help. I have tried adding each of the below in my build.gradle file, but non works :frowning: Any other suggestions? I’m sure there must be a way to pass options to the javac compiler with Gradle.

 compileJava {
     options.compilerArgs << '-parameters'
     options.fork = true
     options.forkOptions.executable = 'javac'  
}
     
tasks.withType(JavaCompile) {
    options.fork = true
    options.forkOptions.jvmArgs += ["-parameters"]  
}

tasks.withType(JavaCompile) {
    options.fork = true
    options.compilerArgs += ["-parameters"]  
}
1 Like

What I’m trying to do is make the below code to output:

true
str

If I enable the following option in eclipse it works:

“Store information about method parameters (usable via reflection)”

(Window -> Preferences -> Java -> Compiler) So I know it is possible. But how can I do this with Gradle, as I don’t want to depend on my IDE?

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

 public class ParamPlay {

       public static void main(String[] args) throws Exception {
             Method m = MyInterface.class.getMethod("method", String.class);
             Parameter p = m.getParameters()[0];
             System.out.println(p.isNamePresent());
             System.out.println(p.getName());
        }  

        public interface MyInterface {
             String method(String str);
        }  
  }
1 Like