How to check package existence from inside annotation processor when annotating?


Bartek Lipinski :

I'm building an annotation processor for Android (let's call it that for future explanation TestProcessor).

The plan is for the processor to operate in two modes:

  • Mode 1: code generationA

  • Mode 2: generate code andAB


Mode 2Should only be selected if there are additional (optional) packages in the environment for which the processor generates code.

Is there a way to find out if an optional package exists in the annotation processor's methods?process


Edit 1:

Some small notes on "(optional) packaging". By an add-on package, I mean a Java package that may ( but does not necessarily) exist in the project (i.e. use the processor). Optional packages can, for example, represent content as external libraries. External libraries can but do not have to be included in the project.

Let me give a small example:

  • Suppose we have a (single module) project Sample.
  • Sampleis using mine TestProcessor.
  • Let's Square's Picassotake an external library as an example
  • Operates and generates code sum if SampleHAS Square's Picassois one of its dependencies . In other words: if you can use the class in (in ) without problems , the processor should run in .TestProcessorMode 2ABcom.square.picassoSampleMode 2
  • If not Square's Picassoin Sample'sdependencies, TestProcessorwork in and generate unique code .Mode 1A

Edit 2:

I thought of two solutions to this problem:

  1. Use two types of annotations for both (eg: , )processor Modes@DoStuffModeOne@DoStuffModeTwo
  2. Use a second type of annotation (that should be used e.g. in Applicationthe trigger's use) (e.g.: )Mode 2@TriggerModeTwo

As far as I'm concerned, the second workaround is preferable, but much worse than a situation where it's possible to decide which one to choose .TestProcessorMode

JBirdVegas :

You can check a ButterKnifeclass in your application's classpath . Then execute logic based on the check result.

Class butterKnifeClass = null;
try {
    butterKnifeClass = Class.forName("butterknife.ButterKnife");
} catch (ClassNotFoundException e) {
    // no butterknife present
}
if (butterKnifeClass != null) {
    // blah... stuff with butterknife
}

Related


Check class hierarchy when compiling on annotation processor

Chilo I am writing an annotation processor to perform the following checks at compile time: has an interfaceE There is an @Applyannotation for annotating methods. Annotated methods @Applyshould be called applyand take only one parameter of the implementing cla

Check class hierarchy when compiling on annotation processor

Chilo I am writing an annotation processor to perform the following checks at compile time: has an interfaceE There is an @Applyannotation for annotating methods. Annotated methods @Applyshould be called applyand take only one parameter of the implementing cla

How to check method parameter type in annotation processor?

Tovey This would be easy if pure reflection was used, but the annotation processor world seems to be different. How do I TypeMirrorreturn from getParameterTypes()to String.class? In my annotation processor, I want to check if the currently accessed method has

How to check method parameter type in annotation processor?

Tovey This would be easy if pure reflection was used, but the annotation processor world seems to be different. How do I TypeMirrorreturn from getParameterTypes()to String.class? In my annotation processor, I want to check if the currently accessed method has

How to check method parameter type in annotation processor?

Tovey This would be easy if pure reflection was used, but the annotation processor world seems to be different. How do I TypeMirrorreturn from getParameterTypes()to String.class? In my annotation processor, I want to check if the currently accessed method has

How to check method parameter type in annotation processor?

Tovey This would be easy if pure reflection was used, but the annotation processor world seems to be different. How do I TypeMirrorreturn from getParameterTypes()to String.class? In my annotation processor, I want to check if the currently accessed method has

How to check method parameter type in annotation processor?

Tovey This would be easy if pure reflection was used, but the annotation processor world seems to be different. How do I TypeMirrorreturn from getParameterTypes()to String.class? In my annotation processor, I want to check if the currently accessed method has

Check if there is no superclass in annotation processor

G_H: TypeElementWhen obtained in an annotation processor, TypeMirrora method can be used to request its superclass (or more specifically, its superclass ) getSuperClass(). According to the JavaDoc , a type does not explicitly extend anything (in other words, O

Check if there is no superclass in annotation processor

G_H: TypeElementWhen obtained in an annotation processor, TypeMirrora method can be used to request its superclass (or more specifically, its superclass ) getSuperClass(). According to the JavaDoc , a type does not explicitly extend anything (in other words, O

Check if there is no superclass in annotation processor

G_H: TypeElementWhen obtained in an annotation processor, TypeMirrora method can be used to request its superclass (or more specifically, its superclass ) getSuperClass(). According to the JavaDoc , a type does not explicitly extend anything (in other words, O

How to exclude annotation processor dependencies from ProGuard

Trevor A: Getting a ProGuard warning when trying to create a release version of my Android project, preventing the build from completing successfully. Warning: Library class com.google.googlejavaformat.java.filer.FormattingJavaFileObject$1$1 extends or impleme

How to call annotation processor from Gradle plugin

Auld Issa I'm currently working on a Gradle custon plugin that should analyze a specific configuration in each subproject of my root project and then generate some kotlin source code in the build directory. I can't find a way to call my annotation processor fr

How to exclude annotation processor dependencies from ProGuard

Trevor A: Getting a ProGuard warning when trying to create a release version of my Android project, preventing the build from completing successfully. Warning: Library class com.google.googlejavaformat.java.filer.FormattingJavaFileObject$1$1 extends or impleme

How to call annotation processor from Gradle plugin

Auld Issa I'm currently working on a Gradle custon plugin that should analyze a specific configuration in each subproject of my root project and then generate some kotlin source code in the build directory. I can't find a way to call my annotation processor fr

How to call annotation processor from Gradle plugin

Auld Issa I'm currently working on a Gradle custon plugin that should analyze a specific configuration in each subproject of my root project and then generate some kotlin source code in the build directory. I can't find a way to call my annotation processor fr

How to call annotation processor from Gradle plugin

Auld Issa I'm currently working on a Gradle custon plugin that should analyze a specific configuration in each subproject of my root project and then generate some kotlin source code in the build directory. I can't find a way to call my annotation processor fr

How to call annotation processor from Gradle plugin

Auld Issa I'm currently working on a Gradle custon plugin that should analyze a specific configuration in each subproject of my root project and then generate some kotlin source code in the build directory. I can't find a way to call my annotation processor fr