public static int getDimensions(VariableDeclaration declaration) { int dim= declaration.getExtraDimensions(); if (declaration instanceof VariableDeclarationFragment && declaration.getParent() instanceof LambdaExpression) { LambdaExpression lambda= (LambdaExpression) declaration.getParent(); IMethodBinding methodBinding= lambda.resolveMethodBinding(); if (methodBinding != null) { ITypeBinding[] parameterTypes= methodBinding.getParameterTypes(); int index= lambda.parameters().indexOf(declaration); ITypeBinding typeBinding= parameterTypes[index]; return typeBinding.getDimensions(); } } else { Type type= getType(declaration); if (type instanceof ArrayType) { dim+= ((ArrayType) type).getDimensions(); } } return dim; }
/** * 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 static String[] getVariableNameSuggestions(int variableKind, IJavaProject project, Type expectedType, Collection<String> excluded, boolean evaluateDefault) { int dim= 0; if (expectedType.isArrayType()) { ArrayType arrayType= (ArrayType)expectedType; dim= arrayType.getDimensions(); expectedType= arrayType.getElementType(); } if (expectedType.isParameterizedType()) { expectedType= ((ParameterizedType)expectedType).getType(); } String typeName= ASTNodes.getTypeName(expectedType); if (typeName.length() > 0) { return getVariableNameSuggestions(variableKind, project, typeName, dim, excluded, evaluateDefault); } return EMPTY; }
/** * 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 static String[] getVariableNameSuggestions( int variableKind, IJavaProject project, Type expectedType, Collection<String> excluded, boolean evaluateDefault) { int dim = 0; if (expectedType.isArrayType()) { ArrayType arrayType = (ArrayType) expectedType; dim = arrayType.getDimensions(); expectedType = arrayType.getElementType(); } if (expectedType.isParameterizedType()) { expectedType = ((ParameterizedType) expectedType).getType(); } String typeName = ASTNodes.getTypeName(expectedType); if (typeName.length() > 0) { return getVariableNameSuggestions( variableKind, project, typeName, dim, excluded, evaluateDefault); } return EMPTY; }
@Override public void createArrayInitStmt(final CaptureLog log, final int logRecNo) { final int oid = log.objectIds.get(logRecNo); final Object[] params = log.params.get(logRecNo); final String arrTypeName = log.oidClassNames.get(log.oidRecMapping.get(oid)); final Class<?> arrType = getClassForName(arrTypeName); // --- create array instance creation e.g. int[] var = new int[10]; final ArrayType arrAstType = (ArrayType) createAstArrayType(arrTypeName, ast); final ArrayCreation arrCreationExpr = ast.newArrayCreation(); arrCreationExpr.setType(arrAstType); arrCreationExpr.dimensions().add(ast.newNumberLiteral(String.valueOf(params.length))); final String arrVarName = this.createNewVarName(oid, arrTypeName); final VariableDeclarationFragment vd = ast.newVariableDeclarationFragment(); final SimpleName arrVarNameExpr = ast.newSimpleName(arrVarName); vd.setName(arrVarNameExpr); vd.setInitializer(arrCreationExpr); final VariableDeclarationStatement varDeclStmt = ast.newVariableDeclarationStatement(vd); varDeclStmt.setType(this.createAstType(arrTypeName, ast)); methodBlock.statements().add(varDeclStmt); // create array access statements var[0] = var1; Integer paramOID; Assignment assign; ArrayAccess arrAccessExpr; for(int i = 0; i < params.length; i++) { assign = ast.newAssignment(); arrAccessExpr = ast.newArrayAccess(); arrAccessExpr.setIndex(ast.newNumberLiteral(String.valueOf(i))); arrAccessExpr.setArray(arrVarNameExpr); assign.setLeftHandSide(arrAccessExpr); paramOID = (Integer) params[i]; if(paramOID == null) { assign.setRightHandSide(ast.newNullLiteral()); } else { assign.setRightHandSide(ast.newSimpleName(this.oidToVarMapping.get(paramOID))); } methodBlock.statements().add(assign); } }
@Override public boolean visit(ArrayCreation node) { this.fBuffer.append("new ");//$NON-NLS-1$ ArrayType at= node.getType(); int dims= at.getDimensions(); Type elementType= at.getElementType(); elementType.accept(this); for (Iterator<Expression> it= node.dimensions().iterator(); it.hasNext();) { this.fBuffer.append("[");//$NON-NLS-1$ Expression e= it.next(); e.accept(this); this.fBuffer.append("]");//$NON-NLS-1$ dims--; } // add empty "[]" for each extra array dimension for (int i= 0; i < dims; i++) { this.fBuffer.append("[]");//$NON-NLS-1$ } if (node.getInitializer() != null) { node.getInitializer().accept(this); } return false; }
public boolean visit(ArrayCreation node) { this.buffer.append("new ");//$NON-NLS-1$ ArrayType at = node.getType(); int dims = at.getDimensions(); Type elementType = at.getElementType(); elementType.accept(this); for (Iterator it = node.dimensions().iterator(); it.hasNext(); ) { this.buffer.append("[");//$NON-NLS-1$ Expression e = (Expression) it.next(); e.accept(this); this.buffer.append("]");//$NON-NLS-1$ dims--; } // add empty "[]" for each extra array dimension for (int i= 0; i < dims; i++) { this.buffer.append("[]");//$NON-NLS-1$ } if (node.getInitializer() != null) { node.getInitializer().accept(this); } return false; }
@Override protected void repairBug(ASTRewrite rewrite, CompilationUnit workingUnit, BugInstance bug) throws BugResolutionException { MethodDeclaration thisMethod = TraversalUtil.findEnclosingMethod(workingUnit, bug.getPrimarySourceLineAnnotation()); @SuppressWarnings("unchecked") List<SingleVariableDeclaration> params = thisMethod.parameters(); SingleVariableDeclaration lastParam = params.get(params.size() - 1); removeArrayDimensions(rewrite, lastParam); rewrite.set(lastParam, SingleVariableDeclaration.VARARGS_PROPERTY, Boolean.TRUE, null); Type lastType = lastParam.getType(); if (lastType.isArrayType()) { // can be false for the int a[] declaration Type bareType = ((ArrayType) lastType).getElementType(); rewrite.replace(lastType, rewrite.createCopyTarget(bareType), null); } }
public Binding getBindingForArrayType(final ArrayType type) { Binding result = null; ITypeBinding binding = type.resolveBinding(); if (binding == null) { if (this.logJDTBindingsIssues) { MoDiscoLogger.logWarning("*** WARNING : binding '" //$NON-NLS-1$ + type.toString() + "' unresolved.", JavaActivator //$NON-NLS-1$ .getDefault()); } result = new UnresolvedBinding(type.toString()); } else { result = new ClassBinding(); result.setName(binding.getQualifiedName()); } return result; }
@Override public boolean visit(ArrayCreation node) { //System.out.println("Found: " + node.getClass()); if (node.getInitializer() != null) { node.getInitializer().accept(this); return false; } print("new "); Type base = node.getType().getComponentType(); while (base instanceof ArrayType) { base = ((ArrayType)base).getComponentType(); } base.accept(this); print("["); int printed = 0; for (Object o : node.dimensions()) { Expression e = (Expression)o; if (printed > 0) { print("]["); } e.accept(this); printed++; } print("]"); return false; }
protected boolean tryToResolve(Type type) { boolean result = false; if (underResolution.getResolverMap().containsKey(type)) { result = true; } else { if (type.isPrimitiveType()) { result = tryToResolve((PrimitiveType)type); } else if (type.isQualifiedType()) { result = tryToResolve((QualifiedType)type); } else if (type.isArrayType()) { result = tryToResolve(((ArrayType)type).getElementType()); } else if (type.isSimpleType()) { result = tryToResolve((SimpleType)type); } else if (type.isParameterizedType()) { result = tryToResolve((ParameterizedType)type); } else if (type.isWildcardType()) { result = tryToResolve((WildcardType)type); } else { CoreActivator.log(IStatus.INFO, "Type not handled " + type.toString()); } } return result; }
/** * Return a name instance from a Type instance */ public static Name getName(Type t) { if (t.isArrayType()) { ArrayType arrayType = (ArrayType) t; return getName(ASTArrayTypeHelper.getComponentType(arrayType)); } else if (t.isParameterizedType()) { return getName(((ParameterizedType) t).getType()); } else if (t.isPrimitiveType()) { return null; } else if (t.isQualifiedType()) { return ((QualifiedType) t).getName(); } else if (t.isSimpleType()) { return ((SimpleType) t).getName(); } return null; }
public static ChildPropertyDescriptor getArrayTypeElementPropertyDescriptor(ArrayType node) { List<?> propertyDescriptors = ArrayType.propertyDescriptors(node.getAST().apiLevel()); for (Object propertyDescriptor : propertyDescriptors) { if (propertyDescriptor instanceof ChildPropertyDescriptor) { ChildPropertyDescriptor childListPropertyDescriptor = (ChildPropertyDescriptor) propertyDescriptor; if (DESCRIPTOR_NAME_AST4.equals(childListPropertyDescriptor.getId())) { if (OptimusLogger.logger.isLoggable(Level.FINE)) { OptimusLogger.logger.fine(String.format(RETURNED_DESCRIPTOR_MESSAGE, DESCRIPTOR_NAME_AST4)); } return childListPropertyDescriptor; } else if (DESCRIPTOR_NAME_AST8.equals(childListPropertyDescriptor.getId())) { if (OptimusLogger.logger.isLoggable(Level.FINE)) { OptimusLogger.logger.fine(String.format(RETURNED_DESCRIPTOR_MESSAGE, DESCRIPTOR_NAME_AST8)); } return childListPropertyDescriptor; } } } return null; }
/** * Returns the element type. This is a convenience method that returns its * argument if it is a simple type and the element type if the parameter is an array type. * @param type The type to get the element type from. * @return The element type of the type or the type itself. */ public static Type getElementType(Type type) { if (! type.isArrayType()) { return type; } return ((ArrayType)type).getElementType(); }
/** * 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); } }
@Override public void endVisit(ArrayType node) { if (skipNode(node)) { return; } processSequential(node, node.getElementType()); }
/** * 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); } }
@Override public void endVisit(ArrayCreation node) { ArrayType arrayType = node.getType(); TypeVariable2 arrayTypeCv = (TypeVariable2) getConstraintVariable(arrayType); if (arrayTypeCv == null) return; setConstraintVariable(node, arrayTypeCv); // TODO: constraints for array initializer? }
private Expression getTempInitializerCopy(ASTRewrite rewrite) { final Expression initializer = (Expression) rewrite.createCopyTarget(getTempInitializer()); if (initializer instanceof ArrayInitializer && ASTNodes.getDimensions(fTempDeclarationNode) > 0) { ArrayCreation arrayCreation = rewrite.getAST().newArrayCreation(); arrayCreation.setType( (ArrayType) ASTNodeFactory.newType(rewrite.getAST(), fTempDeclarationNode)); arrayCreation.setInitializer((ArrayInitializer) initializer); return arrayCreation; } return initializer; }
private static boolean isVoidArrayType(Type type) { if (!type.isArrayType()) return false; ArrayType arrayType = (ArrayType) type; if (!arrayType.getElementType().isPrimitiveType()) return false; PrimitiveType primitiveType = (PrimitiveType) arrayType.getElementType(); return (primitiveType.getPrimitiveTypeCode() == PrimitiveType.VOID); }
@Override public final void endVisit(final ArrayType node) { Type elementType= node.getElementType(); final ConstraintVariable2 variable= fModel.createTypeVariable(elementType); if (variable != null) { elementType.setProperty(PROPERTY_CONSTRAINT_VARIABLE, variable); node.setProperty(PROPERTY_CONSTRAINT_VARIABLE, variable); } }
@Override public void endVisit(ArrayCreation node) { ArrayType arrayType= node.getType(); TypeVariable2 arrayTypeCv= (TypeVariable2) getConstraintVariable(arrayType); if (arrayTypeCv == null) return; setConstraintVariable(node, arrayTypeCv); //TODO: constraints for array initializer? }
private Expression getTempInitializerCopy(ASTRewrite rewrite) { final Expression initializer= (Expression) rewrite.createCopyTarget(getTempInitializer()); if (initializer instanceof ArrayInitializer && ASTNodes.getDimensions(fTempDeclarationNode) > 0) { ArrayCreation arrayCreation= rewrite.getAST().newArrayCreation(); arrayCreation.setType((ArrayType) ASTNodeFactory.newType(rewrite.getAST(), fTempDeclarationNode)); arrayCreation.setInitializer((ArrayInitializer) initializer); return arrayCreation; } return initializer; }
private static boolean isVoidArrayType(Type type){ if (! type.isArrayType()) return false; ArrayType arrayType= (ArrayType)type; if (! arrayType.getElementType().isPrimitiveType()) return false; PrimitiveType primitiveType= (PrimitiveType) arrayType.getElementType(); return (primitiveType.getPrimitiveTypeCode() == PrimitiveType.VOID); }
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 static boolean isVoidArrayType(Type type){ if (! type.isArrayType()) return false; ArrayType arrayType= (ArrayType)type; if (! arrayType.getComponentType().isPrimitiveType()) return false; PrimitiveType primitiveType= (PrimitiveType)arrayType.getComponentType(); return (primitiveType.getPrimitiveTypeCode() == PrimitiveType.VOID); }
public static int getDimensions(VariableDeclaration declaration) { int dim= declaration.getExtraDimensions(); Type type= getType(declaration); if (type instanceof ArrayType) { dim += ((ArrayType) type).getDimensions(); } return dim; }
private static String getPrettyTypeName(Type type) { if (type.isArrayType()) { return getPrettyTypeName((ArrayType) type); } else if (type.isParameterizedType()) { return getPrettyTypeName((ParameterizedType) type); } else if (type.isPrimitiveType()) { return getPrettyTypeName((PrimitiveType) type); } else if (type.isQualifiedType()) { return getPrettyTypeName((QualifiedType) type); } else if (type.isSimpleType()) { return getPrettyTypeName((SimpleType) type); } else { return ""; } }
@Override public void write(ArrayCreation arrayCreation) { matchAndWrite("new"); List<?> dimensions = arrayCreation.dimensions(); // TODO: Support multidimensional arrays if (dimensions.size() > 1) throw new JUniversalException("Multidimensional arrays not currently supported"); // TODO: Support array initializers if (arrayCreation.getInitializer() != null) throw new JUniversalException("Array initializers not currently supported"); Expression dimensionSizeExpression = (Expression) dimensions.get(0); setPosition(dimensionSizeExpression.getStartPosition()); write("("); writeNode(dimensionSizeExpression); copySpaceAndComments(); write(") "); ArrayType arrayType = arrayCreation.getType(); setPosition(arrayType.getStartPosition()); write("Array<"); writeNode(arrayType.getElementType()); skipSpaceAndComments(); write(">"); setPosition(ASTUtil.getEndPosition(dimensionSizeExpression)); skipSpaceAndComments(); match("]"); }
@Override public boolean visit(ArrayType node) { //System.out.println("Found: " + node.getClass()); //visit(node.getComponentType()); node.getComponentType().accept(this); print("[]"); return false; }