English 中文(简体)
采用说明程序创建动态方法
原标题:Create dynamic method using Annotation Processor

此时,我正试图创建一种通知程序,在附加说明的类别中形成一种称为“敲诈”的方法。 为此,我设立了以下职位:

package ro.Gabriel.AnnotationTest;

import com.google.auto.service.AutoService;

import javax.annotation.processing.*;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.TypeElement;
import javax.tools.JavaFileObject;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Set;

@AutoService(javax.annotation.processing.Processor.class)
@SupportedAnnotationTypes("ro.Gabriel.AnnotationTest.CustomGetter")
@SupportedSourceVersion(SourceVersion.RELEASE_8)
public class CustomGetterProcessor extends AbstractProcessor {

    @Override
    public synchronized void init(ProcessingEnvironment processingEnv) {
        super.init(processingEnv);
    }

    @Override
    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
        for (Element element : roundEnv.getElementsAnnotatedWith(CustomGetter.class)) {
            if (element.getKind() == ElementKind.CLASS) {
                generateGetValueMethod((TypeElement) element);
            }
        }
        return true;
    }

    private void generateGetValueMethod(TypeElement classElement) {
        String className = classElement.getSimpleName().toString();
        String packageName = processingEnv.getElementUtils().getPackageOf(classElement).getQualifiedName().toString();

        String generatedClassName = className + "Generated";
        
        String generatedCode = String.format(
                "package %s;

" +
                        "public class %s {
" +
                        "    public String getValue() {
" +
                        "        return "Hello from getValue method!";
" +
                        "    }
" +
                        "}
",
                packageName, generatedClassName
        );

        try {
            JavaFileObject fileObject = processingEnv.getFiler().createSourceFile(
                    packageName + "." + generatedClassName, classElement);
            try (PrintWriter writer = new PrintWriter(fileObject.openWriter())) {
                writer.print(generatedCode);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

这里是:

package ro.Gabriel.AnnotationTest;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.SOURCE)
@Target(ElementType.TYPE)
public @interface CustomGetter { }

In the resources folder I created the following directory: META-INF/services in the file resources/META-INF/services I created the following file: javax.annotation.processing.Processor

http://www.un.org。 我增加了我的处理器:ro.Gabriel.Annotation.CustomGettercessor

在<代码>pom.xml上,文件一增加了<代码>@Autoservice(Processor.class)的以下存放处:

        <dependency>
            <groupId>com.google.auto.service</groupId>
            <artifactId>auto-service</artifactId>
            <version>1.0.1</version>
            <scope>provided</scope>
        </dependency>

现在我设立了以下类别:

package ro.Gabriel.AnnotationTest;

@CustomGetter
public class ExampleClass {

}
ExampleClass e = new ExampleClass();
String value = e.getValue();// this method is not in the ExampleClass class even if it is annotated with @CustomGetter 

我的问题是,为什么在例类动物中不出现<代码>getValue()方法?

我认为,必须在<代码>m.xml上建立某些组合,但我不知道如何。

谁能帮助我?

Thanks!

问题回答

在您的设立之后,您最后将分两门<>ExampleClass:首先,一名阁下附加说明,第二门与制作的炉.。 这可能会造成令人不快的结果,但没有任何用处。

众所周知,通知处理过程很短,无法修改现有的源代码。

Tools/libraries like Lombok that generates methods like getters and setters on the fly will use other means to get their work done.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签