Create custom rule java with for sonar analyse - java

i always used xpath to create custom rule in sonar, but now xpath is removed from plugin java, i need help for my first custom rule in java :
If there is a #Stateless or #Stateful annotation on a class, there should be a #Interceptors annotation. If it's not the case, it should be a Critical issue.
i followed this step :
Create a standard SonarQube plugin.
Put a dependency on the jar of the language plugin for which you are writing custom rules.
Create as many custom rules as required
Generate the SonarQube plugin (jar file).
Place this jar file in the SONARQUBE_HOME/extensions/plugins directory.
Restart the SonarQube server.
and i'm able to follow all the steps but to create custom rule i need to know what i can put on the java class to define my rule before generate the sonarqube plugin.
can anyone help me, thanks in advance!
Regards,
Youssef

As far as I understand, you need to get the annotations of a class. We've done this for #VisibleForTesting in a custom plugin.
Look in https://github.com/arxes-tolina/sonar-plugins/tree/master/src/main/java/de/tolina/sonar/plugins/vft/checks package, especially HasVisibleForTesting.java and IsVisibleForTesting.java.

Related

AOP in Kotlin to be used in Java class

I´m implementing a library in Kotlin that it will be used from Java.
It would be possible to create an annotation and AOP code in Kotlin, and then being used from Java.+
If that possible a documentation or example it would be awesome. I cannot find anything with that interoperability.
Regards.
As for the annotation, there should not be any problem implementing it in Kotlin.
As for the aspect, when compiled with the Kotlin compiler it shall end up being a regular JVM class with all the necessary #AspectJ annotations, but it will not be an aspect because it was not compiled by the AspectJ compiler which as of today only understands Java source code.
If you use such an "unfinished" aspect via LTW (load-time weaving), the AspectJ weaver can finish it into an aspect while it is being loaded, so that scenario should work.
In the case of trying to use the unfinished aspect for compile-time or binary weaving against Java (or Kotlin) target classes, an intermediate step to finish the unfinished aspect using the AspectJ compiler would be necessary, but I never tried that and do not know if it is even possible. It would be interesting to try. I do not speak Kotlin, but maybe it would be fun to try if you have something for me to start with like a sample project, ideally built with Maven. If there is any way to pull this off, we would end up using the AspectJ Maven plugin for just like #dreamcrash suggested, just in a different way.
BTW, I need more information from you about what you mean by "use from Java". Please elaborate.
Update: I just gave it a quick try:
Annotation + aspect both in Kotlin
Compile with Kotlin compiler into my-aspect.jar, aspectjrt.jar on the class path
Package Kotlin classes into a JAR
Java class using annotation from Kotlin aspect JAR
Compile with ajc, my-aspect.jar on the inpath and aspectjrt.jar and kotlin-stdlib.jar on the class path.
Result is e.g. in bin directory, both the Java class and the two Kotlin classes from the JAR, but the aspect this time finished by ajc (bigger class file than original).
Run Java program with bin folder, aspectjrt.jar and kotlin-stdlib.jar on the class path.
Works nicely, aspect kicks in.
The only step remaining is to "mavenise" this in connection with AspectJ Maven plugin, which should be fairly easy. But the answer to your question is: Yes, you can implement an aspect in Kotlin and use it combined with Java target classes. The downside of course is that you need the Kotlin standard library on the class path, not just the AspectJ runtime as usual.
Update 2: I created a Maven multi-module playground project for myself and for your convenience. Just clone my GitHub repository, then build and run according to the read-me file.

How to restrict access to package in Eclipse Java Project

Is there a way (built-in or via plugin) to restrict access to a Java package from other packages in Eclipse?
Example:
Packages (in same project):
com.my.project.common
com.my.project.serviceV1
com.my.project.serviceV2
Now I want to restrict access to serviceV1 and serviceV2 from common. But of course those services should be allowed to access the common package
Edit: If all you are looking for is simple package level protection that can be achieved using the private/protected/public/default package scopes, then I agree with the comment from #AlexisPigeon - that's the simplest route. Otherwise, I would consider the jdepend solution I proposed below.
No, this is not possible. I recommend looking at JDepend. It can produce a report of the package dependencies. You could write a script to parse the output and then fail your build (there are jdepend plugins for maven, ant and gradle) if one of your dependency constraints are violated.

Custom Annotation Processing, Generating Documentation, and Maven 3

