Annotation Processing Tool add code to existing class - java

My goal is to add some source code to existing class using annotations.
First, I create an annotation and then I implemented a AbstractProcessor Class.
After that I create the javax.annotation.processing.Processor file and I generate the JAR file using the export eclipse option.
When I use my jar in other project I have the following error:
Internal compiler error: java.lang.NoClassDefFoundError: com/sun/source/util/Trees at org.xxx.preprocessor.ActionProcessor.init(ActionProcessor.java:44)
And the mentioned line is like the joined picture:
I want to use tree in order to get the compilation unit and add some code to my annotated function.
So in the first time I don't know how to fix this problem, or another way to do this.

You wrote an AP tool which is based on Sun internal code but run it inside of the Eclipse IDE. Eclipse comes with it's own Java compiler, so you don't have access to internal Java classes anymore.
I suggest to look at Project Lombok which has the exact same problem and look at their solution. The source hides behind the "Contribute" link: https://github.com/rzwitserloot/lombok
In the src/ folder is a folder eclipseAgent/ which should get you started.

Related

How to create webui workspace of adempiere customization templae using eclipse webtool using tomcat

I want to run adempiere customization with webui from eclipse using webtool and tomcat server, so i can run and debug code more efficiently for webui customziation.
These thing i have tried in adempiere trunk by using following link :-
http://www.adempiere.com/Creating_WebUI_Workspace_using_Eclipse_Webtool
,but for this i can not do in customization. As i build trunk project using utils_dev/build.xml after that it created webui.war which showing while adding project in server. As for customization given directory were not available i have copied that in customization and try to build it it's not working and showing a lot of files not found. As i think that's not correct way because many of files will not be there in customization.
[javac] Note: Some input files use or override a deprecated API.
[javac] Note: Recompile with -Xlint:deprecation for details.
[javac] Note: Some input files use unchecked or unsafe operations.
[javac] Note: Recompile with -Xlint:unchecked for details.
Secondly i have tried one another link :- https://github.com/adempiere/Customization-Template, in which it showing i have to customize ant build.xml files. As the project is available to me i referred the build.xml files also created build.xml files in client,base and zkwebui folder. But while executing that xml file it showing following error message. I think it's related issue with deprecation, but i am confused in this i will get proper output or not.Any one please suggest me how can i accomplish this things.
The given link http://www.adempiere.com/Creating_WebUI_Workspace_using_Eclipse_Webtool worked successfully for adempiere trunk project but that's not worked for customization. Can any one suggest me how can i accomplish same for customization
Second link i have followed :- https://github.com/adempiere/Customization-Template the same process i followed which i mentioned above.
Check the online book section on development environments. This may be a copy of what you were reading in the wiki. (Note, the wiki has moved to wiki.adempiere.net).
To create customizations in ZK, you'll need to create a file zkcustomization.jar and add it to the install ADEMPIERE_HOME/lib directory (not the eclipse project) before you RUN_Setup/RUN_SilentSetup to build. Any class you add to this jar will override the equivalent class in the main project.
From Eclipse, you can modify the main project directly and debug it normally but you will need to keep track of the classes you modified and add them to the jar file manually. This is a bit tedious.
The template approach is useful when you have many customized classes and need to ensure you get the jar file correct. It allows you select or add the specific java files you require and modify them while keeping the core project untouched. It suffers from the lack of a "hot-swap" feature which is so useful in the SWING interface and also that the web deployment doesn't span projects. For this reason, when debugging the customized ZK interface, the template needs to copy all the compiled classes from the main project in order to deploy the web server. There are ANT configurations/scripts to help with this but they may need some tweaking as the main project is updated. Also, the template needs access to the correct libraries so you may have to update the build configuration as you add customizations. Once you can run the adempiere zk interface from the template, you can create the zkcustomization.jar file automatically. Then be sure to add it to your deployment package.
Its a bit of a hassle getting the template configured but it works quite well once you have it set up.

Run Lint/Sonarqube anaysis on generated code

