how to obtain list of fields in class in java? - java

I want to retrieve list of member variables of a specified class along with other information like datatype, size, value,etc. This is possible using Reflection class. But is there any way other than Reflection class to get this information?
Thanks in advance.

The only other way I'm aware of is via source-code analysis, with tools like Spoon.

Yes introspection may help you apart from Reflection

Just use the methods provided by the field class of your class. See object Class.

reflection, this is actually easiest way to do that
parsing source code using generated compiler (antlr project has java grammar file), it's a little bit more complicated and will require additional dependencies in your project, this is suitable only in case you have source code
reading java class file and analyzing it, the most complicated. you'll have to create a java bytecode parser to read binary file. But this could be the fastest way (no additional deps LALR-k parsing, no overhead like in reflection), you'll be in control what to read, how to read, could work with compiled java code.
The question is why do you think reflection is not suitable for you?
It made much faster in java 1.5 comparing to previous java releases.

The org.springframework.util.ReflectionUtils class is actually quite the helper in these cases.

Apache commons-lang package has a very useful tool: ReflectionToStringBuilder. Here is the link to javadoc: http://commons.apache.org/lang/api-2.4/org/apache/commons/lang/builder/ReflectionToStringBuilder.html

Related

Manipulating Java classes with Java

I would like to manipulate Java classes (with java extension not .class) so that I could :
Delete all methods of a class (keeping the constructor)
Add unimplemented methods
Remove unused imports
...
Is there an API that could accomplish this ?
What I've done so far is trying to manipulate the .java files like text files (with regex,FileUtils, etc.).
Regards.
I
You could look at using the AST (Abstract Syntax Tree) tools from the Eclipse JDT project.
There is a tutorial to get you started at Vogella: Eclipse JDT - Abstract Syntax Tree (AST) and the Java Model - Tutorial
If you only want to temporarily modify the classes (i.e. within the scope of the jvm) then you could do this with reflection:
What is reflection and why is it useful?
If you're taking about permanently altering/creating source code then this is maybe best done using an IDE. Most IDE will tell you about unimplemented methods and provide auto completion to create them. They will also format the source code, remove unused imports etc.
You can use a regular expression, the question then is then what regular expression (And what other options are there!)
Regular expressions maybe aren't ideally suited to this, and for example, when it comes to another task they're not ideally suited to, such as parsing XML, people say don't do it, use an XML parser, but in this case, if you find that there is an absence of a tool built for parsing java source code, then regular expressions may be the best option.
Yes, you can use java reflection api. Please check here
Later edit: To update the class structure you can use javassist. Here you have an example.

How to create Java classes Dynamically by Java Reflection?

Reflection is used to load java class classes and manipulate them on the fly. But I have across a weird question that is asking me how to create Java classes on the fly by Reflection.I mean the classes is not compiled or have source code till we want them created. Is it really possible? Any examples?
You can take a look look at Bean Shell's eval method It lets you execute any Java code on the fly without the need to compile the code into bytecode. You can pass a string containing all the Java code for your class to it's eval method and you'll get back an instance of the dynamically created class. Let me know if you're interested in it and want me to give you an example.
Seems to me you don't need reflection, but just need to call the JavaCompiler directly from your code: JavaCompiler.
You can try ASM
ASM
or Byte code engineering library
Byte code engineering library
for manipulating, creating classes at run time
In .NET we have Reflection.Emit(C#) which can do that
Reflection.Emit
Not sure whether there is a direct java equivalent.
You can see another similar question on SO here Java equivalent of reflection.emit
Maybe Apache DynaBeans will do. You can find some tutorials on creating and manipulating them eg. here: http://www.javaranch.com/journal/2003/07/TouringTheCommonsPart1.html

lib to read java docs

I'm working on a simple parser to transform java-interfaces and value objects to C#. This is done, so a C# client to communicate with the java JMS server can be created automatically.
My parser is almost finished, I can read generic-informatins, reuse C# types, and even merger getter and setter methods to properties. The only thing i can't, because it's not possible to be done with reflections, is to read the parameter names of methods in an interfaces. I found a library (BCEL) and can read the parameter names of "real" methods, in classes, but not within an interfaces.
So my idea was, eitherway it would be cool to have the former java comments also transfered into .net, so i could use it and i could use the very same tool to get the parameter names, since they can also read them.
So my question, do you know of any library which i could use for this? I have the generated javadocs and also the sourcecode which i could use as a source for the tool.
Thank you very much
cheers
zahorak
If you have access to the source code, the easiest way would be to use a custom Javadoc doclet. This gets access to all the declarations (including parameter names), and also all comments. You can then convert it in any format you want.
If you only have the Javadoc output, I suppose most IDEs have some way of parsing it. Have a look at Eclipse or Netbeans, maybe their Javadoc parsing code is extractable.

Retro-actively add Java annotations to methods?

Is there a way to modify .class files in order to add Java annotations to certain methods? Basically I want to traverse methods of each class file in a jar file and annotate certain ones. Note that this is not at run-time while using the jar file. Rather, after I'm done I want to have modified class files with the annotations.
I do have access to the source code, so if there's an automatic source code modifier, that would work as well...
I'm assuming I'll need a tool such as Javassist or ASM. If so, which one should I use and how would I go about it?
Actually, this is a classic use case for AspectJ:
declare #method : public * BankAccount+.*(..) : #Secured(role="supervisor")
While I will grant you that direct byte code manipulation is more powerful, AspectJ is much more user-friendly, and it immediately gives you compiler warnings when you are doing something wrong.
Also, if you use Load Time Weaving, you can leave the original library jar unchanged, because the weaving happens at class-load time.
Reference:
Declare Annotation
AspectJ in Action (book)
Googling for an hour or so turned this article up which seems to completely answer my question: use ASM. To write class files using the changed bytecode, use ClassWriter.
Well, time to get to work then, I guess. :)

Parsing Java Class From Perl or Python

I want to get a .java file, recognize the first class in the file, and get information about annotations, methods and attributes from this class.
Is there any module in both languages that already does that?
I could build up a simple regexp to do it also, but I don't known how to recognize in the regexp the braces indicating the end of the class/method.
If you load the java classes inside Jython you probably could use introspection to find the information you need.
Why are you trying this in perl/python rather than just using a javadoc-aware program that can pull out the info?
I would suggest Inline::Java for accessing Java class from Perl,Once you have that interface running, you can easily access methods and attributes from the class.

Categories

Resources