Skip to content

Dependencies API

Simeon Iliev edited this page Oct 21, 2013 · 1 revision

Overview

The Dependencies API provides an easy way to find the dependencies of Java source and class files.

Nuff said.

Usage

Straight to the examples !

Get the dependencies of a java source file

final Set<ClassName> dependencies = Dependencies
    .ofSource(SourceFile.fromFilepath("/abs/path/to/Source.java"))
    .set();

or

final Set<ClassName> dependencies = Dependencies
    .ofSource(SourceFile.fromClasspath("rel/path/to/Source.java")) // must be present on your classpath
    .set();

or

final Set<ClassName> dependencies = Dependencies
    .ofSource(SourceFile.fromFile(aFile)) // where aFile is a java.io.File
    .set();

You can also pass a Set to either of these methods like so

final Set<ClassName> dependencies = Dependencies
    .ofSources(aSet) // aSet must contain SourceFile objects
    .set();

If you just need the dependencies of a single file this would be a simpler way to get them

final Set<ClassName> dependencies = SourceFile.fromClasspath("rel/path/to/Source.java").dependencies();

Keep in mind that the API expects that the files used for resolution are valid source files (it will validate them), if they're not it won't like you (exceptions will be thrown, lots of them).

Get the dependencies of a compiled class file

Exactly the same as the above. The only difference being that instead of calling ofSources(SourceFile source) you should call ofClasses(ClassFile clazz). Here are some examples.

final Set<ClassName> dependencies = Dependencies
    .ofClass(SourceFile.fromFilepath("/abs/path/to/Binary.class"))
    .set();

and

final Set<ClassName> dependencies = Dependencies
    .ofClasses(aSet) // aSet must contain ClassFile objects
    .set();

The simple way is also valid

final Set<ClassName> dependencies = ClassFile.fromClasspath("rel/path/to/Binary.class").dependencies();

The API will again blow up (throw an exception) if the given file is not a valid .class file (it will check!).

Clone this wiki locally