Java Code Examples for org.eclipse.jdt.core.dom.AST#newArrayType()

The following examples show how to use org.eclipse.jdt.core.dom.AST#newArrayType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DimensionRewrite.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 2
Source File: DimensionRewrite.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 3
Source File: TypeParseVisitor.java    From SimFix with GNU General Public License v2.0 5 votes vote down vote up
public boolean visit(TypeDeclaration node) {
	Pair<String, String> clazzAndMethodName = NodeUtils.getTypeDecAndMethodDec(node.getName());
	String clazz = clazzAndMethodName.getFirst();
	AST ast = AST.newAST(AST.JLS8);
	Type type = ast.newSimpleType(ast.newSimpleName(clazz));
	ProjectInfo.addFieldType(clazz, "THIS", type);
	Type suType = node.getSuperclassType();
	if(suType != null){
		ProjectInfo.addFieldType(clazz, "SUPER", suType);
		ProjectInfo.addSuperClass(clazz, suType.toString());
	}
	
	List<Object> sInterfaces = node.superInterfaceTypes();
	if(sInterfaces != null){
		for(Object object : sInterfaces){
			if(object instanceof Type){
				Type interfaceType = (Type) object;
				ProjectInfo.addSuperInterface(clazz, interfaceType.toString());
			}
		}
	}
	
	FieldDeclaration fields[] = node.getFields();
	for (FieldDeclaration f : fields) {
		for (Object o : f.fragments()) {
			VariableDeclarationFragment vdf = (VariableDeclarationFragment) o;
			Type tmpType = f.getType();
			if(vdf.getExtraDimensions() > 0){
				tmpType = ast.newArrayType((Type) ASTNode.copySubtree(ast, tmpType), vdf.getExtraDimensions());
			}
			ProjectInfo.addFieldType(clazz, vdf.getName().toString(), tmpType);
		}
	}
	return true;
}
 
Example 4
Source File: TypeParseVisitor.java    From SimFix with GNU General Public License v2.0 5 votes vote down vote up
public boolean visit(VariableDeclarationStatement node) {
	if(isAnonymousClass(node)){
		return true;
	}
	for (Object o : node.fragments()) {
		VariableDeclarationFragment vdf = (VariableDeclarationFragment) o;
		Type type = node.getType();
		if(vdf.getExtraDimensions() > 0){
			AST ast = AST.newAST(AST.JLS8);
			type = ast.newArrayType((Type) ASTNode.copySubtree(ast, type), vdf.getExtraDimensions());
		}
		map.put(vdf.getName().toString(), type);
	}
	return true;
}
 
Example 5
Source File: TypeParseVisitor.java    From SimFix with GNU General Public License v2.0 5 votes vote down vote up
public boolean visit(VariableDeclarationExpression node) {
	if(isAnonymousClass(node)){
		return true;
	}
	for (Object o : node.fragments()) {
		VariableDeclarationFragment vdf = (VariableDeclarationFragment) o;
		Type type = node.getType();
		if(vdf.getExtraDimensions() > 0){
			AST ast = AST.newAST(AST.JLS8);
			type = ast.newArrayType((Type) ASTNode.copySubtree(ast, type), vdf.getExtraDimensions());
		}
		map.put(vdf.getName().toString(), type);
	}
	return true;
}
 
Example 6
Source File: IntroduceFactoryRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * @param argType
 * @param extraDims number of extra array dimensions to add to the resulting type
 * @param ast
 * @return a Type that describes the given ITypeBinding. If the binding
 * refers to an object type, use the import rewriter to determine whether
 * the reference requires a new import, or instead needs to be qualified.<br>
 * Like ASTNodeFactory.newType(), but for the handling of imports.
 */
private Type typeNodeForTypeBinding(ITypeBinding argType, int extraDims, AST ast) {
	if (extraDims > 0) {
		return ast.newArrayType(typeNodeForTypeBinding(argType, 0, ast), extraDims);

	} else if (argType.isArray()) {
		Type elementType= typeNodeForTypeBinding(argType.getElementType(), extraDims, ast);
		return ast.newArrayType(elementType, argType.getDimensions());

	} else {
		return fImportRewriter.addImport(argType, ast);
	}
}
 
Example 7
Source File: ASTNodeFactory.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
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);
	}
}
 
