Java Code Examples for org.eclipse.jdt.core.dom.ITypeBinding#getComponentType()

The following examples show how to use org.eclipse.jdt.core.dom.ITypeBinding#getComponentType() . 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: IntroduceIndirectionRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private void copyArguments(MethodDeclaration intermediary, CompilationUnitRewrite rew) throws JavaModelException {
	String[] names= fTargetMethod.getParameterNames();
	ITypeBinding[] types= fTargetMethodBinding.getParameterTypes();
	for (int i= 0; i < names.length; i++) {
		ITypeBinding typeBinding= types[i];
		SingleVariableDeclaration newElement= rew.getAST().newSingleVariableDeclaration();
		newElement.setName(rew.getAST().newSimpleName(names[i]));

		if (i == (names.length - 1) && fTargetMethodBinding.isVarargs()) {
			newElement.setVarargs(true);
			if (typeBinding.isArray())
				typeBinding= typeBinding.getComponentType();
		}

		newElement.setType(rew.getImportRewrite().addImport(typeBinding, rew.getAST()));
		intermediary.parameters().add(newElement);
	}
}
 
Example 2
Source File: JdtBasedTypeFactory.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * @since 2.4
 */
protected JvmAnnotationValue createAnnotationValue(ITypeBinding annotationType, /* @Nullable */ Object value, IMethodBinding methodBinding) {
	ITypeBinding originalTypeBinding = methodBinding.getReturnType();
	ITypeBinding typeBinding = originalTypeBinding;
	if (originalTypeBinding.isArray()) {
		typeBinding = typeBinding.getComponentType();
	}
	if (typeBinding.isParameterizedType())
		typeBinding = typeBinding.getErasure();
	JvmAnnotationValue annotationValue = createAnnotationValue(typeBinding, value);
	annotationValue.setOperation(createMethodProxy(annotationType, methodBinding));
	return annotationValue;
}
 
Example 3
Source File: JdtBasedTypeFactory.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
private void setDefaultValue(JvmOperation operation, IMethodBinding method) {
	Object defaultValue = method.getDefaultValue();
	if (defaultValue != null) {
		ITypeBinding originalTypeBinding = method.getReturnType();
		ITypeBinding typeBinding = originalTypeBinding;
		if (originalTypeBinding.isArray()) {
			typeBinding = typeBinding.getComponentType();
		}
		if (typeBinding.isParameterizedType())
			typeBinding = typeBinding.getErasure();
		JvmAnnotationValue annotationValue = createAnnotationValue(typeBinding, defaultValue);
		operation.setDefaultValue(annotationValue);
		annotationValue.setOperation(operation);
	}
}
 
Example 4
Source File: JdtBasedTypeFactory.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Returns a type reference for the given type binding. If the binding is null, an {@link JvmUnknownTypeReference unknown}
 * type reference is returned.
 */
// @NonNull 
protected JvmTypeReference createTypeReference(/* @Nullable */ ITypeBinding typeBinding) {
	if (typeBinding == null) {
		return TypesFactory.eINSTANCE.createJvmUnknownTypeReference();
	}
	if (typeBinding.isArray()) {
		ITypeBinding componentType = typeBinding.getComponentType();
		JvmTypeReference componentTypeReference = createTypeReference(componentType);
		JvmGenericArrayTypeReference typeReference = TypesFactory.eINSTANCE.createJvmGenericArrayTypeReference();
		typeReference.setComponentType(componentTypeReference);
		return typeReference;
	}
	ITypeBinding outer = null;
	if (typeBinding.isMember() && !Modifier.isStatic(typeBinding.getModifiers())) {
		outer = typeBinding.getDeclaringClass();
	}
	JvmParameterizedTypeReference result;
	if (outer != null) {
		JvmParameterizedTypeReference outerReference = (JvmParameterizedTypeReference) createTypeReference(outer);
		result = TypesFactory.eINSTANCE.createJvmInnerTypeReference();
		((JvmInnerTypeReference) result).setOuter(outerReference);
	} else {
		result = TypesFactory.eINSTANCE.createJvmParameterizedTypeReference();
	}
	ITypeBinding[] typeArguments = typeBinding.getTypeArguments();
	if (typeArguments.length != 0) {
		ITypeBinding erasure = typeBinding.getErasure();
		result.setType(createProxy(erasure));
		InternalEList<JvmTypeReference> arguments = (InternalEList<JvmTypeReference>)result.getArguments();
		for (int i = 0; i < typeArguments.length; i++) {
			JvmTypeReference argument = createTypeArgument(typeArguments[i]);
			arguments.addUnique(argument);
		}
	} else {
		result.setType(createProxy(typeBinding));
	}
	return result;
}
 
