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

The following examples show how to use org.eclipse.jdt.core.dom.ITypeBinding#getTypeBounds() . 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: IntroduceIndirectionRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private ITypeBinding getExpressionType(MethodInvocation invocation) {
	Expression expression= invocation.getExpression();
	ITypeBinding typeBinding= null;
	if (expression == null) {
		typeBinding= invocation.resolveMethodBinding().getDeclaringClass();
	} else {
		typeBinding= expression.resolveTypeBinding();
	}
	if (typeBinding != null && typeBinding.isTypeVariable()) {
		ITypeBinding[] typeBounds= typeBinding.getTypeBounds();
		if (typeBounds.length > 0) {
			for (ITypeBinding typeBound : typeBounds) {
				ITypeBinding expressionType= getExpressionType(invocation, typeBound);
				if (expressionType != null) {
					return expressionType;
				}
			}
			typeBinding= typeBounds[0].getTypeDeclaration();
		} else {
			typeBinding= invocation.getAST().resolveWellKnownType("java.lang.Object"); //$NON-NLS-1$
		}
	}
	Assert.isNotNull(typeBinding, "Type binding of target expression may not be null"); //$NON-NLS-1$
	return typeBinding;
}
 
Example 3
Source File: AbstractTypeVariable.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@Override
protected void initialize(ITypeBinding binding) {
	super.initialize(binding);
	ITypeBinding[] bounds= binding.getTypeBounds();
	if (bounds.length == 0) {
		fBounds= EMPTY_TYPE_ARRAY;
		if (getEnvironment().getJavaLangObject() == null) {
			getEnvironment().initializeJavaLangObject(binding.getErasure());
		}
	} else {
		fBounds= new TType[bounds.length];
		for (int i= 0; i < bounds.length; i++) {
			fBounds[i]= getEnvironment().create(bounds[i]);
		}
	}
}
 
Example 4
Source File: Bindings.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private static Set<ITypeBinding> getTypeBoundsForSubsignature(ITypeBinding typeParameter) {
	ITypeBinding[] typeBounds= typeParameter.getTypeBounds();
	int count= typeBounds.length;
	if (count == 0)
		return Collections.emptySet();

	Set<ITypeBinding> result= new HashSet<ITypeBinding>(typeBounds.length);
	for (int i= 0; i < typeBounds.length; i++) {
		ITypeBinding bound= typeBounds[i];
		if ("java.lang.Object".equals(typeBounds[0].getQualifiedName())) //$NON-NLS-1$
			continue;
		else if (containsTypeVariables(bound))
			result.add(bound.getErasure()); // try to achieve effect of "rename type variables"
		else if (bound.isRawType())
			result.add(bound.getTypeDeclaration());
		else
			result.add(bound);
	}
	return result;
}
 
Example 5
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 6
Source File: StubUtility2.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private static void createTypeParameters(ImportRewrite imports, ImportRewriteContext context, AST ast, IMethodBinding binding, MethodDeclaration decl) {
	ITypeBinding[] typeParams= binding.getTypeParameters();
	List<TypeParameter> typeParameters= decl.typeParameters();
	for (int i= 0; i < typeParams.length; i++) {
		ITypeBinding curr= typeParams[i];
		TypeParameter newTypeParam= ast.newTypeParameter();
		newTypeParam.setName(ast.newSimpleName(curr.getName()));
		ITypeBinding[] typeBounds= curr.getTypeBounds();
		if (typeBounds.length != 1 || !"java.lang.Object".equals(typeBounds[0].getQualifiedName())) {//$NON-NLS-1$
			List<Type> newTypeBounds= newTypeParam.typeBounds();
			for (int k= 0; k < typeBounds.length; k++) {
				newTypeBounds.add(imports.addImport(typeBounds[k], ast, context));
			}
		}
		typeParameters.add(newTypeParam);
	}
}
 
Example 7
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 8
Source File: JdtUtils.java    From j2cl with Apache License 2.0 5 votes vote down vote up
private static TypeDescriptor getTypeBound(ITypeBinding typeBinding) {
  ITypeBinding[] bounds = typeBinding.getTypeBounds();
  if (bounds == null || bounds.length == 0) {
    return TypeDescriptors.get().javaLangObject;
  }
  if (bounds.length == 1) {
    return createTypeDescriptor(bounds[0]);
  }
  return IntersectionTypeDescriptor.newBuilder()
      .setIntersectionTypeDescriptors(createTypeDescriptors(bounds, DeclaredTypeDescriptor.class))
      .build();
}
 
Example 9
Source File: ImplementationCollector.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private static IType getType(ITypeBinding typeBinding) {
	if (typeBinding == null) {
		return null;
	}
	if (typeBinding.isTypeVariable()) {
		ITypeBinding[] typeBounds= typeBinding.getTypeBounds();
		if (typeBounds.length > 0) {
			typeBinding= typeBounds[0].getTypeDeclaration();
		} else {
			return null;
		}
	}
	return (IType) typeBinding.getJavaElement();
}
 
Example 10
Source File: IntroduceIndirectionRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private void copyTypeParameters(MethodDeclaration intermediary, CompilationUnitRewrite rew) {
	ITypeBinding[] typeParameters= fTargetMethodBinding.getTypeParameters();
	for (int i= 0; i < typeParameters.length; i++) {
		ITypeBinding current= typeParameters[i];

		TypeParameter parameter= rew.getAST().newTypeParameter();
		parameter.setName(rew.getAST().newSimpleName(current.getName()));
		ITypeBinding[] bounds= current.getTypeBounds();
		for (int j= 0; j < bounds.length; j++)
			if (!"java.lang.Object".equals(bounds[j].getQualifiedName())) //$NON-NLS-1$
				parameter.typeBounds().add(rew.getImportRewrite().addImport(bounds[j], rew.getAST()));

		intermediary.typeParameters().add(parameter);
	}
}
 
