Java Code Examples for org.eclipse.jdt.core.dom.ArrayType#getDimensions()

The following examples show how to use org.eclipse.jdt.core.dom.ArrayType#getDimensions() . 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: CompilationUnitBuilder.java    From j2cl with Apache License 2.0 6 votes vote down vote up
private NewArray convert(org.eclipse.jdt.core.dom.ArrayCreation expression) {
  ArrayType arrayType = expression.getType();

  List<Expression> dimensionExpressions =
      convertExpressions(JdtUtils.asTypedList(expression.dimensions()));
  // If some dimensions are not initialized then make that explicit.
  while (dimensionExpressions.size() < arrayType.getDimensions()) {
    dimensionExpressions.add(NullLiteral.get());
  }

  ArrayLiteral arrayLiteral =
      expression.getInitializer() == null ? null : convert(expression.getInitializer());

  ArrayTypeDescriptor typeDescriptor =
      (ArrayTypeDescriptor) JdtUtils.createTypeDescriptor(expression.resolveTypeBinding());
  return NewArray.newBuilder()
      .setTypeDescriptor(typeDescriptor)
      .setDimensionExpressions(dimensionExpressions)
      .setArrayLiteral(arrayLiteral)
      .build();
}
 
Example 2
Source File: StubUtility.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
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;
}
 
Example 3
Source File: ArrayCreate.java    From SimFix with GNU General Public License v2.0 5 votes vote down vote up
@Override
public StringBuffer toSrcString() {
	StringBuffer stringBuffer = new StringBuffer();
	stringBuffer.append("new ");
	if(_dimension != null && _dimension.size() > 0){
		// new a[4][];
		if(_type instanceof ArrayType){
			ArrayType arrayType = (ArrayType) _type;
			stringBuffer.append(arrayType.getElementType());
			for(int i = 0; i < arrayType.getDimensions(); i++){
				stringBuffer.append("[");
				if(_dimension.size() > i){
					stringBuffer.append(_dimension.get(i).toSrcString());
				}
				stringBuffer.append("]");
			}
			if(_initializer != null){
				stringBuffer.append(_initializer.toSrcString());
			}
		} else {
			stringBuffer = new StringBuffer();
			stringBuffer.append(_originalNode.toString());
		}
	} else {
		// new a[][]{1,2;3,4};
		stringBuffer.append(_type);
		if(_initializer != null){
			stringBuffer.append(_initializer.toSrcString());
		}
	}
	return stringBuffer;
}
 
Example 4
Source File: StyledStringVisitor.java    From JDeodorant with MIT License 5 votes vote down vote up
public boolean visit(ArrayType type) {
	/*
	 * ArrayType: Type [ ]
	 */
	activateDiffStyle(type);
	handleType(type.getElementType());
	for (int i = 0; i < type.getDimensions(); i++) {
		appendOpenBracket();
		appendClosedBracket();
	}
	deactivateDiffStyle(type);
	return false;
}
 
Example 5
Source File: JavaASTFlattener.java    From xtext-xtend with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public boolean visit(final ArrayCreation node) {
  ArrayType at = node.getType();
  int dims = at.getDimensions();
  if ((dims > 1)) {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("/* FIXME Only one dimensional arrays are supported. ");
    _builder.append(node);
    _builder.append("*/");
    this.appendToBuffer(_builder.toString());
    this.addProblem(node, "Only one dimension arrays are supported.");
    return false;
  }
  ArrayInitializer _initializer = node.getInitializer();
  boolean _tripleNotEquals = (_initializer != null);
  if (_tripleNotEquals) {
    if (this.fallBackStrategy) {
      this.appendToBuffer("(");
    }
    node.getInitializer().accept(this);
    if (this.fallBackStrategy) {
      this.appendToBuffer(" as ");
      at.accept(this);
      this.appendToBuffer(")");
    }
  } else {
    StringConcatenation _builder_1 = new StringConcatenation();
    _builder_1.append("new");
    String _xifexpression = null;
    boolean _isPrimitiveType = node.getType().getElementType().isPrimitiveType();
    if (_isPrimitiveType) {
      Type _elementType = node.getType().getElementType();
      _xifexpression = StringExtensions.toFirstUpper(((PrimitiveType) _elementType).getPrimitiveTypeCode().toString());
    }
    _builder_1.append(_xifexpression);
    _builder_1.append("ArrayOfSize(");
    this.appendToBuffer(_builder_1.toString());
    List _dimensions = node.dimensions();
    (((Expression[])Conversions.unwrapArray(((Iterable<Expression>) _dimensions), Expression.class))[0]).accept(this);
    this.appendToBuffer(")");
  }
  return false;
}
 
Example 6
Source File: Util.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private static void getFullyQualifiedName(Type type, StringBuffer buffer) {
	switch (type.getNodeType()) {
		case ASTNode.ARRAY_TYPE:
			ArrayType arrayType = (ArrayType) type;
			getFullyQualifiedName(arrayType.getElementType(), buffer);
			for (int i = 0, length = arrayType.getDimensions(); i < length; i++) {
				buffer.append('[');
				buffer.append(']');
			}
			break;
		case ASTNode.PARAMETERIZED_TYPE:
			ParameterizedType parameterizedType = (ParameterizedType) type;
			getFullyQualifiedName(parameterizedType.getType(), buffer);
			buffer.append('<');
			Iterator iterator = parameterizedType.typeArguments().iterator();
			boolean isFirst = true;
			while (iterator.hasNext()) {
				if (!isFirst)
					buffer.append(',');
				else
					isFirst = false;
				Type typeArgument = (Type) iterator.next();
				getFullyQualifiedName(typeArgument, buffer);
			}
			buffer.append('>');
			break;
		case ASTNode.PRIMITIVE_TYPE:
			buffer.append(((PrimitiveType) type).getPrimitiveTypeCode().toString());
			break;
		case ASTNode.QUALIFIED_TYPE:
			buffer.append(((QualifiedType) type).getName().getFullyQualifiedName());
			break;
		case ASTNode.SIMPLE_TYPE:
			buffer.append(((SimpleType) type).getName().getFullyQualifiedName());
			break;
		case ASTNode.WILDCARD_TYPE:
			buffer.append('?');
			WildcardType wildcardType = (WildcardType) type;
			Type bound = wildcardType.getBound();
			if (bound == null) return;
			if (wildcardType.isUpperBound()) {
				buffer.append(" extends "); //$NON-NLS-1$
			} else {
				buffer.append(" super "); //$NON-NLS-1$
			}
			getFullyQualifiedName(bound, buffer);
			break;
	}
}