private FieldDeclaration createNewFieldDeclaration(ASTRewrite rewrite) { AST ast = getAST(); VariableDeclarationFragment fragment = ast.newVariableDeclarationFragment(); SimpleName variableName = ast.newSimpleName(fFieldName); fragment.setName(variableName); addLinkedName(rewrite, variableName, false); List<Dimension> extraDimensions = DimensionRewrite.copyDimensions(fTempDeclarationNode.extraDimensions(), rewrite); fragment.extraDimensions().addAll(extraDimensions); if (fInitializeIn == INITIALIZE_IN_FIELD && tempHasInitializer()) { Expression initializer = (Expression) rewrite.createCopyTarget(getTempInitializer()); fragment.setInitializer(initializer); } FieldDeclaration fieldDeclaration = ast.newFieldDeclaration(fragment); VariableDeclarationStatement vds = getTempDeclarationStatement(); Type type = (Type) rewrite.createCopyTarget(vds.getType()); fieldDeclaration.setType(type); fieldDeclaration.modifiers().addAll(ASTNodeFactory.newModifiers(ast, getModifiers())); return fieldDeclaration; }
/** * Creates a {@link ASTRewrite#createCopyTarget(ASTNode) copy} of <code>type</code> * and adds <code>extraDimensions</code> to it. * * @param type the type to copy * @param extraDimensions the dimensions to add * @param rewrite the ASTRewrite with which to create new nodes * @return the copy target with added dimensions */ public static Type copyTypeAndAddDimensions(Type type, List<Dimension> extraDimensions, ASTRewrite rewrite) { AST ast= rewrite.getAST(); if (extraDimensions.isEmpty()) { return (Type) rewrite.createCopyTarget(type); } ArrayType result; if (type instanceof ArrayType) { ArrayType arrayType= (ArrayType) type; Type varElementType= (Type) rewrite.createCopyTarget(arrayType.getElementType()); result= ast.newArrayType(varElementType, 0); result.dimensions().addAll(copyDimensions(extraDimensions, rewrite)); result.dimensions().addAll(copyDimensions(arrayType.dimensions(), rewrite)); } else { Type elementType= (Type) rewrite.createCopyTarget(type); result= ast.newArrayType(elementType, 0); result.dimensions().addAll(copyDimensions(extraDimensions, rewrite)); } return result; }
/** * Creates a {@link ASTRewrite#createCopyTarget(ASTNode) copy} of <code>type</code> and adds * <code>extraDimensions</code> to it. * * @param type the type to copy * @param extraDimensions the dimensions to add * @param rewrite the ASTRewrite with which to create new nodes * @return the copy target with added dimensions */ public static Type copyTypeAndAddDimensions( Type type, List<Dimension> extraDimensions, ASTRewrite rewrite) { AST ast = rewrite.getAST(); if (extraDimensions.isEmpty()) { return (Type) rewrite.createCopyTarget(type); } ArrayType result; if (type instanceof ArrayType) { ArrayType arrayType = (ArrayType) type; Type varElementType = (Type) rewrite.createCopyTarget(arrayType.getElementType()); result = ast.newArrayType(varElementType, 0); result.dimensions().addAll(copyDimensions(extraDimensions, rewrite)); result.dimensions().addAll(copyDimensions(arrayType.dimensions(), rewrite)); } else { Type elementType = (Type) rewrite.createCopyTarget(type); result = ast.newArrayType(elementType, 0); result.dimensions().addAll(copyDimensions(extraDimensions, rewrite)); } return result; }
private FieldDeclaration createNewFieldDeclaration(ASTRewrite rewrite) { AST ast= getAST(); VariableDeclarationFragment fragment= ast.newVariableDeclarationFragment(); SimpleName variableName= ast.newSimpleName(fFieldName); fragment.setName(variableName); addLinkedName(rewrite, variableName, false); List<Dimension> extraDimensions= DimensionRewrite.copyDimensions(fTempDeclarationNode.extraDimensions(), rewrite); fragment.extraDimensions().addAll(extraDimensions); if (fInitializeIn == INITIALIZE_IN_FIELD && tempHasInitializer()){ Expression initializer= (Expression)rewrite.createCopyTarget(getTempInitializer()); fragment.setInitializer(initializer); } FieldDeclaration fieldDeclaration= ast.newFieldDeclaration(fragment); VariableDeclarationStatement vds= getTempDeclarationStatement(); Type type= (Type)rewrite.createCopyTarget(vds.getType()); fieldDeclaration.setType(type); fieldDeclaration.modifiers().addAll(ASTNodeFactory.newModifiers(ast, getModifiers())); return fieldDeclaration; }
/** * 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); } }
private void removeExtraDimensions(SingleVariableDeclaration oldParam) { ListRewrite listRewrite = getASTRewrite() .getListRewrite(oldParam, SingleVariableDeclaration.EXTRA_DIMENSIONS2_PROPERTY); for (Dimension dimension : (List<Dimension>) oldParam.extraDimensions()) { listRewrite.remove(dimension, fDescription); } }
private void removeExtraDimensions(MethodDeclaration methDecl) { ListRewrite listRewrite = getASTRewrite().getListRewrite(methDecl, MethodDeclaration.EXTRA_DIMENSIONS2_PROPERTY); for (Dimension dimension : (List<Dimension>) methDecl.extraDimensions()) { listRewrite.remove(dimension, fDescription); } }
public static Type newCreationType(AST ast, ITypeBinding typeBinding, ImportRewrite importRewrite, ImportRewriteContext importContext) { if (typeBinding.isParameterizedType()) { Type baseType= newCreationType(ast, typeBinding.getTypeDeclaration(), importRewrite, importContext); ParameterizedType parameterizedType= ast.newParameterizedType(baseType); for (ITypeBinding typeArgument : typeBinding.getTypeArguments()) { parameterizedType.typeArguments().add(newCreationType(ast, typeArgument, importRewrite, importContext)); } return parameterizedType; } else if (typeBinding.isParameterizedType()) { Type elementType= newCreationType(ast, typeBinding.getElementType(), importRewrite, importContext); ArrayType arrayType= ast.newArrayType(elementType, 0); while (typeBinding.isArray()) { Dimension dimension= ast.newDimension(); IAnnotationBinding[] typeAnnotations= typeBinding.getTypeAnnotations(); for (IAnnotationBinding typeAnnotation : typeAnnotations) { dimension.annotations().add(importRewrite.addAnnotation(typeAnnotation, ast, importContext)); } arrayType.dimensions().add(dimension); typeBinding= typeBinding.getComponentType(); } return arrayType; } else if (typeBinding.isWildcardType()) { ITypeBinding bound= typeBinding.getBound(); typeBinding= (bound != null) ? bound : typeBinding.getErasure(); return newCreationType(ast, typeBinding, importRewrite, importContext); } else { return importRewrite.addImport(typeBinding, ast, importContext); } }
@SuppressWarnings({ "unchecked" }) private void removeArrayDimensions(ASTRewrite rewrite, SingleVariableDeclaration lastParam) { // removes any additional dimensions (for variables declared like int a[]) ListRewrite extraDimensionsRewrite = rewrite.getListRewrite(lastParam, SingleVariableDeclaration.EXTRA_DIMENSIONS2_PROPERTY); List<Dimension> extraDimensions = lastParam.extraDimensions(); for (Dimension d : extraDimensions) { extraDimensionsRewrite.remove(d, null); } }
public static Type newCreationType( AST ast, ITypeBinding typeBinding, ImportRewrite importRewrite, ImportRewriteContext importContext) { if (typeBinding.isParameterizedType()) { Type baseType = newCreationType(ast, typeBinding.getTypeDeclaration(), importRewrite, importContext); ParameterizedType parameterizedType = ast.newParameterizedType(baseType); for (ITypeBinding typeArgument : typeBinding.getTypeArguments()) { parameterizedType .typeArguments() .add(newCreationType(ast, typeArgument, importRewrite, importContext)); } return parameterizedType; } else if (typeBinding.isParameterizedType()) { Type elementType = newCreationType(ast, typeBinding.getElementType(), importRewrite, importContext); ArrayType arrayType = ast.newArrayType(elementType, 0); while (typeBinding.isArray()) { Dimension dimension = ast.newDimension(); IAnnotationBinding[] typeAnnotations = typeBinding.getTypeAnnotations(); for (IAnnotationBinding typeAnnotation : typeAnnotations) { dimension .annotations() .add(importRewrite.addAnnotation(typeAnnotation, ast, importContext)); } arrayType.dimensions().add(dimension); typeBinding = typeBinding.getComponentType(); } return arrayType; } else if (typeBinding.isWildcardType()) { ITypeBinding bound = typeBinding.getBound(); typeBinding = (bound != null) ? bound : typeBinding.getErasure(); return newCreationType(ast, typeBinding, importRewrite, importContext); } else { return importRewrite.addImport(typeBinding, ast, importContext); } }
private MethodDeclaration createSetterMethod(AST ast, ASTRewrite rewriter, String lineDelimiter) throws CoreException { FieldDeclaration field = (FieldDeclaration) ASTNodes.getParent(fFieldDeclaration, FieldDeclaration.class); Type type = field.getType(); MethodDeclaration result = ast.newMethodDeclaration(); result.setName(ast.newSimpleName(fSetterName)); result.modifiers().addAll(ASTNodeFactory.newModifiers(ast, createModifiers())); if (fSetterMustReturnValue) { result.setReturnType2((Type) rewriter.createCopyTarget(type)); } SingleVariableDeclaration param = ast.newSingleVariableDeclaration(); result.parameters().add(param); param.setName(ast.newSimpleName(fArgName)); param.setType((Type) rewriter.createCopyTarget(type)); List<Dimension> extraDimensions = DimensionRewrite.copyDimensions(fFieldDeclaration.extraDimensions(), rewriter); param.extraDimensions().addAll(extraDimensions); Block block = ast.newBlock(); result.setBody(block); String fieldAccess = createFieldAccess(); String body = CodeGeneration.getSetterMethodBodyContent( fField.getCompilationUnit(), getTypeName(field.getParent()), fSetterName, fieldAccess, fArgName, lineDelimiter); if (body != null) { ASTNode setterNode = rewriter.createStringPlaceholder(body, ASTNode.BLOCK); block.statements().add(setterNode); } else { Assignment ass = ast.newAssignment(); ass.setLeftHandSide( (Expression) rewriter.createStringPlaceholder(fieldAccess, ASTNode.QUALIFIED_NAME)); ass.setRightHandSide(ast.newSimpleName(fArgName)); block.statements().add(ass); } if (fSetterMustReturnValue) { ReturnStatement rs = ast.newReturnStatement(); rs.setExpression(ast.newSimpleName(fArgName)); block.statements().add(rs); } if (fGenerateJavadoc) { String string = CodeGeneration.getSetterComment( fField.getCompilationUnit(), getTypeName(field.getParent()), fSetterName, fField.getElementName(), ASTNodes.asString(type), fArgName, StubUtility.getBaseName(fField), lineDelimiter); if (string != null) { Javadoc javadoc = (Javadoc) fRewriter.createStringPlaceholder(string, ASTNode.JAVADOC); result.setJavadoc(javadoc); } } return result; }
@Override public boolean visit(final Dimension node) { return false; }
@Override public boolean visit(final Dimension node) { // not instrumentable, contains no instrumentable nodes return false; }
protected static void copyExtraDimensions(final VariableDeclaration oldVarDeclaration, final VariableDeclaration newVarDeclaration) { final AST ast= newVarDeclaration.getAST(); for (int index= 0, n= oldVarDeclaration.extraDimensions().size(); index < n; index++) newVarDeclaration.extraDimensions().add(ASTNode.copySubtree(ast, (Dimension) oldVarDeclaration.extraDimensions().get(index))); }
protected static void copyExtraDimensions(final MethodDeclaration oldMethod, final MethodDeclaration newMethod) { final AST ast= newMethod.getAST(); for (int index= 0, n= oldMethod.extraDimensions().size(); index < n; index++) newMethod.extraDimensions().add(ASTNode.copySubtree(ast, (Dimension) oldMethod.extraDimensions().get(index))); }
private void removeExtraDimensions(SingleVariableDeclaration oldParam) { ListRewrite listRewrite= getASTRewrite().getListRewrite(oldParam, SingleVariableDeclaration.EXTRA_DIMENSIONS2_PROPERTY); for (Dimension dimension : (List<Dimension>) oldParam.extraDimensions()) { listRewrite.remove(dimension, fDescription); } }
private void removeExtraDimensions(MethodDeclaration methDecl) { ListRewrite listRewrite= getASTRewrite().getListRewrite(methDecl, MethodDeclaration.EXTRA_DIMENSIONS2_PROPERTY); for (Dimension dimension : (List<Dimension>) methDecl.extraDimensions()) { listRewrite.remove(dimension, fDescription); } }
private MethodDeclaration createSetterMethod(AST ast, ASTRewrite rewriter, String lineDelimiter) throws CoreException { FieldDeclaration field= (FieldDeclaration)ASTNodes.getParent(fFieldDeclaration, FieldDeclaration.class); Type type= field.getType(); MethodDeclaration result= ast.newMethodDeclaration(); result.setName(ast.newSimpleName(fSetterName)); result.modifiers().addAll(ASTNodeFactory.newModifiers(ast, createModifiers())); if (fSetterMustReturnValue) { result.setReturnType2((Type)rewriter.createCopyTarget(type)); } SingleVariableDeclaration param= ast.newSingleVariableDeclaration(); result.parameters().add(param); param.setName(ast.newSimpleName(fArgName)); param.setType((Type)rewriter.createCopyTarget(type)); List<Dimension> extraDimensions= DimensionRewrite.copyDimensions(fFieldDeclaration.extraDimensions(), rewriter); param.extraDimensions().addAll(extraDimensions); Block block= ast.newBlock(); result.setBody(block); String fieldAccess= createFieldAccess(); String body= CodeGeneration.getSetterMethodBodyContent(fField.getCompilationUnit(), getTypeName(field.getParent()), fSetterName, fieldAccess, fArgName, lineDelimiter); if (body != null) { ASTNode setterNode= rewriter.createStringPlaceholder(body, ASTNode.BLOCK); block.statements().add(setterNode); } else { Assignment ass= ast.newAssignment(); ass.setLeftHandSide((Expression) rewriter.createStringPlaceholder(fieldAccess, ASTNode.QUALIFIED_NAME)); ass.setRightHandSide(ast.newSimpleName(fArgName)); block.statements().add(ass); } if (fSetterMustReturnValue) { ReturnStatement rs= ast.newReturnStatement(); rs.setExpression(ast.newSimpleName(fArgName)); block.statements().add(rs); } if (fGenerateJavadoc) { String string= CodeGeneration.getSetterComment( fField.getCompilationUnit() , getTypeName(field.getParent()), fSetterName, fField.getElementName(), ASTNodes.asString(type), fArgName, StubUtility.getBaseName(fField), lineDelimiter); if (string != null) { Javadoc javadoc= (Javadoc)fRewriter.createStringPlaceholder(string, ASTNode.JAVADOC); result.setJavadoc(javadoc); } } return result; }
@Override public void endVisit(Dimension node) { endVisitNode(node); }
@Override public boolean visit(Dimension node) { return visitNode(node); }
/** * Returns {@link ASTRewrite#createCopyTarget(ASTNode) copies} of the given <code>dimensions</code>. * * @param dimensions the dimensions to copy * @param rewrite the ASTRewrite with which to create new nodes * * @return list of copy targets */ public static List<Dimension> copyDimensions(List<Dimension> dimensions, ASTRewrite rewrite) { ArrayList<Dimension> result= new ArrayList<>(); for (int i= 0; i < dimensions.size(); i++) { result.add((Dimension) rewrite.createCopyTarget(dimensions.get(i))); } return result; }
/** * Returns {@link ASTRewrite#createCopyTarget(ASTNode) copies} of the given <code>dimensions * </code>. * * @param dimensions the dimensions to copy * @param rewrite the ASTRewrite with which to create new nodes * @return list of copy targets */ public static List<Dimension> copyDimensions(List<Dimension> dimensions, ASTRewrite rewrite) { ArrayList<Dimension> result = new ArrayList<Dimension>(); for (int i = 0; i < dimensions.size(); i++) { result.add((Dimension) rewrite.createCopyTarget(dimensions.get(i))); } return result; }
/** * Returns {@link ASTRewrite#createCopyTarget(ASTNode) copies} of the given <code>dimensions</code>. * * @param dimensions the dimensions to copy * @param rewrite the ASTRewrite with which to create new nodes * * @return list of copy targets */ public static List<Dimension> copyDimensions(List<Dimension> dimensions, ASTRewrite rewrite) { ArrayList<Dimension> result= new ArrayList<Dimension>(); for (int i= 0; i < dimensions.size(); i++) { result.add((Dimension) rewrite.createCopyTarget(dimensions.get(i))); } return result; }