标签: annotation-processor

如何使用@Target(ElementType.TYPE_USE)处理注释?

我正在实现一个注释处理器,以确保标记有注释的元素是实现某个接口的类的实例,或者是实现某个接口的类型的使用:

@Documented
@Target(value = { ElementType.PARAMETER, ElementType.TYPE_USE })
@Retention(value = RetentionPolicy.RUNTIME)
public @interface AuditSubject {

}

public interface Auditable {
    // methods that provide data for writing a log entry...
}

public class Report implements Auditable {

}
Run Code Online (Sandbox Code Playgroud)

对于带注释的元素,必须在方法执行后(使用AOP)创建日志条目.例子:

@CreateLogEntry
public Result persist(@AuditSubject Report newReport) {
    // A log entry must be created based on the incoming 'newReport' instance.    
}

@CreateLogEntry
public UpdateResult<@AuditSubject Report> update(Report update) {
    // A log entry must be created based on the updated report, which …
Run Code Online (Sandbox Code Playgroud)

java annotations annotation-processing java-8 annotation-processor

7
推荐指数
2
解决办法
581
查看次数

如何在Eclipse中配置Java注释处理器?

我已经使用Java 8通过命令提示符编译成功运行了CLASS级别保留注释的注释处理器.

但是,当我尝试在eclipse中配置注释处理器并尝试使用"-proc:only"选项运行它时,它没有生效.

我已将包含自定义注释处理器类文件的Jar文件包含在Project Properties -> Annotation Processing -> Factory Path.我还提供了-proc:only选项Project Properties -> Annotation Processing -> Processor Options,当执行包含我的注释的类时,仍然没有调用注释处理器.

请帮我识别所需的设置或错误或通过eclipse运行注释处理器的附加步骤.

java eclipse annotations annotation-processor

6
推荐指数
2
解决办法
2万
查看次数

如何在Gradle Java-library Project构建脚本中指定buildConfigField

在我的Android项目中,我可以指定Gradle常量,如下所示:

buildConfigField 'Boolean', 'analyticsEnabled', 'false'
Run Code Online (Sandbox Code Playgroud)

并在我的Android应用程序中访问它们,如下所示: -

public boolean isAnalyticsEnabled() {
        return BuildConfig.analyticsEnabled;
}
Run Code Online (Sandbox Code Playgroud)

如何在Java库Gradle构建脚本中获得相同的功能?

更确切地说,我正在开发一个自定义注释处理器作为我的Android应用程序所依赖的纯Java项目(库).

我想在我的Java Gradle构建文件中定义可由我的注释处理器访问的常量.

如果这是可能的,那么我该如何实现呢?

java gradle annotation-processor

6
推荐指数
1
解决办法
358
查看次数

我怎样才能在android.mk中使用annotationProcessor

我只是想使用bufferknife,drag2在我的系统应用程序中,我使用命令构建了我的应用程序mm.

我已经尝试了所有可能找到的方法,但都失败了!我只Android.mk通过Google搜索找到了以下内容:

# Copyright (C) 2015 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a c

opy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS …
Run Code Online (Sandbox Code Playgroud)

android makefile android.mk annotation-processor

6
推荐指数
1
解决办法
567
查看次数

有没有办法在 Scala 中创建自定义注释并编写自定义注释处理器来验证注释?

我一直在学习注释以及注释处理器是什么。我正在查看 Java 示例,似乎有一种正确的方法可以做到这一点。但是,在 Scala 中,我没有合适的网站/文档来创建自定义注释和注释处理器。

如果在 Scala 中不可能,有没有办法在 Scala 类中使用 Java 自定义注释处理器?

有人可以指出我正确的方向吗?

java annotations scala annotation-processor

6
推荐指数
1
解决办法
589
查看次数

无法在 Kotlin 中构建注释处理器

我目前正在尝试在 Kotlin 中为 Android 编写一个注释处理器。项目结构如下:

/annotation
  /src/main/kotlin/<package>
    Annotation.kt
    AnnotationProcessor.kt
/sample
Run Code Online (Sandbox Code Playgroud)

项目/build.gradle

buildscript {
  dependencies {
    classpath 'com.android.tools.build:gradle:3.1.1'
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.2.31"
  }
}
Run Code Online (Sandbox Code Playgroud)

注释/build.gradle

apply plugin: 'kotlin'

sourceCompatibility = 1.7
targetCompatibility = 1.7

dependencies {
  implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlinVersion"
}
Run Code Online (Sandbox Code Playgroud)

示例/build.gradle

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'

android {
  ...
  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_7
    targetCompatibility JavaVersion.VERSION_1_7
  }

dependencies {
  implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlinVersion"
  implementation project(':annotation')
  kapt project(':annotation')
}
Run Code Online (Sandbox Code Playgroud)

注释.kt

@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.SOURCE)
annotation class Annotation(val comment: String = "")
Run Code Online (Sandbox Code Playgroud)

注释处理器.kt