Example 11
Source File: StubUtility2.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static void getDelegatableMethods(List<IMethodBinding> methods, IVariableBinding fieldBinding, ITypeBinding typeBinding, ITypeBinding binding, List<DelegateEntry> result) {
	boolean match= false;
	if (typeBinding.isTypeVariable()) {
		ITypeBinding[] typeBounds= typeBinding.getTypeBounds();
		if (typeBounds.length > 0) {
			for (int i= 0; i < typeBounds.length; i++) {
				getDelegatableMethods(methods, fieldBinding, typeBounds[i], binding, result);
			}
		} else {
			ITypeBinding objectBinding= Bindings.findTypeInHierarchy(binding, "java.lang.Object"); //$NON-NLS-1$
			if (objectBinding != null) {
				getDelegatableMethods(methods, fieldBinding, objectBinding, binding, result);
			}
		}
	} else {
		IMethodBinding[] candidates= getDelegateCandidates(typeBinding, binding);
		for (int index= 0; index < candidates.length; index++) {
			match= false;
			final IMethodBinding methodBinding= candidates[index];
			for (int offset= 0; offset < methods.size() && !match; offset++) {
				if (Bindings.areOverriddenMethods(methods.get(offset), methodBinding))
					match= true;
			}
			if (!match) {
				result.add(new DelegateEntry(methodBinding, fieldBinding));
				methods.add(methodBinding);
			}
		}
		final ITypeBinding superclass= typeBinding.getSuperclass();
		if (superclass != null)
			getDelegatableMethods(methods, fieldBinding, superclass, binding, result);
		ITypeBinding[] superInterfaces= typeBinding.getInterfaces();
		for (int offset= 0; offset < superInterfaces.length; offset++)
			getDelegatableMethods(methods, fieldBinding, superInterfaces[offset], binding, result);
	}
}
 
Example 12
Source File: NewAsyncRemoteServiceInterfaceCreationWizardPage.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 4 votes vote down vote up
public static String createMethodContents(IType newType,
    ImportManagerAdapter imports, IMethodBinding overridableSyncMethod,
    boolean addComments) throws CoreException, JavaModelException {
  StringBuilder sb = new StringBuilder();

  IMethod syncMethod = (IMethod) overridableSyncMethod.getJavaElement();

  if (addComments) {
    String lineDelimiter = "\n"; // OK, since content is formatted afterwards

    // Don't go through CodeGeneration type since it can't deal with delegates
    String comment = StubUtility.getMethodComment(
        newType.getCompilationUnit(), newType.getFullyQualifiedName(),
        syncMethod.getElementName(), NO_STRINGS, NO_STRINGS,
        Signature.SIG_VOID, NO_STRINGS, syncMethod, true, lineDelimiter);
    if (comment != null) {
      sb.append(comment);
      sb.append(lineDelimiter);
    }
  }

  // Expand the type parameters
  ITypeParameter[] typeParameters = syncMethod.getTypeParameters();
  ITypeBinding[] typeParameterBindings = overridableSyncMethod.getTypeParameters();
  if (typeParameters.length > 0) {
    sb.append("<");
    for (int i = 0; i < typeParameters.length; ++i) {
      sb.append(typeParameters[i].getElementName());
      ITypeBinding typeParameterBinding = typeParameterBindings[i];
      ITypeBinding[] typeBounds = typeParameterBinding.getTypeBounds();
      if (typeBounds.length > 0) {
        sb.append(" extends ");
        for (int j = 0; j < typeBounds.length; ++j) {
          if (j != 0) {
            sb.append(" & ");
          }
          expandTypeBinding(typeBounds[j], sb, imports);
        }
      }
    }
    sb.append(">");
  }

  // Default to a void return type for the async method
  sb.append("void ");

  // Expand the method name
  sb.append(overridableSyncMethod.getName());

  // Expand the arguments
  sb.append("(");
  String[] parameterNames = syncMethod.getParameterNames();
  ITypeBinding[] parameterTypes = overridableSyncMethod.getParameterTypes();
  for (int i = 0; i < parameterNames.length; ++i) {
    if (i != 0) {
      sb.append(", ");
    }

    expandTypeBinding(parameterTypes[i], sb, imports);
    sb.append(" ");
    sb.append(parameterNames[i]);
  }

  if (parameterNames.length > 0) {
    sb.append(", ");
  }

  sb.append(imports.addImport(RemoteServiceUtilities.ASYNCCALLBACK_QUALIFIED_NAME));
  sb.append("<");
  ITypeBinding syncReturnType = overridableSyncMethod.getReturnType();
  if (syncReturnType.isPrimitive()) {
    String wrapperTypeName = JavaASTUtils.getWrapperTypeName(syncReturnType.getName());
    sb.append(imports.addImport(wrapperTypeName));
  } else {
    expandTypeBinding(syncReturnType, sb, imports);
  }
  sb.append("> ");
  sb.append(StringUtilities.computeUniqueName(parameterNames, "callback"));
  sb.append(");");

  return sb.toString();
}