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

The following examples show how to use org.eclipse.jdt.core.dom.ITypeBinding#getBound() . 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: ChangeSignatureProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private void collectTypeVariables(ITypeBinding typeBinding, Set<ITypeBinding> typeVariablesCollector) {
	if (typeBinding.isTypeVariable()) {
		typeVariablesCollector.add(typeBinding);
		ITypeBinding[] typeBounds= typeBinding.getTypeBounds();
		for (int i= 0; i < typeBounds.length; i++)
			collectTypeVariables(typeBounds[i], typeVariablesCollector);

	} else if (typeBinding.isArray()) {
		collectTypeVariables(typeBinding.getElementType(), typeVariablesCollector);

	} else if (typeBinding.isParameterizedType()) {
		ITypeBinding[] typeArguments= typeBinding.getTypeArguments();
		for (int i= 0; i < typeArguments.length; i++)
			collectTypeVariables(typeArguments[i], typeVariablesCollector);

	} else if (typeBinding.isWildcardType()) {
		ITypeBinding bound= typeBinding.getBound();
		if (bound != null) {
			collectTypeVariables(bound, typeVariablesCollector);
		}
	}
}
 
Example 2
Source File: Bindings.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Normalizes the binding so that it can be used as a type inside a declaration (e.g. variable
 * declaration, method return type, parameter type, ...).
 * For null bindings, java.lang.Object is returned.
 * For void bindings, <code>null</code> is returned.
 * 
 * @param binding binding to normalize
 * @param ast current AST
 * @return the normalized type to be used in declarations, or <code>null</code>
 */
public static ITypeBinding normalizeForDeclarationUse(ITypeBinding binding, AST ast) {
	if (binding.isNullType())
		return ast.resolveWellKnownType("java.lang.Object"); //$NON-NLS-1$
	if (binding.isPrimitive())
		return binding;
	binding= normalizeTypeBinding(binding);
	if (binding == null || !binding.isWildcardType())
		return binding;
	ITypeBinding bound= binding.getBound();
	if (bound == null || !binding.isUpperbound()) {
		ITypeBinding[] typeBounds= binding.getTypeBounds();
		if (typeBounds.length > 0) {
			return typeBounds[0];
		} else {
			return binding.getErasure();
		}
	} else {
		return bound;
	}
}
 
Example 3
Source File: ASTResolving.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Use this method before creating a type for a wildcard. Either to assign a wildcard to a new type or for a type to be assigned.
 *
 * @param wildcardType the wildcard type to normalize
 * @param isBindingToAssign if true, then the type X for new variable x is returned (X x= s);
 *     if false, the type of an expression x (R r= x)
 * @param ast the current AST
 * @return the normalized binding or null when only the 'null' binding
 * 
 * @see Bindings#normalizeForDeclarationUse(ITypeBinding, AST)
 */
public static ITypeBinding normalizeWildcardType(ITypeBinding wildcardType, boolean isBindingToAssign, AST ast) {
	ITypeBinding bound= wildcardType.getBound();
	if (isBindingToAssign) {
		if (bound == null || !wildcardType.isUpperbound()) {
			ITypeBinding[] typeBounds= wildcardType.getTypeBounds();
			if (typeBounds.length > 0) {
				return typeBounds[0];
			} else {
				return wildcardType.getErasure();
			}
		}
	} else {
		if (bound == null || wildcardType.isUpperbound()) {
			return null;
		}
	}
	return bound;
}
 
Example 4
Source File: AbstractPairedInterfaceValidator.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Returns <code>true</code> if the given type binding references a type
 * variable, at any depth.
 */
protected static boolean containsTypeVariableReferences(
    ITypeBinding typeBinding) {
  if (typeBinding.isTypeVariable()) {
    return true;
  }

  if (typeBinding.isArray()) {
    return containsTypeVariableReferences(typeBinding.getComponentType());
  }

  if (typeBinding.isParameterizedType()) {
    ITypeBinding[] typeArguments = typeBinding.getTypeArguments();
    for (int i = 0; i < typeArguments.length; ++i) {
      if (containsTypeVariableReferences(typeArguments[i])) {
        return true;
      }
    }
  }

  if (typeBinding.isWildcardType()) {
    ITypeBinding bound = typeBinding.getBound();
    if (bound != null) {
      return containsTypeVariableReferences(bound);
    }
  }

  return false;
}
 
