/** * Returns an {@link ArrayType} that adds one dimension to the given type node. * If the given node is already an ArrayType, then a new {@link Dimension} * without annotations is inserted at the first position. * * @param type the type to be wrapped * @return the array type * @since 3.10 */ public static ArrayType newArrayType(Type type) { if (type instanceof ArrayType) { Dimension dimension= type.getAST().newDimension(); ArrayType arrayType= (ArrayType) type; arrayType.dimensions().add(0, dimension); // first dimension is outermost return arrayType; } else { return type.getAST().newArrayType(type); } }
/** * Returns an {@link ArrayType} that adds one dimension to the given type node. * If the given node is already an ArrayType, then a new {@link Dimension} * without annotations is inserted at the first position. * * @param type the type to be wrapped * @return the array type * @since 3.10 */ public static ArrayType newArrayType(Type type) { if (type instanceof ArrayType) { Dimension dimension= type.getAST().newDimension(); ArrayType arrayType= (ArrayType) type; arrayType.dimensions().add(0, dimension); // first dimension is outermost return arrayType; } else { return type.getAST().newArrayType(type); } }
@SuppressWarnings("unchecked") private static ParameterizedType parametrizeSimple(final org.eclipse.jdt.core.dom.Type original, final String... parameters) { final AST ast = original.getAST(); final ParameterizedType result = ast.newParameterizedType(original); for (final String name : parameters) { final SimpleType type = ast.newSimpleType(ast.newSimpleName(name)); result.typeArguments().add(type); } return result; }
/** * Map parameter for HashMap constructor to EnumMap constructor. HashMap(Map) -> * EnumMap(EnumMap) <br/> * other HashMap constructors -> EnumMap(Class) <br> * * @return correct parameter for EnumMap constructor */ private Expression resolveParameter(Type keyType, List<Expression> originalArgs) { if (!originalArgs.isEmpty() && instanceOf(originalArgs.get(0), "java.util.EnumMap")) { return ctx.getASTBuilder().copy(originalArgs.get(0)); } TypeLiteral keyTypeLiteral = keyType.getAST().newTypeLiteral(); keyTypeLiteral.setType(ctx.getASTBuilder().copy(keyType)); return keyTypeLiteral; } }
private static String typeName(Type type) { if (type instanceof SimpleType) { return ((SimpleType) type).getName().toString().trim(); } if (type instanceof ArrayType) { return typeName(((ArrayType) type).getElementType()); } if (type instanceof ParameterizedType) { return typeName(((ParameterizedType) type).getType()); } if (type instanceof AnnotatableType) { AnnotatableType noAnnoType = (AnnotatableType) ASTNode.copySubtree(type.getAST(), type); noAnnoType.annotations().clear(); return noAnnoType.toString().trim(); } return type.toString().trim(); }
private static Type getType(ASTNode node) { switch (node.getNodeType()) { case ASTNode.SINGLE_VARIABLE_DECLARATION: { SingleVariableDeclaration param = (SingleVariableDeclaration) node; Type type = param.getType(); int extraDim = param.getExtraDimensions(); if (extraDim == 0) { return type; } AST ast = type.getAST(); type = (Type) ASTNode.copySubtree(ast, type); type = ast.newArrayType(type, extraDim); return type; } default: { // ASTNode.METHOD_DECLARATION MethodDeclaration methodDeclaration = (MethodDeclaration) node; Type type = methodDeclaration.getReturnType2(); int extraDim = methodDeclaration.getExtraDimensions(); if (extraDim == 0) { return type; } AST ast = type.getAST(); type = (Type) ASTNode.copySubtree(ast, type); type = ast.newArrayType(type, extraDim); return type; } } }
protected static Type createPlaceholderForType(final Type type, final ICompilationUnit declaringCu, final TypeVariableMaplet[] mapping, final ASTRewrite rewrite) throws JavaModelException { Type result= null; try { final IDocument document= new Document(declaringCu.getBuffer().getContents()); final ASTRewrite rewriter= ASTRewrite.create(type.getAST()); final ITrackedNodePosition position= rewriter.track(type); type.accept(new TypeVariableMapper(rewriter, mapping)); rewriter.rewriteAST(document, declaringCu.getJavaProject().getOptions(true)).apply(document, TextEdit.NONE); result= (Type) rewrite.createStringPlaceholder(document.get(position.getStartPosition(), position.getLength()), ASTNode.SIMPLE_TYPE); } catch (MalformedTreeException exception) { JavaPlugin.log(exception); } catch (BadLocationException exception) { JavaPlugin.log(exception); } return result; }
protected static Type createPlaceholderForType(final Type type, final ICompilationUnit declaringCu, final TypeVariableMaplet[] mapping, final ASTRewrite rewrite) throws JavaModelException { Type result= null; try { final IDocument document= new Document(declaringCu.getBuffer().getContents()); final ASTRewrite rewriter= ASTRewrite.create(type.getAST()); final ITrackedNodePosition position= rewriter.track(type); type.accept(new TypeVariableMapper(rewriter, mapping)); rewriter.rewriteAST(document, declaringCu.getJavaProject().getOptions(true)).apply(document, TextEdit.NONE); result= (Type) rewrite.createStringPlaceholder(document.get(position.getStartPosition(), position.getLength()), ASTNode.SIMPLE_TYPE); } catch (MalformedTreeException exception) { JavaPlugin.log(exception); } catch (BadLocationException exception) { JavaPlugin.log(exception); } return result; }
protected static Type createPlaceholderForType(final Type type, final ICompilationUnit declaringCu, final TypeVariableMaplet[] mapping, final ASTRewrite rewrite) throws JavaModelException { Type result= null; try { final IDocument document= new Document(declaringCu.getBuffer().getContents()); final ASTRewrite rewriter= ASTRewrite.create(type.getAST()); final ITrackedNodePosition position= rewriter.track(type); type.accept(new TypeVariableMapper(rewriter, mapping)); rewriter.rewriteAST(document, declaringCu.getJavaProject().getOptions(true)).apply(document, TextEdit.NONE); result= (Type) rewrite.createStringPlaceholder(document.get(position.getStartPosition(), position.getLength()), ASTNode.SIMPLE_TYPE); } catch (MalformedTreeException exception) { JavaPlugin.log(exception); } catch (BadLocationException exception) { JavaPlugin.log(exception); } return result; }
if (type.getAST().apiLevel() >= AST.JLS10_INTERNAL && type.isVar()) { return resolveTypeBindingForName(((SimpleType) type).getName());