Debuging Java注释处理器(可能与Maven) [英] Debuging Java annotation processors (possibly with Maven)

查看:851
本文介绍了Debuging Java注释处理器(可能与Maven)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想学习如何做一个自定义注解处理器和我被困,试图调试。

I'm trying to learn how to make a custom annotation processor and I'm stuck with trying to debug it.

我已经成功地运行在调试模式下javac编译器(带有 mvnDebug干净安装)(与别人同注释处理器项目),连接到它的IntelliJ IDEA并使其停止在断点注解处理器。

I have already managed to run the javac compiler in debug mode (with mvnDebug clean install) (with someone else's project with an annotation processor), connect to it with IntelliJ IDEA and have it stop at breakpoints in the annotation processor.

如果我们有我们的项目是这样一些包,正在就像任何其他类(如无特殊配置或任何东西。)

If we have something like this in some package in our project, being just like any other class (eg. no special configuration or anything):

public class MyProcessor extends AbstractProcessor {...}

我们可以以某种方式把它嵌入的Maven构建过程注释处理器?因此,它首先被编译,那么整个项目被编译与注解处理器活跃。

Can we somehow hook it into the build process of maven as an annotation processor? So that it is compiled first, then the whole project is compiled with the annotation processor active.

另外,据我所知,注释处理器需要某种META INF文件,可以用类似的谷歌自动操作注释处理器。

Also, as far as I know, annotation processors require some kind of META INF file, which can be generated with something like google autoservices annotation processor.

所以,也许一个Maven构建过程中,我们有 自动操作先运行,再扩展AbstractProcessor类编译为一个注释处理器终于有整个项目用我们自己的注解处理器编译有效(并在调试模式下的javac编译器)。

So maybe a maven build process where we have autoservices run first, then the class extending the AbstractProcessor compiled as an annotation processor and finally have the whole project compile with our own annotation processor active (and with the javac compiler in debug mode).

推荐答案

似乎试图为无数个小时后,我设法得到它的工作。也许这只是我,谁是在做事情的工作,我无法得到一个注释处理器停止在断点如此糟糕,不管它,这里是我如何成功地做到这一点。如果这有助于ATLEAST一个人,我会很高兴我写了下来。结果

It seems that after trying for countless hours I managed to get it to work. Maybe it's just me who is so bad at making things work that I couldn't get an annotation processor to stop at breakpoints, regardless of it, here is how I managed to do it. If this helps atleast one person, I'll be glad I wrote this down.

因此​​,这里是它的recepie,ATLEAST什么为我工作,也许它的部分是多余的,我不知道,我也不在乎。

So here is the recepie for it, atleast what worked for me, maybe parts of it are redundant, I don't know and I don't care.

旁注:我做到了真正详述在某些情况下,跳过你已经知道该怎么办的部件

Sidenote: I made it really detailed in some cases, skip the parts you already know how to do.

1)首先,下载并安装Maven ,然后下载并安装的的IntelliJ IDEA (简称IDEA从这里开始)。 (如果你不知道如何使用Windows CMD,这里是一个简短的它教程,也:如何打开命令提示符

1.) First of all, download and install Maven, then download and install IntelliJ IDEA (referred to as IDEA from here on). (If you don't know how to use Windows CMD, here is a short tutorial for it, also: how to open the command prompt)

2),没有任何原型创建IDEA Maven项目。然后在制造一些些包的来源>主>的Java

2.) Create a Maven project in IDEA without any Archetype. Then create some some package in src > main > java

3。)创建延伸javax.annotation.processing.AbstractProcessor一类。

3.) Create a Class which extends javax.annotation.processing.AbstractProcessor.

4)将一些最起码的code,这只是为了工作。 (不要忘了在类声明顶部的注释!)

