use of org.eclipse.jdt.core.dom.ArrayType in project eclipse.jdt.ls by eclipse.
the class StubUtility method getVariableNameSuggestions.
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;
}
use of org.eclipse.jdt.core.dom.ArrayType in project eclipse.jdt.ls by eclipse.
the class StubUtility2 method createParameters.
// private static List<SingleVariableDeclaration> createParameters(IJavaProject project, ImportRewrite imports, ImportRewriteContext context, AST ast, IMethodBinding binding, String[] paramNames, MethodDeclaration decl) {
// return createParameters(project, imports, context, ast, binding, paramNames, decl, null);
// }
private static List<SingleVariableDeclaration> createParameters(IJavaProject project, ImportRewrite imports, ImportRewriteContext context, AST ast, IMethodBinding binding, String[] paramNames, MethodDeclaration decl, IAnnotationBinding defaultNullness) {
boolean is50OrHigher = JavaModelUtil.is50OrHigher(project);
List<SingleVariableDeclaration> parameters = decl.parameters();
ITypeBinding[] params = binding.getParameterTypes();
if (paramNames == null || paramNames.length < params.length) {
paramNames = StubUtility.suggestArgumentNames(project, binding);
}
for (int i = 0; i < params.length; i++) {
SingleVariableDeclaration var = ast.newSingleVariableDeclaration();
ITypeBinding type = params[i];
type = replaceWildcardsAndCaptures(type);
if (!is50OrHigher) {
type = type.getErasure();
var.setType(imports.addImport(type, ast, context, TypeLocation.PARAMETER));
} else if (binding.isVarargs() && type.isArray() && i == params.length - 1) {
var.setVarargs(true);
/*
* Varargs annotations are special.
* Example:
* foo(@O Object @A [] @B ... arg)
* => @B is not an annotation on the array dimension that constitutes the vararg.
* It's the type annotation of the *innermost* array dimension.
*/
int dimensions = type.getDimensions();
@SuppressWarnings("unchecked") List<Annotation>[] dimensionAnnotations = (List<Annotation>[]) new List<?>[dimensions];
for (int dim = 0; dim < dimensions; dim++) {
dimensionAnnotations[dim] = new ArrayList<>();
for (IAnnotationBinding annotation : type.getTypeAnnotations()) {
dimensionAnnotations[dim].add(imports.addAnnotation(annotation, ast, context));
}
type = type.getComponentType();
}
Type elementType = imports.addImport(type, ast, context);
if (dimensions == 1) {
var.setType(elementType);
} else {
ArrayType arrayType = ast.newArrayType(elementType, dimensions - 1);
List<Dimension> dimensionNodes = arrayType.dimensions();
for (int dim = 0; dim < dimensions - 1; dim++) {
// all except the innermost dimension
Dimension dimension = dimensionNodes.get(dim);
dimension.annotations().addAll(dimensionAnnotations[dim]);
}
var.setType(arrayType);
}
List<Annotation> varargTypeAnnotations = dimensionAnnotations[dimensions - 1];
var.varargsAnnotations().addAll(varargTypeAnnotations);
} else {
var.setType(imports.addImport(type, ast, context, TypeLocation.PARAMETER));
}
var.setName(ast.newSimpleName(paramNames[i]));
IAnnotationBinding[] annotations = binding.getParameterAnnotations(i);
for (IAnnotationBinding annotation : annotations) {
if (StubUtility2.isCopyOnInheritAnnotation(annotation.getAnnotationType(), project, defaultNullness)) {
var.modifiers().add(imports.addAnnotation(annotation, ast, context));
}
}
parameters.add(var);
}
return parameters;
}
use of org.eclipse.jdt.core.dom.ArrayType in project eclipse.jdt.ls by eclipse.
the class ASTNodeFactory method newType.
/**
* Returns the new type node corresponding to the type of the given declaration
* including the extra dimensions. If the type is a {@link UnionType}, use the LUB type.
* If the <code>importRewrite</code> is <code>null</code>, the type may be fully-qualified.
*
* @param ast The AST to create the resulting type with.
* @param declaration The variable declaration to get the type from
* @param importRewrite the import rewrite to use, or <code>null</code>
* @param context the import rewrite context, or <code>null</code>
* @return a new type node created with the given AST.
*
* @since 3.7.1
*/
public static Type newType(AST ast, VariableDeclaration declaration, ImportRewrite importRewrite, ImportRewriteContext context) {
if (declaration instanceof VariableDeclarationFragment && declaration.getParent() instanceof LambdaExpression) {
return newType((LambdaExpression) declaration.getParent(), (VariableDeclarationFragment) declaration, ast, importRewrite, context);
}
Type type = ASTNodes.getType(declaration);
if (declaration instanceof SingleVariableDeclaration) {
Type type2 = ((SingleVariableDeclaration) declaration).getType();
if (type2 instanceof UnionType) {
ITypeBinding typeBinding = type2.resolveBinding();
if (typeBinding != null) {
if (importRewrite != null) {
type = importRewrite.addImport(typeBinding, ast, context);
return type;
} else {
String qualifiedName = typeBinding.getQualifiedName();
if (qualifiedName.length() > 0) {
type = ast.newSimpleType(ast.newName(qualifiedName));
return type;
}
}
}
// XXX: fallback for intersection types or unresolved types: take first type of union
type = (Type) ((UnionType) type2).types().get(0);
return type;
}
}
type = (Type) ASTNode.copySubtree(ast, type);
List<Dimension> extraDimensions = declaration.extraDimensions();
if (!extraDimensions.isEmpty()) {
ArrayType arrayType;
if (type instanceof ArrayType) {
arrayType = (ArrayType) type;
} else {
arrayType = ast.newArrayType(type, 0);
type = arrayType;
}
arrayType.dimensions().addAll(ASTNode.copySubtrees(ast, extraDimensions));
}
return type;
}
use of org.eclipse.jdt.core.dom.ArrayType in project eclipse.jdt.ls by eclipse.
the class DimensionRewrite method copyTypeAndAddDimensions.
/**
* 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;
}
use of org.eclipse.jdt.core.dom.ArrayType in project eclipse-cs by checkstyle.
the class ArrayTypeStyleQuickfix method handleGetCorrectingASTVisitor.
/**
* {@inheritDoc}
*/
@Override
protected ASTVisitor handleGetCorrectingASTVisitor(final IRegion lineInfo, final int markerStartOffset) {
return new ASTVisitor() {
@Override
public boolean visit(VariableDeclarationStatement node) {
if (containsPosition(node, markerStartOffset)) {
if (isCStyle(node.fragments())) {
int dimensions = 0;
List<?> fragments = node.fragments();
for (int i = 0, size = fragments.size(); i < size; i++) {
VariableDeclaration decl = (VariableDeclaration) fragments.get(i);
if (decl.getExtraDimensions() > dimensions) {
dimensions = decl.getExtraDimensions();
}
decl.setExtraDimensions(0);
}
// wrap current type into ArrayType
ArrayType arrayType = createArrayType(node.getType(), dimensions);
node.setType(arrayType);
} else if (isJavaStyle(node.getType())) {
int dimensions = ((ArrayType) node.getType()).getDimensions();
List<?> fragments = node.fragments();
for (int i = 0, size = fragments.size(); i < size; i++) {
VariableDeclaration decl = (VariableDeclaration) fragments.get(i);
decl.setExtraDimensions(dimensions);
}
Type elementType = (Type) ASTNode.copySubtree(node.getAST(), ((ArrayType) node.getType()).getElementType());
node.setType(elementType);
}
}
return true;
}
@Override
public boolean visit(SingleVariableDeclaration node) {
if (containsPosition(node, markerStartOffset)) {
if (isCStyle(node)) {
// wrap the existing type into an array type
node.setType(createArrayType(node.getType(), node.getExtraDimensions()));
node.setExtraDimensions(0);
} else if (isJavaStyle(node.getType())) {
ArrayType arrayType = (ArrayType) node.getType();
Type elementType = (Type) ASTNode.copySubtree(node.getAST(), arrayType.getElementType());
node.setType(elementType);
node.setExtraDimensions(arrayType.getDimensions());
}
}
return true;
}
@Override
public boolean visit(FieldDeclaration node) {
if (containsPosition(node, markerStartOffset)) {
if (isCStyle(node.fragments())) {
int dimensions = 0;
List<?> fragments = node.fragments();
for (int i = 0, size = fragments.size(); i < size; i++) {
VariableDeclaration decl = (VariableDeclaration) fragments.get(i);
if (decl.getExtraDimensions() > dimensions) {
dimensions = decl.getExtraDimensions();
}
decl.setExtraDimensions(0);
}
// wrap current type into ArrayType
ArrayType arrayType = createArrayType(node.getType(), dimensions);
node.setType(arrayType);
} else if (isJavaStyle(node.getType())) {
int dimensions = ((ArrayType) node.getType()).getDimensions();
List<?> fragments = node.fragments();
for (int i = 0, size = fragments.size(); i < size; i++) {
VariableDeclaration decl = (VariableDeclaration) fragments.get(i);
decl.setExtraDimensions(dimensions);
}
Type elementType = (Type) ASTNode.copySubtree(node.getAST(), ((ArrayType) node.getType()).getElementType());
node.setType(elementType);
}
}
return true;
}
private boolean isJavaStyle(Type type) {
return type instanceof ArrayType;
}
private boolean isCStyle(VariableDeclaration decl) {
return decl.getExtraDimensions() > 0;
}
private boolean isCStyle(List<?> fragments) {
Iterator<?> it = fragments.iterator();
while (it.hasNext()) {
VariableDeclaration decl = (VariableDeclaration) it.next();
if (isCStyle(decl)) {
return true;
}
}
return false;
}
private ArrayType createArrayType(Type componentType, int dimensions) {
Type type = (Type) ASTNode.copySubtree(componentType.getAST(), componentType);
ArrayType arrayType = componentType.getAST().newArrayType(type, dimensions);
return arrayType;
}
};
}
Aggregations