C-namespace section in spring reference document - java

I am reading spring document and I couldn't understand below statement from c-namespace section in the reference document
For the rare cases where the constructor argument names are not available (usually if the bytecode was
compiled without debugging information), one can use fallback to the argument indexes
My questions are :
In what cases constructor argument is not available.
What does it mean that -byte code compiled without debugging information. Can be it checked using eclipse ?
I was checking for this over web, but could get any reference. I found Constructor injection using c:namespace but it didn't explain any thing

Constructor argument names are only available if the class is compiled with variable debugging information. When using javac, this is the -g:vars option. In Eclipse, this is Windows > Preferences > Java > Compiler > Add variable attributes to generated class files.

If the class in question was compiled by javac without the -g flag ("debug info" - see javac docs), then the compiled class bytecode will not contain the names of the constructor parameters. This means that Spring cannot use reflection to match the constructor parameter names, so you need to inject them by position (i.e. by index) instead.
It's up the build environment that generates the compiled bytecode to ensure that debug info is supplied. Once the code is compiled, there's nothing you can do to retrieve that information, short of recompiling it.
See also What does the javac debugging information option -g:vars do?

Related

Where to set java compiler options in VSCode

I hava a Java project that uses reflection tech while collecting class method info like method name, return type, parameters info of the method of some classes. In order to get the real parameter name defined for method other than just get "arg*", I should compile classes with -parameters option.
I have been finding solutions for half a day, unfortunatly still can't find where to set the compiler options in vscode.
Both Eclipse and Idea provide a way to set compiler options with "-parameters", in eclipse there is a option in "Java-> Compiler" configurations, which is "Store information about method parameters(usable via reflection)", and in Idea there is a input to add option "-parameters" manually.

Unable to use AbstractProcessor in IDEs

Motivation:
In our code we have a few places where some methods are run by their name. There are some big if-else-if blocks with each function name and call of the corresponding method (I use the term function to describe just names, for example function X01 might correspond to method SomeClass.functionX01). I've been looking into ways to improve that
Goal:
Write just methods that are annotated with some custom annotation, removing the need to update or even include if-else-if blocks in order to run specific function. Have access to any generated code if any code is generated.
What I did:
I've created first prove of concept using runtime annotations and it proved successful, but slower then if-else-if. Next attempt was with source annotation
I've followed this link for an example, however it did not seam to run in IntelliJ. What I wanted is to have - in this case PersonBuilder class generated, instead there was none. In some cases an error was raised Error:java: Bad service configuration file, or exception thrown while constructing Processor object: javax.annotation.processing.Processor: Provider BuilderProcessor not found
After some Googling and failing to find anything I've turned to book (Core Java, Volume II - Advanced Features - 9th Edition, Polish translation) and there was reccomended to run the following commands:
javac [AbstractProcessor implementation]
javac -processor [Compiled Processor] [other source files to compile]
This worked, however is unsatisfactory as it needs to happen inside IDE (NetBeans and IntelliJ to be specific) automatically during build. Code does not need to be generated on the fly, but programmer must have access to it after build (as in - be able to call methods of generated classes)
Question:
How to have and use generated code used in NetBeans and IntelliJ without the need of using external tools? Is it possible, or using reflection, runtime annotations or external tools is the only way?
Additional info (just in case):
Language level: Java 1.8
JVM versions: 12 and 13
IDEs: NetBeans and IntelliJ

Cannot view variables values from Java sources during debug

While debugging a Java program, I cannot view variables values in java source code, for example in function Integer.valueOf(). I try to add variables in Expressions or Inspect, but get 'a cannot be resolved'
This is known problem. Typically you can see the argument values as arg0, arg1 etc and sometimes member variables. If lhballoti is right and the problem is that the JDK is compiled without debug information try to compile it yourself from sources (src.zip). I believe that it is hard to compile whole JDK but I think that you can compile only interesting classes. Then push this classes into bootclasspath when you are running your java program (using -Xbootclasspath/p).
I hope this will work.
'a' is not a local variable in this method, however I don't think the JRE classes are enabled with full debugging information. It might not be possible to see local variables in the JRE.

What is annotation processing in Java?

Quoting, Sun's Official Java Tutorial
Class names, 'HelloWorldApp', are only
accepted if annotation processing is
explicitly requested
What does it mean? And how to apply it?
"Annotation Processing" is a hook into the compile process of the java compiler, to analyse the source code for user defined annotations and handle then (by producing compiler errors, compiler warning, emitting source code, byte code ...).
API reference: javax.annotation.processing (Java Platform SE 6).
From the very next line of the page that you refer to:
Class names, 'HelloWorldApp', are only accepted if annotation processing is explicitly requested
If you receive this error, you forgot to include the .java suffix when compiling the program. Remember, the command is javac HelloWorldApp.java not javac HelloWorldApp.
That is, the string that you are referring to is a possible error that you might get when trying to compile the examples. The very next line in the document, tells you how to resolve the issue.
If you want to know more about annotations, what they are, and how to use them, then I would suggest to go through the Annotations tutorial.
This error is due to incorrect use of java compilation command i.e javac with file name w/o java extension (.java)
Use proper compilation command
javac HelloWorldApp.java
Command used foe execution
java HelloWorldApp

Eclipse Java Compiler Warnings as ant task

I want the eclipse Java Compiler Warnings available as an ant task (ie without eclipse) - ideally as ant plugins - but I want the cruise control ant task to fail if an eclipse warning shows up. For the following warnings
Non-static access to static member
Method with a constructor name
Serializable class without serialVersionUID
Assignment has no effect
finally does not complete normally
Using a char array in string concatenation
Hidden catch block
Inexact type match for vararg arguments
Null pointer access
Type parameter hides another type
Method does not override package visible method
Interface method conflicts with protected 'Object' method
Local variable is never read
unused local or private member
Unchecked generic type operation
Usage of a raw type
Generic type parameter declared with a final type bound
Annotation is used as a super interface
I assume this means that the eclipse abstract syntax tree would have to be used - and an eclipse compilation unit would have to be created.
The question is:
(1) Has this been done?
(2) If it hasn't - then given a
org.eclipse.jdt.core.dom.CompilationUnit
object - how do you (ie in code examples) get the warnings out of this CompilationUnit?
(I KNOW about PMD, checkstyle etc - none of these EXACTLY match the eclipse preferences for coding style. I want an ant task that exactly matches the eclipse coding style)
What version of eclipse?
It is possible to launch the JDT compiler via ant. See:
http://help.eclipse.org/ganymede/topic/org.eclipse.jdt.doc.isv/guide/jdt_api_compile.htm
See 'Using the ant javac adapter'
Warnings and errors are attached to resources (such as files or CompilationUnits) in the Eclipse workspace. They are known as 'markers'. It may be easier to get the warnings as markers rather than via the compilation process directly.
Another avenue to look into is launching a PDE build but I think this is overkill for your requirements and such build scripts can get very difficult to maintain with time.

Categories

Resources