Example 5
Source File: NewAsyncRemoteServiceInterfaceCreationWizardPage.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 5 votes vote down vote up
static void expandTypeBinding(ITypeBinding typeBinding, StringBuilder sb,
    ImportManagerAdapter importsManager) {

  if (typeBinding.isParameterizedType()) {
    expandTypeBinding(typeBinding.getErasure(), sb, importsManager);

    sb.append("<");
    ITypeBinding[] typeArguments = typeBinding.getTypeArguments();
    for (int i = 0; i < typeArguments.length; ++i) {
      if (i != 0) {
        sb.append(", ");
      }
      expandTypeBinding(typeArguments[i], sb, importsManager);
    }

    sb.append(">");
  } else if (typeBinding.isWildcardType()) {
    ITypeBinding bound = typeBinding.getBound();
    if (bound == null) {
      sb.append("?");
    } else {
      if (typeBinding.isUpperbound()) {
        sb.append("? extends ");
      } else {
        sb.append("? super ");
      }
      expandTypeBinding(bound, sb, importsManager);
    }
  } else if (typeBinding.isTypeVariable()) {
    sb.append(typeBinding.getName());
  } else if (typeBinding.isArray()) {
    expandTypeBinding(typeBinding.getComponentType(), sb, importsManager);
    sb.append("[]");
  } else if (typeBinding.isPrimitive()) {
    sb.append(typeBinding.getName());
  } else {
    sb.append(importsManager.addImport(typeBinding));
  }
}
 
Example 6
Source File: WildcardType.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
protected void initialize(ITypeBinding binding) {
	Assert.isTrue(binding.isWildcardType());
	super.initialize(binding);
	ITypeBinding bound= binding.getBound();
	if (bound != null) {
		fBound= getEnvironment().create(bound);
	}
}
 
Example 7
Source File: TypeEnvironment.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public TType create(ITypeBinding binding) {
	if (binding.isPrimitive()) {
		return createPrimitiveType(binding);
	} else if (binding.isArray()) {
		return createArrayType(binding);
	} else if (binding.isRawType()) {
		return createRawType(binding);
	} else if (binding.isGenericType()) {
		return createGenericType(binding);
	} else if (binding.isParameterizedType()) {
		return createParameterizedType(binding);
	} else if (binding.isTypeVariable()) {
		return createTypeVariable(binding);
	} else if (binding.isWildcardType()) {
		if (binding.getBound() == null) {
			return createUnboundWildcardType(binding);
		} else if (binding.isUpperbound()) {
			return createExtendsWildCardType(binding);
		} else {
			return createSuperWildCardType(binding);
		}
	} else if (binding.isCapture()) {
		if (fRemoveCapures) {
			return create(binding.getWildcard());
		} else {
			return createCaptureType(binding);
		}
	}
	if ("null".equals(binding.getName())) //$NON-NLS-1$
		return NULL;
	return createStandardType(binding);
}
 
Example 8
Source File: Bindings.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
static boolean containsTypeVariables(ITypeBinding type) {
	if (type.isTypeVariable())
		return true;
	if (type.isArray())
		return containsTypeVariables(type.getElementType());
	if (type.isCapture())
		return containsTypeVariables(type.getWildcard());
	if (type.isParameterizedType())
		return containsTypeVariables(type.getTypeArguments());
	if (type.isWildcardType() && type.getBound() != null)
		return containsTypeVariables(type.getBound());
	return false;
}
 
Example 9
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 10
Source File: ASTResolving.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public static boolean isUseableTypeInContext(ITypeBinding type, IBinding context, boolean noWildcards) {
	if (type.isArray()) {
		type= type.getElementType();
	}
	if (type.isAnonymous()) {
		return false;
	}
	if (type.isRawType() || type.isPrimitive()) {
		return true;
	}
	if (type.isTypeVariable()) {
		return isVariableDefinedInContext(context, type);
	}
	if (type.isGenericType()) {
		ITypeBinding[] typeParameters= type.getTypeParameters();
		for (int i= 0; i < typeParameters.length; i++) {
			if (!isUseableTypeInContext(typeParameters[i], context, noWildcards)) {
				return false;
			}
		}
		return true;
	}
	if (type.isParameterizedType()) {
		ITypeBinding[] typeArguments= type.getTypeArguments();
		for (int i= 0; i < typeArguments.length; i++) {
			if (!isUseableTypeInContext(typeArguments[i], context, noWildcards)) {
				return false;
			}
		}
		return true;
	}
	if (type.isCapture()) {
		type= type.getWildcard();
	}

	if (type.isWildcardType()) {
		if (noWildcards) {
			return false;
		}
		if (type.getBound() != null) {
			return isUseableTypeInContext(type.getBound(), context, noWildcards);
		}
	}
	return true;
}