Example 5
Source File: AbstractPairedInterfaceValidator.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Comparing type bindings from different ASTs appears to work correctly
 * unless the bindings involve arrays of primitives or type parameters. In the
 * case of arrays case,
 * {@link TypeRules#canAssign(ITypeBinding, ITypeBinding)} assumes that the
 * bindings are from the same AST and so it uses an identity comparison
 * instead of equality.
 * 
 * In the case of type parameters, two List<T>'s where T extend Serializable
 * are not considered equal or assignment compatible. In this case, we simply
 * erase to the entire type and simply check raw types.
 * 
 * TODO: Maybe create a BindingUtilities class for this?
 */
protected static boolean canAssign(ITypeBinding lhs, ITypeBinding rhs) {
  if (containsTypeVariableReferences(lhs)
      || containsTypeVariableReferences(rhs)) {
    // One of the type bindings referenced a type parameter, so just compare
    // the erasures of each type
    lhs = lhs.getErasure();
    rhs = rhs.getErasure();
  }

  if (lhs.isArray() && rhs.isArray()) {
    if (lhs.getDimensions() == rhs.getDimensions()) {

      while (lhs.isArray()) {
        lhs = lhs.getComponentType();
      }

      while (rhs.isArray()) {
        rhs = rhs.getComponentType();
      }

      if (lhs.isPrimitive() && rhs.isPrimitive()) {
        return lhs.getKey().equals(rhs.getKey());
      }
    }
  }

  return TypeRules.canAssign(lhs, rhs);
}
 
Example 6
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 7
Source File: AbstractToStringGenerator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Iterates over selected members to determine whether helper methods will be needed.
 */
protected void checkNeedForHelperMethods() {
	if ((!fContext.isLimitItems() && !fContext.isCustomArray()) || (fContext.isLimitItems() && fContext.getLimitItemsValue() == 0))
		return;

	boolean isNonPrimitive= false;
	for (int i= 0; i < fContext.getSelectedMembers().length; i++) {
		ITypeBinding memberType= getMemberType(fContext.getSelectedMembers()[i]);
		boolean[] implementsInterfaces= implementsInterfaces(memberType.getErasure(), new String[] { "java.util.Collection", "java.util.List", "java.util.Map" }); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
		boolean isCollection= implementsInterfaces[0];
		boolean isList= implementsInterfaces[1];
		boolean isMap= implementsInterfaces[2];

		if (fContext.isLimitItems() && (isCollection || isMap) && !isList) {
			needCollectionToStringMethod= true;
		}
		if (fContext.isCustomArray() && memberType.isArray()) {
			ITypeBinding componentType= memberType.getComponentType();
			if (componentType.isPrimitive() && (!fContext.is50orHigher() || (!fContext.is60orHigher() && fContext.isLimitItems()))) {
				if (!typesThatNeedArrayToStringMethod.contains(componentType))
					typesThatNeedArrayToStringMethod.add(componentType);
			} else if (!componentType.isPrimitive())
				isNonPrimitive= true;
		}
	}
	if (!typesThatNeedArrayToStringMethod.isEmpty() && isNonPrimitive)
		typesThatNeedArrayToStringMethod.add(fAst.resolveWellKnownType("java.lang.Object")); //$NON-NLS-1$
}
 
Example 8
Source File: ASTResolving.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static ITypeBinding getReducedDimensionBinding(ITypeBinding arrayBinding, int dimsToReduce) {
	while (dimsToReduce > 0) {
		arrayBinding= arrayBinding.getComponentType();
		dimsToReduce--;
	}
	return arrayBinding;
}
 
Example 9
Source File: TypeURIHelper.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
protected void createResourceURIForArray(ITypeBinding typeBinding, StringBuilder uriBuilder) {
	ITypeBinding componentType = typeBinding.getComponentType();
	createResourceURI(componentType, uriBuilder);
}
 
Example 10
Source File: Bindings.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 3 votes vote down vote up
/**
 * Returns the n-th component type of the given type, or <code>null</code> if
 * the type binding is not an array type or has not that many dimensions.
 * 
 * @param arrayType an array type binding
 * @param n number of dimensions to cut
 * @return arrayType with n dimensions removed, or <code>null</code>
 * @since 3.10
 */
public static ITypeBinding getComponentType(ITypeBinding arrayType, int n) {
	ITypeBinding type= arrayType;
	while (n > 0 && type != null) {
		type= type.getComponentType();
		n--;
	}
	return type;
}