4.) Insert some minimal code, just to make it work. (Don't forget the Annotation at the top of the class declaration!)

@SupportedAnnotationTypes("core.Factory")
public class MyProcessor extends AbstractProcessor {
    Messager messager;

    @Override
    public void init(ProcessingEnvironment env) {
        messager = env.getMessager();
        super.init(env);
    }

    @Override
    public boolean process(Set<? extends TypeElement> annotations,       RoundEnvironment roundEnv) {
        for (TypeElement te : annotations)
            for (Element e : roundEnv.getElementsAnnotatedWith(te))
                messager.printMessage(Diagnostic.Kind.NOTE, "Printing: " +   e.toString());
        return true;
    }

    @Override
    public SourceVersion getSupportedSourceVersion() {
        return SourceVersion.latestSupported();
    }
}

5)在同一个包创建一个anotation。

5.) Create an anotation in the same package.

public @interface Factory {

}

6)。在该项目有可能是一个目录来源>测试>的Java 中,具有相同的名称如你先前创建的包中创建有另一个包。然后在其中创建类与测试(例如:MyProcessorTest)结尾的名称。然后用前面创建的新的注释类型(@Factory)注释这个类。

6.) In the project there is probably a directory src > test > java, create there another package with the same name as the package you've created earlier. Then create a Class in it with a name ending with "Test" (for example: MyProcessorTest). Then annotate this class with the new annotation type you created earlier (@Factory).

@Factory
public class MyProcessorTest {

}

7)。现在,标注处理工作,他们必须有在META-INF某些文件。为了实现这一目标,我们将使用另一种叫做注释处理器汽车维修。因此,在的pom.xml 的文件中插入它的依赖。

7.) Now, for annotation processors to work, they have to have some file in META-INF. To achieve that, we'll use another annotation processor called autoservice. So in the pom.xml file insert it's dependency.

<dependencies>
    <dependency>
        <groupId>com.google.auto.service</groupId>
        <artifactId>auto-service</artifactId>
        <version>1.0-rc2</version>
    </dependency>
</dependencies>

7.1)旁注:出于某种原因,如果我没有明确指定,则Maven项目使用Java 1.5。要强制它与Java 1.8工作,此插入到pom.xml文件。

7.1) SideNote: For some reason, if I don't specify it explicitly, the Maven project uses Java 1.5. To force it to work with Java 1.8, insert this into the pom.xml file.

<build>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.3</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
    </plugins>
</build>

8。)注释我们的处理器类的 @AutoService(Processor.class)

9)。现在,我们必须建立在IDEA远程调试器配置。要做到这一点,转到运行>编辑配置,点击左上角的绿色+按钮,选择遥控器。将它命名为类似mvnDebug,设置主机到本地主机和端口 8000 ,preSS确定,这是很好的去了。

9.) Now, we have to set up a remote debugger configuration in IDEA. To do that, go to Run > Edit Configurations, click on the green + button on the top left, select remote. Name it something like "mvnDebug", set the Host to localhost and the Port to 8000, press ok and it's good to go.

10)。在工艺方法设置一个断点在我们的处理器。

10.) Set a break point in the process method in our Processor.

11。)打开Windows命令提示符,导航到你的项目目录,其中在的pom.xml 的所在。然后键入 mvnDebug干净的安装。如果一切都已经树立正确的,应该说类似地址监听运输dt_socket:8000。

11.) Open up the Windows command prompt, navigate to your projects directory, where the pom.xml resides. Then type in mvnDebug clean install.If everything has been set up right, it should say something like "Listening for transport dt_socket at address: 8000".

12。)回到IDEA和执行我们刚刚作出的mvnDebug配置。如果一切都已经树立正确的,应该说类似连接到目标VM,地址:'本地主机:8000',运输:'插座'。

12.) Go back to IDEA and execute the mvnDebug configuration we've just made. If everything has been set up right, it should say something like "Connected to the target VM, address: 'localhost:8000', transport: 'socket'".

13。)回到命令提示符,如果没有发生preSS一些关键将其唤醒。

13.) Go back to the Command Prompt and if nothing is happening press some key to wake it up.

14)。如果一切都设置正确,IDEA将在断点处停止,暂停的javac的(Java编译器)执行。

14.) If everything was set up right, IDEA will stop at the breakpoint, suspending javac's (the Java compiler) execution.

  • Annotation processing 101 - by Hannes Dorfmann.

这篇关于Debuging Java注释处理器(可能与Maven)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