2014-10-16 4 views

답변

2

예 JDT는이 문제에 대한 좋은 접근 방법입니다.

다음 코드는 동일한 작업을 수행하는 방법에 대한 아이디어를 제공합니다.
(참고 : 코드 테스트 또는 컴파일되지 않음)

if (javaElement instanceof IMethod) { 

    // Get the compilation unit for traversing AST 
    final ASTParser parser = ASTParser.newParser(AST.JLS4); 
    parser.setSource(javaElement.getCompilationUnit()); 
    parser.setResolveBindings(true); 

    final CompilationUnit compilationUnit = (CompilationUnit) parser.createAST(null); 

    // Record modification - to be later written with ASTRewrite 
    compilationUnit.recordModifications(); 

    // Get AST node for IMethod 
    int methodIndex = javaElement.getCompilationUnit().getSource().indexOf(javaElement.getSource()); 

    ASTNode methodASTNode = NodeFinder.perform(compilationUnit.getRoot(), methodIndex, javaElement.getSource().length()); 

    // Create the annotation 
    final NormalAnnotation newNormalAnnotation = methodASTNode.getAST().newNormalAnnotation(); 
    newNormalAnnotation.setTypeName(methodASTNode.getAST().newName("AnnotationTest")); 

    // Add logic for writing the AST here. 

} 
+1

감사합니다. 나는 [annotationUtils] (http://help.eclipse.org/luna/index.jsp?topic=%2Forg.eclipse.jst.ws.jaxws.doc.isv%2Freference%2Fapi%2Forg%2Feclipse%2Fjst%)를 사용했다. 2Fws % 2Fannotations % 2Fcore % 2Futils % 2FAnnotationUtils.html)이 간단한 apis를 사용할 수있는 일식 루나에서. –

관련 문제