나는 Abstract Processor를 사용하여 사용자 정의 주석과 주석 처리기를 만들었습니다. 따라서 컴파일하기 전에 주석 처리를하고 싶습니다. 맞춤 주석과 프로세서를 Jar로 내보내고 간단한 Java 테스트 프로그램에서 사용하려고했습니다. 컴파일 시간에 자바 프로세서 클래스를 찾았지만 어쨌든 콘솔에서 기대하고있는 것은 아님을 확신합니다. 다음은 내 프로세서의 프로세스 메서드 코드입니다.사용자 정의 주석 처리기 - Java
나는 또한 의도적으로 @SupportedSourceVersion(SourceVersion.RELEASE_6)
을 사용하고 있으므로 테스트 클래스를 실행할 때 주석 처리기를 찾는다는 버전 경고가 표시됩니다.
여기 내 전체 프로젝트에 GitHub link이 있는데 여기에는 내가 만든 패키지 된 Jar도 있습니다.
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
// TODO Auto-generated method stub
processingEnv.getMessager().printMessage(Kind.NOTE, "Hello compile time message");
for (Element elem : roundEnv.getElementsAnnotatedWith(Contract.class)) {
if(elem.getKind() != ElementKind.METHOD)
{
processingEnv.getMessager().printMessage(Kind.NOTE, "Error");
return true;
}
System.out.println("Hello ... Inside for ");
Contract contract = elem.getAnnotation(Contract.class);
String message = "annotation found in " + elem.getSimpleName()
+ " with complexity " + contract.pre_cond();
processingEnv.getMessager().printMessage(Kind.NOTE, message);
}
return true; // no further processing of this annotation type
}
아래는 내가 사용하고 테스트 클래스,
import annotations.Contract;
public class AnnotationTest {
public AnnotationTest()
{
super();
}
@Contract(pre_cond = { "var > 0" })
public static void testMethod(int var)
{
System.out.println("hello1");
}
public static void main(String args[])
{
testMethod(1);
System.out.println("hello");
}
내가 javac -cp newcontractlib.jar AnnotationTest.java
명령을 사용하여 위의 테스트 클래스를 컴파일하고 난 그냥 버전 경고 및 내 처리 방법에서 어떤 메시지를 얻을 수있다.