How to access JAR archives in filesystem?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

How to access JAR archives in filesystem?

krasnyt
I have a stand-alone external library which I want to use in my custom SQ plugin. The input of this library are two collections of JAR archives. So far I've specified paths to these archives in sonar-project.properties, e.g.:

myplugin.folder1.jars=target/jars1
myplugin.folder2.jars=target/jars2


The external library then runs analysis over these collections and returns a result which contains description of errors between those JAR archives. The error also says which class in which JAR archive contains the error. Now I'd like to add an issue over this class which will be displayed in SQ instance.

I know I can add new issues through Issuable interface:

Issuable issuable = perspectives.as(Issuable.class, inputFile);

Now I don't know how to get the 2nd parameter of as method called inputFile from FileSystem. I know the full name of the class (e.g.: mypackage.MyClass) which contains the error but this class is not in the FileSystem so I can not do something like

InputFile inputFile = fs.inputFile(fs.predicates().hasAbsolutePath("mypackage/MyClass.class"));

How can I "unzip" these JAR files and add classes to the FileSystem so they can be accessed later? Is this even a good approach? I've checked sources of java-squid, package org.sonar.java.bytecode but I am pretty confused about it.

Thanks in advance,
Tom.
fge
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [sonar-dev] How to access JAR archives in filesystem?

fge
You can't do that with the pure Sonar API; however, if you use Java
7+, you have JSR 203.

Provided that you have a `File` (ewww) to the jar then you can turn
that into a `Path` (yay!) and use that to inspect the contents.

Pseudocode:

// the File object named "file" contains a reference to the zip
final Path path = file.toPath();
final Map<String, ?> env = Collections.emptyMap();
final URI uri = URI.create("jar:" + path.toUri());

try (
    final FileSystem fs = FileSystems.newFileSystem(uri, env); //
NOTE: this is NOT Sonar's FileSystem
) {
    // inspect the zip/jar contents here
}


On Tue, May 19, 2015 at 9:04 AM, krasnyt <[hidden email]> wrote:

> I have a stand-alone external library which I want to use in my custom SQ
> plugin. The input of this library are two collections of JAR archives. So
> far I've specified paths to these archives in sonar-project.properties,
> e.g.:
>
> /myplugin.folder1.jars=target/jars1
> myplugin.folder2.jars=target/jars2/
>
> The external library then runs analysis over these collections and returns a
> result which contains description of errors between those JAR archives. The
> error also says which class in which JAR archive contains the error. Now I'd
> like to add an issue over this class which will be displayed in SQ instance.
>
> I know I can add new issues through Issuable interface:
>
> /Issuable issuable = perspectives.as(Issuable.class, inputFile);/
>
> Now I don't know how to get the 2nd parameter of as method called inputFile
> from FileSystem. I know the full name of the class (e.g.: mypackage.MyClass)
> which contains the error but this class is not in the FileSystem so I can
> not do something like
>
> /InputFile inputFile =
> fs.inputFile(fs.predicates().hasAbsolutePath("mypackage/MyClass.class"));
> /
> *How can I "unzip" these JAR files and add classes to the FileSystem so they
> can be accessed later? Is this even a good approach? I've checked sources of
> java-squid, package org.sonar.java.bytecode but I am pretty confused about
> it.*
>
> Thanks in advance,
> Tom.
>
>
>
> --
> View this message in context: http://sonarqube.15.x6.nabble.com/How-to-access-JAR-archives-in-filesystem-tp5035485.html
> Sent from the SonarQube Developers mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>     http://xircles.codehaus.org/manage_email
>
>



--
Francis Galiegue, [hidden email], https://github.com/fge
JSON Schema in Java: http://json-schema-validator.herokuapp.com
Parsers in pure Java: https://github.com/fge/grappa

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [sonar-dev] How to access JAR archives in filesystem?

Dinesh Bolkensteyn-2
Hi Tom,

SonarQube issues are meant to be saved on files that are imported into SonarQube.

The binary class files are typically not imported in SonarQube, which is why you do not find them through the FileSystem API.

In your case, you are probably going to want to report issues on the .java sources files instead of on the binary .class ones.

You can have a look at the FindBugs plugin which does something similar, by depending on the SonarQube Java plugin and by using its JavaResourceLocator class:



On Tue, May 19, 2015 at 9:15 AM, Francis Galiegue <[hidden email]> wrote:
You can't do that with the pure Sonar API; however, if you use Java
7+, you have JSR 203.

Provided that you have a `File` (ewww) to the jar then you can turn
that into a `Path` (yay!) and use that to inspect the contents.

Pseudocode:

// the File object named "file" contains a reference to the zip
final Path path = file.toPath();
final Map<String, ?> env = Collections.emptyMap();
final URI uri = URI.create("jar:" + path.toUri());

try (
    final FileSystem fs = FileSystems.newFileSystem(uri, env); //
NOTE: this is NOT Sonar's FileSystem
) {
    // inspect the zip/jar contents here
}


On Tue, May 19, 2015 at 9:04 AM, krasnyt <[hidden email]> wrote:
> I have a stand-alone external library which I want to use in my custom SQ
> plugin. The input of this library are two collections of JAR archives. So
> far I've specified paths to these archives in sonar-project.properties,
> e.g.:
>
> /myplugin.folder1.jars=target/jars1
> myplugin.folder2.jars=target/jars2/
>
> The external library then runs analysis over these collections and returns a
> result which contains description of errors between those JAR archives. The
> error also says which class in which JAR archive contains the error. Now I'd
> like to add an issue over this class which will be displayed in SQ instance.
>
> I know I can add new issues through Issuable interface:
>
> /Issuable issuable = perspectives.as(Issuable.class, inputFile);/
>
> Now I don't know how to get the 2nd parameter of as method called inputFile
> from FileSystem. I know the full name of the class (e.g.: mypackage.MyClass)
> which contains the error but this class is not in the FileSystem so I can
> not do something like
>
> /InputFile inputFile =
> fs.inputFile(fs.predicates().hasAbsolutePath("mypackage/MyClass.class"));
> /
> *How can I "unzip" these JAR files and add classes to the FileSystem so they
> can be accessed later? Is this even a good approach? I've checked sources of
> java-squid, package org.sonar.java.bytecode but I am pretty confused about
> it.*
>
> Thanks in advance,
> Tom.
>
>
>
> --
> View this message in context: http://sonarqube.15.x6.nabble.com/How-to-access-JAR-archives-in-filesystem-tp5035485.html
> Sent from the SonarQube Developers mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>     http://xircles.codehaus.org/manage_email
>
>



--
Francis Galiegue, [hidden email], https://github.com/fge
JSON Schema in Java: http://json-schema-validator.herokuapp.com
Parsers in pure Java: https://github.com/fge/grappa

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [sonar-dev] How to access JAR archives in filesystem?

krasnyt
Hi, yes I've tried that but JavaResourceLocator doesn't see my jar (or I didn't add it properly) and whatever class I try to find, the result is always null.
When I debug the plugin I can see all loaded jars and classes, but my jar is not one of them.

If you know how to do it, could you post some simple pseudocode?

Thanks, Tom.
Loading...