我编写了自定义注释处理器,它收集所有带注释的类,按字典顺序组织它们,并为每个带注释的类生成一个新类。
在 Intellij Idea 中,当增量构建项目时,并非项目中的所有带注释的类都会传递到我的注释处理器,而只有那些已修改/添加的类。这违反了排序逻辑。
如何确保注释处理器始终应用于每个构建上的所有注释元素?
我也发现了它的文章,但似乎它只适用于 Gradle: https: //docs.gradle.org/current/userguide/java_plugin.html#sec :incremental_annotation_processing
是否可以使注释处理器为任何构建工具聚合增量?
是否可以使这样的注释处理器隔离增量?
我的注释处理器的源代码:https ://github.com/ElegantNetworking/ElegantNetworkingAnnotationProcessor
如何制作apt流程测试源文件?
当我有以下设置时,我得到异常:
java.lang.IllegalStateException: Module adapter for class info.korzeniowski.walletplus.test.module.TestDatabaseModule could not be loaded. Please ensure that code generation was run for this module.
并且TestDatabaseModule$$ModuleAdapter在DatabaseModule$$ModuleAdapter正确生成时不生成类.
我在这做错了什么?
apt {
arguments {
androidManifestFile variant.processResources.manifestFile
resourcePackageName 'info.korzeniowski.walletplus'
}
}
dependencies {
// Dagger
def daggerVersion = '1.2.+'
apt "com.squareup.dagger:dagger-compiler:$daggerVersion"
compile "com.squareup.dagger:dagger:$daggerVersion"
}
Run Code Online (Sandbox Code Playgroud)
package info.korzeniowski.walletplus.module;
@Module(
...
)
public class DatabaseModule {
private DatabaseHelper databaseHelper;
public DatabaseModule(WalletPlus application) {
databaseHelper = OpenHelperManager.getHelper(application, DatabaseHelper.class);
}
@Provides
@Singleton
public DatabaseHelper provideDatabaseHelper() { …Run Code Online (Sandbox Code Playgroud) 我正在写一个注释处理器,我PostConstruct只对方法使用注释。假设我有一个这样的课:
public MyClass{
@PostConstruct
public void onCreate(){
}
}
Run Code Online (Sandbox Code Playgroud)
所以在我的注释处理器中,我可以获得onCreate方法:
for (Element element : roundEnv.getElementsAnnotatedWith(PostConstruct.class)) {
if (element.getKind() != ElementKind.METHOD) {
return false;
}
ExecutableElement method = (ExecutableElement) element;
}
Run Code Online (Sandbox Code Playgroud)
而且我还需要了解onCreate方法(MyClass)的类名,但ExecutableElement没有提供任何方法来做到这一点。还有其他实用程序类可以给我类名吗?
假设我有这样的课程:
public class MyClass extends Ancestor{
@MyAnnotation
void doSomething(){
}
@MyAnnotation
void doAnotherthing(String[] args){
}
}
public class Ancestor{
@MyAnnotation
void doFirst(){
}
}
Run Code Online (Sandbox Code Playgroud)
在我的注释处理器中,我有TypeElement一个MyClass.
如何@MyAnnotation在两者MyClass及其祖先中找到带注释的方法?
(我知道这是可能的,RoundEnvironment但我不想使用它)
我想在我的注释处理器中从我的andoid studio项目访问资源.
我首先尝试使用filer中的getResource方法:
FileObject fo = processingEnv.getFiler().getResource(StandardLocation.SOURCE_PATH, "", "src/main/res/layout/activity_main.xml");
Run Code Online (Sandbox Code Playgroud)
,但它总是抛出一个异常,只是将"src/main/res/layout/activity_main.xml"作为消息返回.
接下来我想我试过了
this.getClass().getResource("src/main/res/layout/activity_main.xml")
Run Code Online (Sandbox Code Playgroud)
,但这总是返回null.
我试图使用的最后一个想法是这样的:
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null);
Iterable<? extends File> locations = fm.getLocation(StandardLocation.SOURCE_PATH);
for (File file : locations) {
System.out.print(file.getAbsolutePath());
}
Run Code Online (Sandbox Code Playgroud)
,但它抛出一个空指针异常
我正在尝试遵循此堆栈溢出答案来创建我自己的自定义注释。但是我无法导入JavacAnnotationHandler. 我查看了 lombok 1.18.2 jar,它包含的文件是:lombok/javac/JavacAnnotationHandler.SCL.lombok。
我怎样才能将它导入到我的java类中?
我一起使用 mapstruct 和 lombok 并遇到了一些问题:
EntityMapper.java:10: error: Unknown property "id" in result type Entity. Did you mean "null"?
@Mapping(target = "id", ignore = true)
^
Run Code Online (Sandbox Code Playgroud)
我的 Entity 和 EntityDto 类:
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class Entity {
private int id;
private String property;
}
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
public class EntityDto {
private String property;
}
Run Code Online (Sandbox Code Playgroud)
实体映射器:
@Mapper(implementationName = "MapStruct<CLASS_NAME>")
public interface EntityMapper {
// neither of them work
@Mapping(target = "id", ignore = true)
//@Mapping(target = "id", defaultValue …Run Code Online (Sandbox Code Playgroud)