My goal is to "read" the Java classes of a certain package, then to process the Javadoc and annotations (preferably at the same time) on the classes within that package and on the methods within those classes. Solution must be implemented in Java 6 or 7, build tool is Maven 3. We're currently using the maven-compiler-plugin, version 2.5.1, but I can probably upgrade that further if available/necessary.
As I understand it, the purpose of the javax.annotation.processing classes are to do this kind of thing, and I believe Java code along the lines of this other SO answer should work for my purposes, but the practical details of how to get it actually running are still a little fuzzy.
All that being said, here's what I think I need:
Java code to pick out the annotations and Javadoc items that I want, and then convert those items into the data model needed to create my custom documentation.
Java code to then write this data model out to a docs file or directory of files
Maven 3 configuration to:
Run the annotation processor once at a good time
Include the generated docs directory in the outputted war file
The Maven bits trip me up more than the Java code, so if you're only going to answer half, that's the half that'll get my check mark. Also, my preference would be to not put this annotation processor into a Maven repository as a separate plugin since it will be very tightly coupled with some custom annotations we're using.
Here's a brief listing of questions that I found as related from which I could not synthesize my own answer, though:
Writing an annotation processor for maven-processor-plugin
Maven annotation processing with maven-compiler-plugin
How to configure the Annotation Processing API without external Jar using Maven?
Can the Pluggable Annotation Processor API retrieve source code comments?
I'm not sure if you can access the Javadoc from annotation processors. Consider to use the Doclet API:
http://docs.oracle.com/javase/7/docs/technotes/guides/javadoc/doclet/overview.html
Edit:
Here is an annotation processor I wrote. Maybe it can serve as a sample:
http://softsmithy.hg.sourceforge.net/hgweb/softsmithy/lib/main-golden/file/ae786193023d/softsmithy-lib-core/src/main/java/org/softsmithy/lib/util/impl/ServiceProviderAnnotationProcessor.java
http://softsmithy.hg.sourceforge.net/hgweb/softsmithy/lib/main-golden/file/ae786193023d/softsmithy-lib-core/pom.xml
http://softsmithy.hg.sourceforge.net/hgweb/softsmithy/softsmithy-parent/main-golden/file/9397853ba514/pom.xml
For Maven: as far as I can remember you have to pass -proc:none as compiler argument (compiler plugin) in the project that contains the annotation processor. In that project also add the following resource file: META-INF/services/javax.annotation.processing.Processor and add the fully qualified name of your annotation processor in the first line.
I usually package the annotation, the annotation processor and this "service registry file" in the same project A. Then in any other project B where I'm using the annotation and thus have a dependency on this project A, the annotation processor is picked up automatically.
I'm using the Maven Compiler Plugin v2.3.2 and Java SE 7.

Can Java annotation do this?

I'm looking into a solution that displays the subversion revision number and last modification date in my application (written in GWT, therefore reflection is not available). Encode the revision in subversion keyword doesn't work as it applies only to the current file. Is there a better solution using annotation? (e.g., a separate class that's executed during the compile time, grab the latest revision # on the whole project and inject the revision and last modification date to the source code)
I kn
#SvnRevision("$Id$")
public class Foo {
}
Then your classes are all annotated with their version. You need to make sure the annotation is defined as having runtime retention so it can be queried at runtime.
EDIT
OK, since SVN doesn't have that feature, I'd write a Maven plugin to emulate it. Maven has access to the SCM information for every file so during the build phase you could have it do the same keyword expansion.
Annotations are not really designed for this. It's easiest to do it as part of the build.
Using Ant you can generate a file that contains the version information, include it in your application's JAR, load it as a resource on the server, and serve it out to the browser-side code by RPC. Ant can also do string replacement in files as it copies them, which you can use to include the version number in your application's HTML files (no need for RPC then).
No idea about Maven, but I would be very surprised if it could not do the same kind of thing.
Subversion still has the $Id$ feature, but it needs to be enabled explicitly using the svn:keywords property on the files (set it to 'Id').
See also: http://svnbook.red-bean.com/en/1.5/svn.advanced.props.special.keywords.html
So the idea of Jherico above with #SvnRevision would work.

How do you figure out with Eclipse which JARs depend on which one?

I've trying to use Eclipse JDT AST parsing classes. After including the initial JAR, and sorting out a couple more dependencies, it is with 7+ JARs and I still having NoClassDefFoundError exceptions. This situation arises whenever I'm trying to test libraries with little or no documentation. Trial and error seems a very dumb (and annoying) approach to solve this problem.
Is there a way to automatically sort this out using Eclipse?
Update: Later I found that adding all the JARs you have, and using Ctrl-T (to view/locate types), lets you manually locate the JAR. That was the solution that Google provided so far. Is there a better way?
If you refer to this SO question Finding unused jars used in an eclipse project, you also have:
ClassPathHelper, which can quickly focus on unresolved classes:
It automatically identifies orphan jars, blocked (obscured) classes, and much more.
The only limit is dependencies that are not defined in classes, e.g. in dependency injection framework configuration files.
I have found setting up a workspace exclusively for browsing the eclipse source code incredibly useful. In this manner, you can use PDE tools like the Plug-in Spy, bundle dependency analysis, browsing the documentation, etc much like you would your own plugin projects. I found this article at Vogella a very useful guide.
If you know which bundle your desired class is you can generate the transitive closure of dependencies by creating a new OSGi launch configuration, with just the single bundle selected. By hitting the Add Required button, you can see all bundles necessary to use the one you're interested in.
Edit:
From your question it wasn't clear as to the environment you want to run the compiler in. If you're interested in an embeddable Java compiler to be run outside of an OSGi environment, may I suggest Janino.
You could use a dependency analyzer like:
JarAnalyzer
This will parse a directory full of Jars and give you an XML output dependency map, for which there are several tools for displaying in either graphical or text form.

Categories

Resources