class AnnotationProcessor : AbstractProcessor() {
  override …
Run Code Online (Sandbox Code Playgroud)

android kotlin instant-run kapt annotation-processor

5
推荐指数
1
解决办法
3237
查看次数

如何在 Android 模块之间共享使用 annotationProcessor 的依赖项?

我有一个 Android 应用程序模块 (A) 和一个 Android 库模块 (B)。(A) 和 (B) 都包含这些相同的依赖项:

dependencies {
   implementation 'com.jakewharton:butterknife:8.8.1'
   annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
}
Run Code Online (Sandbox Code Playgroud)

但是,在我的项目中,模块 (A) 依赖于模块 (B),因此我确实在堆栈溢出中搜索了有关如何实现Don't Repeat Yourself设计模式的信息,以便我仅将这些依赖项包含在模块 (B) 中,我发现很有用但我没有找到我怎么能做出这种依赖

annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1
Run Code Online (Sandbox Code Playgroud)

在这两个模块之间共享,那么我该怎么做呢?

android android-gradle-plugin annotation-processor android-module

5
推荐指数
1
解决办法
487
查看次数

如何从注释处理器中的嵌套注释读取 Class[] 值

我正在尝试使用Java注释处理工具生成一些代码,我有嵌套注释,其中父注释值是子注释的数组,子注释值是类的数组。

注释:

public @interface ParentAnnotation {
    ChildAnnotation[] value();
}
public @interface ChildAnnotation {
    Class<?>[] value();
}
Run Code Online (Sandbox Code Playgroud)

用法:

@ParentAnnotation(
{
      @ChildAnnotation({Foo.class, Bar.class}),
      @ChildAnnotation({Goo.class, Doo.class})
})
public class Sample{
}
Run Code Online (Sandbox Code Playgroud)

使用我的子类型调用value()注释Processor失败,并出现以下异常:

Error:java: error while creating source file javax.lang.model.type.MirroredTypeException: Attempt to access Class object for TypeMirror org.dominokit.samples.layout.shared.extension.LayoutEvent
    at com.sun.tools.javac.model.AnnotationProxyMaker$MirroredTypeExceptionProxy.generateException(AnnotationProxyMaker.java:308)
    at sun.reflect.annotation.AnnotationInvocationHandler.invoke(AnnotationInvocationHandler.java:84)
    at com.sun.proxy.$Proxy28.value(Unknown Source)
    at org.dominokit.domino.apt.client.processors.module.client.presenters.PresenterProxySourceWriter.generateFireActivationEvent(PresenterProxySourceWriter.java:238)
    at org.dominokit.domino.apt.client.processors.module.client.presenters.PresenterProxySourceWriter.asTypeBuilder(PresenterProxySourceWriter.java:64)
    at org.dominokit.domino.apt.client.processors.module.client.presenters.PresenterProxyProcessingStep.generateProxy(PresenterProxyProcessingStep.java:66)
    at org.dominokit.domino.apt.client.processors.module.client.presenters.PresenterProxyProcessingStep.process(PresenterProxyProcessingStep.java:53)
    at org.dominokit.domino.apt.client.processors.module.client.presenters.PresenterProxyProcessor.process(PresenterProxyProcessor.java:61)
    at com.sun.tools.javac.processing.JavacProcessingEnvironment.callProcessor(JavacProcessingEnvironment.java:794)
    at com.sun.tools.javac.processing.JavacProcessingEnvironment.discoverAndRunProcs(JavacProcessingEnvironment.java:705)
    at com.sun.tools.javac.processing.JavacProcessingEnvironment.access$1800(JavacProcessingEnvironment.java:91)
    at com.sun.tools.javac.processing.JavacProcessingEnvironment$Round.run(JavacProcessingEnvironment.java:1035)
    at com.sun.tools.javac.processing.JavacProcessingEnvironment.doProcessing(JavacProcessingEnvironment.java:1176)
    at com.sun.tools.javac.main.JavaCompiler.processAnnotations(JavaCompiler.java:1170)
    at com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:856)
    at …
Run Code Online (Sandbox Code Playgroud)

java annotation-processing annotation-processor

5
推荐指数
1
解决办法
1230
查看次数

如何使用JavaPoet生成调用超类构造函数的构造函数

我想生成一个使用 JavaPoet 扩展其他类的类。

例如我有这个类:

@MyAnnotation
public class ClassA {
  public ClassA(String paramA, int paramB) {
     // some code
  }
} 
Run Code Online (Sandbox Code Playgroud)

我想生成这样的新类:

public class Generated_ClassA extends ClassA {
  public Generated_ClassA (String paramA, int paramB) {
     super(paramA, paramB);
  }
} 
Run Code Online (Sandbox Code Playgroud)

但是,我在 JavaPoet 中没有看到任何现成的 API 来创建调用超类构造函数的构造函数。如何做到这一点以及最佳实践是什么?

java annotation-processing javapoet annotation-processor

4
推荐指数
1
解决办法
1986
查看次数

从字段元素 - 注释处理器获取包名称和参数化类型

我怎样才能获得package name,generic type并且Parametrized typetypefield element在注释处理器?

说,如果Element.asType回来java.util.List<String>,我想得到

  • 包裹名字 java.util
  • 通用类型List<E>或原始类型List(最好是原始类型)
  • 实际类型 String

有没有方法element utils,type utils

java annotations annotation-processing annotation-processor

4
推荐指数
1
解决办法
1440
查看次数

在构建发布版本时,compileReleaseJavaWithJavaC 无法响应本机模块

任务 :react-native-device-info:compileReleaseJavaWithJavac 失败

FAILURE:构建失败,出现异常。

  • 出了什么问题:任务 ':react-native-device-info:compileReleaseJavaWithJavac' 执行失败。

    java.io.FileNotFoundException: /workspace/eos-native/node_modules/react-native-device-info/android/build/intermediates/annotation_processor_list/release/annotationProcessors.json(没有这样的文件或目录)

错误是有道理的,文件不在那里,但我不知道为什么它不在那里?它是在我运行应用程序的调试版本和我的库的调试版本、react-native-device-info、react-orientation-locker 等时创建的。组装或安装发布版本我在几秒钟内收到此错误。是什么导致了 Java

android react-native annotation-processor

0
推荐指数
1
解决办法
1822
查看次数