我正在实现一个注释处理器,以确保标记有注释的元素是实现某个接口的类的实例,或者是实现某个接口的类型的使用:
@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
我已经使用Java 8通过命令提示符编译成功运行了CLASS级别保留注释的注释处理器.
但是,当我尝试在eclipse中配置注释处理器并尝试使用"-proc:only"选项运行它时,它没有生效.
我已将包含自定义注释处理器类文件的Jar文件包含在Project Properties -> Annotation Processing -> Factory Path.我还提供了-proc:only选项Project Properties -> Annotation Processing -> Processor Options,当执行包含我的注释的类时,仍然没有调用注释处理器.
请帮我识别所需的设置或错误或通过eclipse运行注释处理器的附加步骤.
在我的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构建文件中定义可由我的注释处理器访问的常量.
如果这是可能的,那么我该如何实现呢?
我只是想使用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) 我一直在学习注释以及注释处理器是什么。我正在查看 Java 示例,似乎有一种正确的方法可以做到这一点。但是,在 Scala 中,我没有合适的网站/文档来创建自定义注释和注释处理器。
如果在 Scala 中不可能,有没有办法在 Scala 类中使用 Java 自定义注释处理器?
有人可以指出我正确的方向吗?
我目前正在尝试在 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 应用程序模块 (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
我正在尝试使用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) 我想生成一个使用 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 来创建调用超类构造函数的构造函数。如何做到这一点以及最佳实践是什么?
我怎样才能获得package name,generic type并且Parametrized type从type从field element在注释处理器?
说,如果Element.asType回来java.util.List<String>,我想得到
java.utilList<E>或原始类型List(最好是原始类型)String有没有方法element utils,type utils?
任务 :react-native-device-info:compileReleaseJavaWithJavac 失败
FAILURE:构建失败,出现异常。
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
java ×7
android ×4
annotations ×4
android.mk ×1
eclipse ×1
gradle ×1
instant-run ×1
java-8 ×1
javapoet ×1
kapt ×1
kotlin ×1
makefile ×1
react-native ×1
scala ×1