2012-06-18 1 views
5

org.eclipse.jdt.core.dom.ITypeBinding 인스턴스를 org.eclipse.jdt.core.dom.Type 인스턴스로 변환하는 일반적인 방법을 찾고 있습니다. 이 작업을 수행하기 위해 API 호출이 있어야한다고 판단 되더라도 찾을 수 없습니다.Eclipse JDT ITypeBinding을 Type으로 변환하십시오.

특정 유형에 따라이 작업을 수동으로 수행하는 방법은 다양합니다.

ITypeBinding을 복용하고 Type을받는 일반적인 방법이 있습니까? String을 가지고 Type을 반환하는 것도 허용됩니다.

업데이트 지금까지 응답에서

, 내가 모든 특별한 경우를 처리해야합니까 나타납니다. 이렇게하는 첫 시도입니다. 나는이 그렇게 조사가 감사 완전히 정확하지 않습니다 확신 :

public static Type typeFromBinding(AST ast, ITypeBinding typeBinding) { 
    if(ast == null) 
     throw new NullPointerException("ast is null"); 
    if(typeBinding == null) 
     throw new NullPointerException("typeBinding is null"); 

    if(typeBinding.isPrimitive()) { 
     return ast.newPrimitiveType(
      PrimitiveType.toCode(typeBinding.getName())); 
    } 

    if(typeBinding.isCapture()) { 
     ITypeBinding wildCard = typeBinding.getWildcard(); 
     WildcardType capType = ast.newWildcardType(); 
     ITypeBinding bound = wildCard.getBound(); 
     if(bound != null) { 
      capType.setBound(typeFromBinding(ast, bound)), 
       wildCard.isUpperbound()); 
     } 
     return capType; 
    } 

    if(typeBinding.isArray()) { 
     Type elType = typeFromBinding(ast, typeBinding.getElementType()); 
     return ast.newArrayType(elType, typeBinding.getDimensions()); 
    } 

    if(typeBinding.isParameterizedType()) { 
     ParameterizedType type = ast.newParameterizedType(
      typeFromBinding(ast, typeBinding.getErasure())); 

     @SuppressWarnings("unchecked") 
     List<Type> newTypeArgs = type.typeArguments(); 
     for(ITypeBinding typeArg : typeBinding.getTypeArguments()) { 
      newTypeArgs.add(typeFromBinding(ast, typeArg)); 
     } 

     return type; 
    } 

    // simple or raw type 
    String qualName = typeBinding.getQualifiedName(); 
    if("".equals(qualName)) { 
     throw new IllegalArgumentException("No name for type binding."); 
    } 
    return ast.newSimpleType(ast.newName(qualName)); 
} 
+1

작은 버그가 있습니다 : _capType.setBound (typeFromBinding (AST, ** wildCard.getBound() **), wildCard.isUpperbound()); _ typeBinding은 원료 수집, wildCard.getBound 인 경우()는 null를 돌려 주어, 메소드는 유효한 상황의 형태를 생성하지 않습니다. 바운드를 확인하고 코드를 수정하도록 설정하지 않아도됩니다. – taksan

답변

2

나는 더 나은 대체 솔루션을 찾았습니다. 컴파일 단위의 가져 오기 문을 관리하는 org.eclipse.jdt.core.dom.rewrite.ImportRewrite을 사용할 수 있습니다. Type addImport (ITypeBinding, AST)를 사용하면 기존의 가져 오기를 고려하여 필요에 따라 새로운 유형 노드를 추가 할 수 있습니다.

+0

내가 이것에 영향을 받았기 때문에 : ImportRewrite는 "AST는 ICompilationUnit에서 만들어 져야합니다. 즉 AST를 만들 때 ASTParser.setSource (ICompilationUnit)가 사용되었음을 의미합니다"라는 요구 사항이 있습니다. – sevenforce

0

난 당신이 '유형'객체 또는 왜 당신이 그것을 필요로 할 수있는 것을 확실하지 않다. 당신은 당신이 새로운 ASTNode 필요하면 다음 ASTRewrite을 통해이 소스를 수정하는 데 사용되는, 그러나 http://wiki.eclipse.org/JDT/FAQ#From_an_IBinding_to_its_declaring_ASTNode

  • 을 사용할 수 있습니다 - 당신은 기존의 유형 노드 즉 TypeBinding에 대한 선언 노드가 말을해야하는 경우

    • 직접 손으로 직접 만들어야하고 모든 경우를 다뤄야합니다. 모든 상황에서 '유형'의 모든 하위 유형을 처리 할 필요는 없습니다 (예 : UnionType (Java 7)은 catch 블록에만 관련이 있습니다.

  • +0

    'ASTRewrite'에 필요합니다. 표현식의 형식을 변경하지 않고 인라인 할당을 통해 새 변수에 표현식 결과를 캡처하려고합니다. –

    +0

    사례를 처리 할 수있는 잠재적 인 코드로 질문을 업데이트했습니다. 제가 놓친 사례를 제안 해 주시겠습니까? –

    +0

    빠른 시선에서 나에게 괜찮아 보인다. –