// Função auxiliar para copiar o tipo da variável
 // considerando que um tipo nao pode pertencer a outra AST
 private Type getType(ITypeBinding typeBinding, AST newAST) {
   if (typeBinding.isPrimitive()) {
     return newAST.newPrimitiveType(PrimitiveType.toCode(typeBinding.getName()));
   } else if (typeBinding.isArray()) {
     return newAST.newArrayType(
         this.getType(typeBinding.getElementType(), newAST), typeBinding.getDimensions());
   } else if (typeBinding.isParameterizedType()) {
     ParameterizedType pt =
         newAST.newParameterizedType(this.getType(typeBinding.getTypeDeclaration(), newAST));
     for (ITypeBinding itb : typeBinding.getTypeArguments()) {
       pt.typeArguments().add(this.getType(itb, newAST));
     }
     return pt;
   } else if (typeBinding.isWildcardType()) {
     WildcardType wt = newAST.newWildcardType();
     wt.setBound(
         typeBinding.getBound() == null ? null : this.getType(typeBinding.getBound(), newAST),
         typeBinding.isUpperbound());
     return wt;
   } else {
     try {
       return newAST.newSimpleType(newAST.newName(typeBinding.getQualifiedName()));
     } catch (Exception e) {
       return newAST.newSimpleType(newAST.newName(typeBinding.getName()));
     }
   }
 }
 /*
  * @see ASTVisitor#visit(ParameterizedType)
  * @since 3.0
  */
 public boolean visit(ParameterizedType node) {
   node.getType().accept(this);
   this.fBuffer.append("<"); // $NON-NLS-1$
   for (Iterator it = node.typeArguments().iterator(); it.hasNext(); ) {
     Type t = (Type) it.next();
     t.accept(this);
     if (it.hasNext()) {
       this.fBuffer.append(","); // $NON-NLS-1$
     }
   }
   this.fBuffer.append(">"); // $NON-NLS-1$
   return false;
 }
  /**
   * @param type
   * @return
   */
  private String getParametrizedType(final ParameterizedType type, final Boolean innerTypes) {
    final StringBuffer sb = new StringBuffer(getFullyQualifiedNameFor(type.getType().toString()));

    if (innerTypes) {
      sb.append("<");
      for (final Object typeArg : type.typeArguments()) {
        final Type arg = (Type) typeArg;
        final String argString = getNameOfType(arg);
        sb.append(argString);
        sb.append(",");
      }
      sb.deleteCharAt(sb.length() - 1);
      sb.append(">");
    }
    return sb.toString();
  }
Example #4
0
  private static Type resolveQualifiedType(String s) {

    if (s.equals("byte")) {
      return AST.newAST(AST.JLS8).newPrimitiveType(PrimitiveType.BYTE);
    }

    if (s.equals("")) {
      return AST.newAST(AST.JLS8).newWildcardType();
    }

    // It's a type parameter
    if (s.contains("<") && s.contains(">")) {
      // TODO: resolve type parameters
      String s0 = s.substring(0, s.indexOf("<"));
      Type hd = resolveQualifiedType(s0);
      AST tAST = AST.newAST(AST.JLS8);
      ParameterizedType pt = tAST.newParameterizedType((Type) ASTNode.copySubtree(tAST, hd));

      for (String i : splitTypeArgs(s.substring(s.indexOf("<") + 1, s.lastIndexOf(">")))) {
        pt.typeArguments().add(ASTNode.copySubtree(tAST, resolveQualifiedType(i)));
      }
      return pt;
    }

    // It's an array type
    if (s.contains("[") && s.contains("]")) {
      String s0 = s.substring(0, s.indexOf("["));
      Type hd = resolveQualifiedType(s0);
      AST tast = AST.newAST(AST.JLS8);
      ArrayType pt = tast.newArrayType((Type) ASTNode.copySubtree(tast, hd));
      return pt;
    }

    if (!s.contains(".")) {
      AST ast = AST.newAST(AST.JLS8);
      if (s == null || s.equals("null")) {
        return ast.newSimpleType((Name) ASTNode.copySubtree(ast, genSimpleName("String")));
      }
      switch (s) {
        case "void":
          return AST.newAST(AST.JLS8).newPrimitiveType(PrimitiveType.VOID);
        case "int":
          return AST.newAST(AST.JLS8).newPrimitiveType(PrimitiveType.INT);
        case "char":
          return AST.newAST(AST.JLS8).newPrimitiveType(PrimitiveType.CHAR);
        case "long":
          return AST.newAST(AST.JLS8).newPrimitiveType(PrimitiveType.LONG);
        case "boolean":
          return AST.newAST(AST.JLS8).newPrimitiveType(PrimitiveType.BOOLEAN);
        case "float":
          return AST.newAST(AST.JLS8).newPrimitiveType(PrimitiveType.FLOAT);
        case "short":
          return AST.newAST(AST.JLS8).newPrimitiveType(PrimitiveType.SHORT);
        case "byte":
          return AST.newAST(AST.JLS8).newPrimitiveType(PrimitiveType.BYTE);
        case "double":
          return AST.newAST(AST.JLS8).newPrimitiveType(PrimitiveType.DOUBLE);
      }
      return ast.newSimpleType((Name) ASTNode.copySubtree(ast, genSimpleName(s)));
    } else {
      int last = s.lastIndexOf(".");
      AST ast = AST.newAST(AST.JLS8);
      String lastFrag = s.substring(last + 1);
      return ast.newQualifiedType(
          (Type) ASTNode.copySubtree(ast, resolveQualifiedType(s.substring(0, last))),
          (SimpleName) ASTNode.copySubtree(ast, genSimpleName(lastFrag)));
    }
  }