Example 8
Source File: ConvertForLoopOperation.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private SingleVariableDeclaration createParameterDeclaration(String parameterName, VariableDeclarationFragment fragement, Expression arrayAccess, ForStatement statement, ImportRewrite importRewrite, ASTRewrite rewrite, TextEditGroup group, LinkedProposalPositionGroup pg, boolean makeFinal) {
	CompilationUnit compilationUnit= (CompilationUnit)arrayAccess.getRoot();
	AST ast= compilationUnit.getAST();

	SingleVariableDeclaration result= ast.newSingleVariableDeclaration();

	SimpleName name= ast.newSimpleName(parameterName);
	pg.addPosition(rewrite.track(name), true);
	result.setName(name);

	ITypeBinding arrayTypeBinding= arrayAccess.resolveTypeBinding();
	Type type= importType(arrayTypeBinding.getElementType(), statement, importRewrite, compilationUnit);
	if (arrayTypeBinding.getDimensions() != 1) {
		type= ast.newArrayType(type, arrayTypeBinding.getDimensions() - 1);
	}
	result.setType(type);

	if (fragement != null) {
		VariableDeclarationStatement declaration= (VariableDeclarationStatement)fragement.getParent();
		ModifierRewrite.create(rewrite, result).copyAllModifiers(declaration, group);
	}
	if (makeFinal && (fragement == null || ASTNodes.findModifierNode(Modifier.FINAL, ASTNodes.getModifiers(fragement)) == null)) {
		ModifierRewrite.create(rewrite, result).setModifiers(Modifier.FINAL, 0, group);
	}

	return result;
}
 
Example 9
Source File: ExtractToNullCheckedLocalProposal.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/** 
 * Create a fresh type reference
 * @param typeBinding the type we want to refer to
 * @param ast AST for creating new nodes
 * @param imports use this for optimal type names
 * @return a fully features non-null type reference (can be parameterized and/or array).
 */
public static Type newType(ITypeBinding typeBinding, AST ast, ImportRewrite imports) {
	// unwrap array type:
	int dimensions= typeBinding.getDimensions();
	if (dimensions > 0)
		typeBinding= typeBinding.getElementType();
	
	// unwrap parameterized type:
	ITypeBinding[] typeArguments= typeBinding.getTypeArguments();
	typeBinding= typeBinding.getErasure();	

	// create leaf type:
	Type elementType = (typeBinding.isPrimitive())
				? ast.newPrimitiveType(PrimitiveType.toCode(typeBinding.getName()))
				: ast.newSimpleType(ast.newName(imports.addImport(typeBinding)));

	// re-wrap as parameterized type:
	if (typeArguments.length > 0) {
		ParameterizedType parameterizedType= ast.newParameterizedType(elementType);
		for (ITypeBinding typeArgument : typeArguments)
			parameterizedType.typeArguments().add(newType(typeArgument, ast, imports));
		elementType = parameterizedType;
	}
	
	// re-wrap as array type:
	if (dimensions > 0)
		return ast.newArrayType(elementType, dimensions);
	else
		return elementType;
}
 
Example 10
Source File: JavaStatementPostfixContext.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private Type createType(String typeSig, AST ast) {
	int sigKind = Signature.getTypeSignatureKind(typeSig);
       switch (sigKind) {
           case Signature.BASE_TYPE_SIGNATURE:
               return ast.newPrimitiveType(PrimitiveType.toCode(Signature.toString(typeSig)));
           case Signature.ARRAY_TYPE_SIGNATURE:
               Type elementType = createType(Signature.getElementType(typeSig), ast);
               return ast.newArrayType(elementType, Signature.getArrayCount(typeSig));
           case Signature.CLASS_TYPE_SIGNATURE:
               String erasureSig = Signature.getTypeErasure(typeSig);

               String erasureName = Signature.toString(erasureSig);
               if (erasureSig.charAt(0) == Signature.C_RESOLVED) {
                   erasureName = addImport(erasureName);
               }
               
               Type baseType= ast.newSimpleType(ast.newName(erasureName));
               String[] typeArguments = Signature.getTypeArguments(typeSig);
               if (typeArguments.length > 0) {
                   ParameterizedType type = ast.newParameterizedType(baseType);
                   List argNodes = type.typeArguments();
                   for (int i = 0; i < typeArguments.length; i++) {
                       String curr = typeArguments[i];
                       if (containsNestedCapture(curr)) {
                           argNodes.add(ast.newWildcardType());
                       } else {
                           argNodes.add(createType(curr, ast));
                       }
                   }
                   return type;
               }
               return baseType;
           case Signature.TYPE_VARIABLE_SIGNATURE:
               return ast.newSimpleType(ast.newSimpleName(Signature.toString(typeSig)));
           case Signature.WILDCARD_TYPE_SIGNATURE:
               WildcardType wildcardType= ast.newWildcardType();
               char ch = typeSig.charAt(0);
               if (ch != Signature.C_STAR) {
                   Type bound= createType(typeSig.substring(1), ast);
                   wildcardType.setBound(bound, ch == Signature.C_EXTENDS);
               }
               return wildcardType;
           case Signature.CAPTURE_TYPE_SIGNATURE:
               return createType(typeSig.substring(1), ast);
       }
       
       return ast.newSimpleType(ast.newName("java.lang.Object"));
}