I'm working on an annotation processing library that generates java code using JavaPoet. It is composed by three modules
An annotation processor
A library that defines the annotations and some other helper code
A test app that uses both
The whole thing is built using Gradle.
I have an environment with Sonarqube running and I can analyse the code with ./gradlew sonarqube. However Sonar only scans under src/main/java for all three modules. I would like to be able to scan the build/ directory of the app module to ensure that the generated code is fine, but I cannot find any clue on how to do this.
I would expect a way to instruct Sonar to scan other directories beside the default ones. Can anybody suggest an approach for this?
Ok I managed, for anyone interested just add a new source path to sonar.sources
property "sonar.sources", "src/main/java, app/build/generated/source/apt/debug/"

IntelliJ doesn't pick up generated .class files

I have implemented an AnnotationProcessor that picks up class annotations that take a string argument. The string argument is an expression in a domain-specific language, which the annotation processor will use to compile a class file.
I create a small test project to try this out. The behaviour I see is this:
I can successfully build the project using maven
I can successfully run the project from within intellij
Despite the project RUNNING in intellij, the generated class is not recognised in the editor ("Cannot resolve class '...'"), and intelli-sense does not work, either.
I've tried to find the issue and found:
the class file that is being generated is being created in target/classes/package/name/KlassName.class (this is the location that the Filer::createClassFile method picks, I'd have expected this to go to some separate directory though).
if I'd create a java source file during annotation processing (using Filer::createSourceFile), intellij would have no problem. However, I can't do that, since the compiler is a library that really must create classes directly.
I have two guesses about what a solution might look like:
This problem might stem from intellij not looking inside target/classes when type checking in the editor window.
The class files should be generated in a separate directory instead. If so, what is the setting to fix that?
I have reproduced this issue using intellij IDEA 2016.2.1 and intellij IDEA 2017.2 EAP.

Replace Eclipse library class with my own

I'm developing an Eclipse plugin in which I want to overwrite the functionality of a method contained in a class that is in the Eclipse library.
What I've tried so far is creating an identical (same package and file names) file in my plugin source, and making the changes I want there. Then, I set the build path order such that my source is above the Plug-in Dependencies. Based on my limited understanding, this should mean that when Java looks for that class, it should use mine over the one in the library.
However, this is not working. The behavior that I want to override is not changing, and I don't see the print statements I put in my code either.
How can I "replace" a class in the Eclipse library with one of my own?
I did it once (not proud of it :-)) in the following way:
Import the plugin you wish to hack by Import->Plugin Development->Plug-ins and Fragments (Import as Projects with source folders).
Set the project to build automatically, edit the file and find its resulting class file.
Open the jar of the plugin (the one containing its class files), inject your class file instead of the original one.
If jar file is signed remove all signature information from MANIFEST.MF (and maybe other files).
I admit it's ugly but it's the best way I've found.
I ended up using the JVM JavaAgent to achieve this, by overriding the class loader and loading in my own class to replace the one in the library.
This was a useful tutorial for me: https://stackoverflow.com/a/11898653/634324

How do I import an Android library and use it in both production code and tests?

I've extracted a bunch of functionality from my app into a library. The problem is I'd like to make use of the library classes in both production code and tests. The issue is that my app, my library, and the test code are separate modules, so both the app code and the test code need to depend on the library. When I try to compile the test module, I get the following error:
UNEXPECTED TOP-LEVEL EXCEPTION:
java.lang.IllegalArgumentException: already added: (some class)
It's cryptic, but it's trying to say that I've tried to add the same class to the .dex file more than once. It's not too surprising since the test code depends on the library code and on the app at compile time, which also depends on the library at compile time. How do I set up my dependencies (or change my code) to avoid this?
NOTE: I'm using IntelliJ IDEA 10.5 CE, so I use their terminology, but I think the problem is at least conceptually IDE-agnostic.
It's a bug in IDEA, we've submitted an issue for it, please watch/vote.
Hi I feel the same jar is added multiple times using different ways.
There is two ways of putting jar in your project
Right on Project->Properties->Java Build Path->Add Jars->
Right on Project->Properties->Java Build Path->Add External jars->
first remove all jars. inside your application folder create a folder there put all the required jars.
Right on Project->Properties->Java Build Path->Add Jars-> select your project folder and select the jar file
Thanks
Deepak

Categories

Resources