I가 ASTTransformation를 구현하고 특정 주석으로 표시된 모든 필드에 대한 getter와 setter 메소드 생성이 클래스 다음 테스트 클래스그루비 AST 의미 분석 중복 클래스 정의
class Main {
@Accessors
int counter = 5
public static void main(String[] args) {
println getCounter()
}
}
실행
@GroovyASTTransformation(phase=CompilePhase.SEMANTIC_ANALYSIS)
public class Rewriter implements ASTTransformation {
public void visit(ASTNode[] nodes, SourceUnit source) {
List fields = nodes.findAll { it instanceof FieldNode }
fields.each {
MethodNode get = createGetter(it)
MethodNode set = createSetter(it)
source.getAST().addMethod(get)
source.getAST().addMethod(set)
}
}
private MethodNode createGetter(FieldNode f) {
Parameter[] parameters = []
ClassNode[] exceptions = []
Statement state = new AstBuilder().buildFromString("return ${f.name}").get(0)
return new MethodNode("get" + f.name.capitalize(), Opcodes.ACC_PUBLIC, ClassHelper.make(f.getType().name), parameters, exceptions, state)
}
private MethodNode createSetter(FieldNode f) {
Parameter[] parameters = [
new Parameter(ClassHelper.make(f.getType().name), f.name)
]
ClassNode[] exceptions = []
Statement state = new AstBuilder().buildFromString("this.${f.name} = ${f.name}").get(0)
return new MethodNode("set" + f.name.capitalize(), Opcodes.ACC_PUBLIC, ClassHelper.VOID_TYPE, parameters, exceptions, state)
}
}
을
이org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
User\groovy\meta\src\Main.groovy: -1: Invalid duplicate class definition of class Main : The source User\groovy\meta\src\Main.groovy contains at least two definitions of the class Main.
One of the classes is an explicit generated class using the class statement, the other is a class generated from the script body based on the file name. Solutions are to change the file name or to change the class name.
@ line -1, column -1.
1 error
무엇이 발생합니다
다음과 같은 오류를 얻을 수 ? 메서드를 만들고 소스의 AST 벌금에 추가하는 것 같습니다. 그루비 이클립스를 사용하고 있습니다.