org.eclipse.jdt.core.ITypeParameter Java Examples

The following examples show how to use org.eclipse.jdt.core.ITypeParameter. 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: TypeVariableUtil.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Creates a type variable mapping from a domain to a range.
 *
 * @param domain
 *        the domain of the mapping
 * @param range
 *        the range of the mapping
 * @return a possibly empty type variable mapping
 */
private static TypeVariableMaplet[] signaturesToParameters(final String[] domain, final ITypeParameter[] range) {
	Assert.isNotNull(domain);
	Assert.isNotNull(range);
	Assert.isTrue(domain.length == 0 || domain.length == range.length);

	final List<TypeVariableMaplet> list= new ArrayList<TypeVariableMaplet>();
	String source= null;
	String target= null;
	for (int index= 0; index < domain.length; index++) {
		source= Signature.toString(domain[index]);
		target= range[index].getElementName();
		list.add(new TypeVariableMaplet(source, index, target, index));
	}
	final TypeVariableMaplet[] result= new TypeVariableMaplet[list.size()];
	list.toArray(result);
	return result;
}
 
Example #2
Source File: JDTUtils.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
public static ISourceRange getNameRange(IJavaElement element) throws JavaModelException {
	ISourceRange nameRange = null;
	if (element instanceof IMember) {
		IMember member = (IMember) element;
		nameRange = member.getNameRange();
		if ((!SourceRange.isAvailable(nameRange))) {
			nameRange = member.getSourceRange();
		}
	} else if (element instanceof ITypeParameter || element instanceof ILocalVariable) {
		nameRange = ((ISourceReference) element).getNameRange();
	} else if (element instanceof ISourceReference) {
		nameRange = ((ISourceReference) element).getSourceRange();
	}
	if (!SourceRange.isAvailable(nameRange) && element.getParent() != null) {
		nameRange = getNameRange(element.getParent());
	}
	return nameRange;
}
 
Example #3
Source File: JavaElementLabelComposer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private void appendTypeParameterWithBounds(ITypeParameter typeParameter, long flags) throws JavaModelException {
	fBuffer.append(getElementName(typeParameter));

	if (typeParameter.exists()) {
		String[] bounds= typeParameter.getBoundsSignatures();
		if (bounds.length > 0 &&
				! (bounds.length == 1 && "Ljava.lang.Object;".equals(bounds[0]))) { //$NON-NLS-1$
			fBuffer.append(" extends "); //$NON-NLS-1$
			for (int j= 0; j < bounds.length; j++) {
				if (j > 0) {
					fBuffer.append(" & "); //$NON-NLS-1$
				}
				appendTypeSignatureLabel(typeParameter, bounds[j], flags);
			}
		}
	}
}
 
Example #4
Source File: CompletionProposalReplacementProvider.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
private String computeTypeProposal(ITypeBinding binding, ITypeParameter parameter) throws JavaModelException {
	final String name = TypeProposalUtils.getTypeQualifiedName(binding);
	if (binding.isWildcardType()) {

		if (binding.isUpperbound()) {
			// replace the wildcard ? with the type parameter name to get "E extends Bound" instead of "? extends Bound"
			//				String contextName= name.replaceFirst("\\?", parameter.getElementName()); //$NON-NLS-1$
			// upper bound - the upper bound is the bound itself
			return binding.getBound().getName();
		}

		// no or upper bound - use the type parameter of the inserted type, as it may be more
		// restrictive (eg. List<?> list= new SerializableList<Serializable>())
		return computeTypeProposal(parameter);
	}

	// not a wildcard but a type or type variable - this is unambigously the right thing to insert
	return name;
}
 
Example #5
Source File: SuperInterfaceSelectionDialog.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public static String getNameWithTypeParameters(IType type) {
	String superName= type.getFullyQualifiedName('.');
	if (!JavaModelUtil.is50OrHigher(type.getJavaProject())) {
		return superName;
	}
	try {
		ITypeParameter[] typeParameters= type.getTypeParameters();
		if (typeParameters.length > 0) {
			StringBuffer buf= new StringBuffer(superName);
			buf.append('<');
			for (int k= 0; k < typeParameters.length; k++) {
				if (k != 0) {
					buf.append(',').append(' ');
				}
				buf.append(typeParameters[k].getElementName());
			}
			buf.append('>');
			return buf.toString();
		}
	} catch (JavaModelException e) {
		// ignore
	}
	return superName;

}
 
Example #6
Source File: TypeProposalUtils.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
static int mapTypeParameterIndex(IType[] path, int pathIndex, int paramIndex) throws JavaModelException, ArrayIndexOutOfBoundsException {
	if (pathIndex == 0) {
		// break condition: we've reached the top of the hierarchy
		return paramIndex;
	}

	IType subType= path[pathIndex];
	IType superType= path[pathIndex - 1];

	String superSignature= findMatchingSuperTypeSignature(subType, superType);
	ITypeParameter param= subType.getTypeParameters()[paramIndex];
	int index= findMatchingTypeArgumentIndex(superSignature, param.getElementName());
	if (index == -1) {
		// not mapped through
		return -1;
	}

	return mapTypeParameterIndex(path, pathIndex - 1, index);
}
 
Example #7
Source File: NamedMember.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private void appendTypeParameters(StringBuffer buffer) throws JavaModelException {
	ITypeParameter[] typeParameters = getTypeParameters();
	int length = typeParameters.length;
	if (length == 0) return;
	buffer.append('<');
	for (int i = 0; i < length; i++) {
		ITypeParameter typeParameter = typeParameters[i];
		buffer.append(typeParameter.getElementName());
		String[] bounds = typeParameter.getBounds();
		int boundsLength = bounds.length;
		if (boundsLength > 0) {
			buffer.append(" extends "); //$NON-NLS-1$
			for (int j = 0; j < boundsLength; j++) {
				buffer.append(bounds[j]);
				if (j < boundsLength-1)
					buffer.append(" & "); //$NON-NLS-1$
			}
		}
		if (i < length-1)
			buffer.append(", "); //$NON-NLS-1$
	}
	buffer.append('>');
}
 
Example #8
Source File: MethodOverrideTester.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private boolean hasCompatibleTypeParameters(IMethod overriding, IMethod overridden) throws JavaModelException {
	ITypeParameter[] overriddenTypeParameters= overridden.getTypeParameters();
	ITypeParameter[] overridingTypeParameters= overriding.getTypeParameters();
	int nOverridingTypeParameters= overridingTypeParameters.length;
	if (overriddenTypeParameters.length != nOverridingTypeParameters) {
		return nOverridingTypeParameters == 0;
	}
	Substitutions overriddenSubst= getMethodSubstitions(overridden);
	Substitutions overridingSubst= getMethodSubstitions(overriding);
	for (int i= 0; i < nOverridingTypeParameters; i++) {
		String erasure1= overriddenSubst.getErasure(overriddenTypeParameters[i].getElementName());
		String erasure2= overridingSubst.getErasure(overridingTypeParameters[i].getElementName());
		if (erasure1 == null || !erasure1.equals(erasure2)) {
			return false;
		}
		// comparing only the erasure is not really correct: Need to compare all bounds, that can be in different order
		int nBounds= overriddenTypeParameters[i].getBounds().length;
		if (nBounds > 1 && nBounds != overridingTypeParameters[i].getBounds().length) {
			return false;
		}
	}
	return true;
}
 
Example #9
Source File: JavaMatchFilter.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private static boolean isParameterizedElement(IJavaElement element) {
	while (element != null) {
		ITypeParameter[] typeParameters= null;
		try {
			if (element instanceof IType) {
				typeParameters= ((IType)element).getTypeParameters();
			} else if (element instanceof IMethod) {
				typeParameters= ((IMethod)element).getTypeParameters();
			}
		} catch (JavaModelException e) {
			return false;
		}
		if (typeParameters == null)
			return false;
		
		if (typeParameters.length > 0)
			return true;
		
		element= element.getParent();
	}
	return false;
}
 
Example #10
Source File: StubCreator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
protected void appendTypeParameters(final ITypeParameter[] parameters) throws JavaModelException {
	final int length= parameters.length;
	if (length > 0)
		fBuffer.append("<"); //$NON-NLS-1$
	for (int index= 0; index < length; index++) {
		if (index > 0)
			fBuffer.append(","); //$NON-NLS-1$
		final ITypeParameter parameter= parameters[index];
		fBuffer.append(parameter.getElementName());
		final String[] bounds= parameter.getBounds();
		final int size= bounds.length;
		if (size > 0)
			fBuffer.append(" extends "); //$NON-NLS-1$
		for (int offset= 0; offset < size; offset++) {
			if (offset > 0)
				fBuffer.append(" & "); //$NON-NLS-1$
			fBuffer.append(bounds[offset]);
		}
	}
	if (length > 0)
		fBuffer.append(">"); //$NON-NLS-1$
}
 
Example #11
Source File: UtilitiesTest.java    From sarl with Apache License 2.0 6 votes vote down vote up
@Test
public void getNameWithTypeParameters_oneTypeParameter() throws Exception {
	IJavaProject project = Mockito.mock(IJavaProject.class);
	Mockito.when(project.getOption(ArgumentMatchers.anyString(), ArgumentMatchers.anyBoolean())).thenReturn(JavaCore.VERSION_1_8);
	//
	ITypeParameter typeParameter = Mockito.mock(ITypeParameter.class);
	Mockito.when(typeParameter.getElementName()).thenReturn("io.sarl.eclipse.tests.FakeObjectParameter");
	//
	IType type = Mockito.mock(IType.class);
	Mockito.when(type.getJavaProject()).thenReturn(project);
	Mockito.when(type.getFullyQualifiedName(ArgumentMatchers.anyChar())).thenReturn("io.sarl.eclipse.tests.FakeObject");
	Mockito.when(type.getTypeParameters()).thenReturn(new ITypeParameter[] {typeParameter});
	//
	String name = Utilities.getNameWithTypeParameters(type);
	assertNotNull(name);
	assertEquals("io.sarl.eclipse.tests.FakeObject<io.sarl.eclipse.tests.FakeObjectParameter>", name);
}
 
Example #12
Source File: SelectionRequestor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public void acceptLocalMethodTypeParameter(TypeVariableBinding typeVariableBinding) {
	MethodBinding methodBinding = (MethodBinding)typeVariableBinding.declaringElement;
	IJavaElement res = findLocalElement(methodBinding.sourceStart());
	if(res != null && res.getElementType() == IJavaElement.METHOD) {
		IMethod method = (IMethod) res;

		ITypeParameter typeParameter = method.getTypeParameter(new String(typeVariableBinding.sourceName));
		if (typeParameter.exists()) {
			addElement(typeParameter);
			if(SelectionEngine.DEBUG){
				System.out.print("SELECTION - accept type parameter("); //$NON-NLS-1$
				System.out.print(typeParameter.toString());
				System.out.println(")"); //$NON-NLS-1$
			}
		}
	}
}
 
Example #13
Source File: MethodProposalInfo.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * The type and method signatures received in
 * <code>CompletionProposals</code> of type <code>METHOD_REF</code>
 * contain concrete type bounds. When comparing parameters of the signature
 * with an <code>IMethod</code>, we have to make sure that we match the
 * case where the formal method declaration uses a type variable which in
 * the signature is already substituted with a concrete type (bound).
 * <p>
 * This method creates a map from type variable names to type signatures
 * based on the position they appear in the type declaration. The type
 * signatures are filtered through
 * {@link SignatureUtil#getLowerBound(char[])}.
 * </p>
 *
 * @param type the type to get the variables from
 * @return a map from type variables to concrete type signatures
 * @throws JavaModelException if accessing the java model fails
 */
private Map<String, char[]> computeTypeVariables(IType type) throws JavaModelException {
	Map<String, char[]> map= new HashMap<String, char[]>();
	char[] declarationSignature= fProposal.getDeclarationSignature();
	if (declarationSignature == null) // array methods don't contain a declaration signature
		return map;
	char[][] concreteParameters= Signature.getTypeArguments(declarationSignature);

	ITypeParameter[] typeParameters= type.getTypeParameters();
	for (int i= 0; i < typeParameters.length; i++) {
		String variable= typeParameters[i].getElementName();
		if (concreteParameters.length > i)
			// use lower bound since method equality is only parameter based
			map.put(variable, SignatureUtil.getLowerBound(concreteParameters[i]));
		else
			// fProposal.getDeclarationSignature() is a raw type - use Object
			map.put(variable, "Ljava.lang.Object;".toCharArray()); //$NON-NLS-1$
	}

	return map;
}
 
Example #14
Source File: JavaElementLabelComposer.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Appends the styled label for a type parameter.
 *
 * @param typeParameter the element to render
 * @param flags the rendering flags. Flags with names starting with 'T_' are considered.
 */
public void appendTypeParameterLabel(ITypeParameter typeParameter, long flags) {
	try {
		appendTypeParameterWithBounds(typeParameter, flags);

		// post qualification
		if (getFlag(flags, JavaElementLabels.TP_POST_QUALIFIED)) {
			fBuilder.append(JavaElementLabels.CONCAT_STRING);
			IMember declaringMember= typeParameter.getDeclaringMember();
			appendElementLabel(declaringMember, JavaElementLabels.M_PARAMETER_TYPES | JavaElementLabels.M_FULLY_QUALIFIED | JavaElementLabels.T_FULLY_QUALIFIED | (flags & QUALIFIER_FLAGS));
		}

	} catch (JavaModelException e) {
		JavaLanguageServerPlugin.logException("", e); // NotExistsException will not reach this point
	}
}
 
Example #15
Source File: JavaElementLabelComposer.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
private void appendTypeParameterWithBounds(ITypeParameter typeParameter, long flags) throws JavaModelException {
	fBuilder.append(getElementName(typeParameter));

	if (typeParameter.exists()) {
		String[] bounds= typeParameter.getBoundsSignatures();
		if (bounds.length > 0 &&
				! (bounds.length == 1 && "Ljava.lang.Object;".equals(bounds[0]))) { //$NON-NLS-1$
			fBuilder.append(" extends "); //$NON-NLS-1$
			for (int j= 0; j < bounds.length; j++) {
				if (j > 0) {
					fBuilder.append(" & "); //$NON-NLS-1$
				}
				appendTypeSignatureLabel(typeParameter, bounds[j], flags);
			}
		}
	}
}
 
Example #16
Source File: TypeVariableUtil.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Returns a type variable mapping from a superclass to a subclass.
 *
 * @param supertype
 *        the type representing the superclass
 * @param subtype
 *        the type representing the subclass
 * @return a type variable mapping. The mapping entries consist of simple type variable names.
 * @throws JavaModelException
 *         if the signature of one of the types involved could not be retrieved
 */
public static TypeVariableMaplet[] superTypeToInheritedType(final IType supertype, final IType subtype) throws JavaModelException {
	Assert.isNotNull(subtype);
	Assert.isNotNull(supertype);

	final ITypeParameter[] domain= supertype.getTypeParameters();
	if (domain.length > 0) {
		final String signature= subtype.getSuperclassTypeSignature();
		if (signature != null) {
			final String[] range= getVariableSignatures(signature);
			if (range.length > 0)
				return parametersToSignatures(domain, range, true);
		}
	}
	return new TypeVariableMaplet[0];
}
 
Example #17
Source File: SelectionRequestor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public void acceptLocalTypeParameter(TypeVariableBinding typeVariableBinding) {
	IJavaElement res;
	if(typeVariableBinding.declaringElement instanceof ParameterizedTypeBinding) {
		LocalTypeBinding localTypeBinding = (LocalTypeBinding)((ParameterizedTypeBinding)typeVariableBinding.declaringElement).genericType();
		res = findLocalElement(localTypeBinding.sourceStart());
	} else {
		SourceTypeBinding typeBinding = (SourceTypeBinding)typeVariableBinding.declaringElement;
		res = findLocalElement(typeBinding.sourceStart());
	}
	if (res != null && res.getElementType() == IJavaElement.TYPE) {
		IType type = (IType) res;
		ITypeParameter typeParameter = type.getTypeParameter(new String(typeVariableBinding.sourceName));
		if (typeParameter.exists()) {
			addElement(typeParameter);
			if(SelectionEngine.DEBUG){
				System.out.print("SELECTION - accept type parameter("); //$NON-NLS-1$
				System.out.print(typeParameter.toString());
				System.out.println(")"); //$NON-NLS-1$
			}
		}
	}
}
 
Example #18
Source File: TypeVariableUtil.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Returns a type variable mapping from a subclass to a superclass.
 *
 * @param subtype
 *        the type representing the subclass
 * @param supertype
 *        the type representing the superclass
 * @return a type variable mapping. The mapping entries consist of simple type variable names.
 * @throws JavaModelException
 *         if the signature of one of the types involved could not be retrieved
 */
public static TypeVariableMaplet[] subTypeToSuperType(final IType subtype, final IType supertype) throws JavaModelException {
	Assert.isNotNull(subtype);
	Assert.isNotNull(supertype);

	final TypeVariableMaplet[] mapping= subTypeToInheritedType(subtype);
	if (mapping.length > 0) {
		final ITypeParameter[] range= supertype.getTypeParameters();
		if (range.length > 0) {
			final String signature= subtype.getSuperclassTypeSignature();
			if (signature != null) {
				final String[] domain= getVariableSignatures(signature);
				if (domain.length > 0)
					return composeMappings(mapping, signaturesToParameters(domain, range));
			}
		}
	}
	return mapping;
}
 
Example #19
Source File: UtilitiesTest.java    From sarl with Apache License 2.0 6 votes vote down vote up
@Test
public void getNameWithTypeParameters_twoTypeParameters() throws Exception {
	IJavaProject project = Mockito.mock(IJavaProject.class);
	Mockito.when(project.getOption(ArgumentMatchers.anyString(), ArgumentMatchers.anyBoolean())).thenReturn(JavaCore.VERSION_1_8);
	//
	ITypeParameter typeParameter1 = Mockito.mock(ITypeParameter.class);
	Mockito.when(typeParameter1.getElementName()).thenReturn("io.sarl.eclipse.tests.FakeObjectParameter1");
	//
	ITypeParameter typeParameter2 = Mockito.mock(ITypeParameter.class);
	Mockito.when(typeParameter2.getElementName()).thenReturn("io.sarl.eclipse.tests.FakeObjectParameter2");
	//
	IType type = Mockito.mock(IType.class);
	Mockito.when(type.getJavaProject()).thenReturn(project);
	Mockito.when(type.getFullyQualifiedName(ArgumentMatchers.anyChar())).thenReturn("io.sarl.eclipse.tests.FakeObject");
	Mockito.when(type.getTypeParameters()).thenReturn(new ITypeParameter[] {typeParameter1, typeParameter2});
	//
	String name = Utilities.getNameWithTypeParameters(type);
	assertNotNull(name);
	assertEquals("io.sarl.eclipse.tests.FakeObject<io.sarl.eclipse.tests.FakeObjectParameter1, io.sarl.eclipse.tests.FakeObjectParameter2>", name);
}
 
Example #20
Source File: TypeParameterPattern.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * @param findDeclarations
 * @param findReferences
 * @param typeParameter
 * @param matchRule
 */
public TypeParameterPattern(boolean findDeclarations, boolean findReferences, ITypeParameter typeParameter, int matchRule) {
	super(TYPE_PARAM_PATTERN, matchRule);

	this.findDeclarations = findDeclarations; // set to find declarations & all occurences
	this.findReferences = findReferences; // set to find references & all occurences
	this.typeParameter = typeParameter;
	this.name = typeParameter.getElementName().toCharArray(); // store type parameter name
	IMember member = typeParameter.getDeclaringMember();
	this.declaringMemberName = member.getElementName().toCharArray(); // store type parameter declaring member name

	// For method type parameter, store also declaring class name and parameters type names
	if (member instanceof IMethod) {
		IMethod method = (IMethod) member;
		this.methodDeclaringClassName = method.getParent().getElementName().toCharArray();
		String[] parameters = method.getParameterTypes();
		int length = parameters.length;
		this.methodArgumentTypes = new char[length][];
		for (int i=0; i<length; i++) {
			this.methodArgumentTypes[i] = Signature.toCharArray(parameters[i].toCharArray());
		}
	}
}
 
Example #21
Source File: TypeContextChecker.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private int appendMethodDeclaration(StringBuffer cuString, String[] types, int parameterCount) throws JavaModelException {
	int flags= fMethod.getFlags();
	if (Flags.isStatic(flags)) {
		cuString.append("static "); //$NON-NLS-1$
	} else if (Flags.isDefaultMethod(flags)) {
		cuString.append("default "); //$NON-NLS-1$
	}

	ITypeParameter[] methodTypeParameters= fMethod.getTypeParameters();
	if (methodTypeParameters.length != 0) {
		cuString.append('<');
		for (int i= 0; i < methodTypeParameters.length; i++) {
			ITypeParameter typeParameter= methodTypeParameters[i];
			if (i > 0)
				cuString.append(',');
			cuString.append(typeParameter.getElementName());
		}
		cuString.append("> "); //$NON-NLS-1$
	}

	cuString.append(types[parameterCount]).append(' ');
	int offsetBeforeMethodName= cuString.length();
	cuString.append(METHOD_NAME).append('(');
	for (int i= 0; i < parameterCount; i++) {
		if (i > 0)
			cuString.append(',');
		cuString.append(types[i]).append(" p").append(i); //$NON-NLS-1$
	}
	cuString.append(");"); //$NON-NLS-1$

	return offsetBeforeMethodName;
}
 
Example #22
Source File: LazyGenericTypeProposal.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private String computeTypeParameterDisplayName(ITypeParameter parameter, String[] bounds) {
	if (bounds.length == 0 || bounds.length == 1 && "java.lang.Object".equals(bounds[0])) //$NON-NLS-1$
		return parameter.getElementName();
	StringBuffer buf= new StringBuffer(parameter.getElementName());
	buf.append(" extends "); //$NON-NLS-1$
	for (int i= 0; i < bounds.length; i++) {
		buf.append(Signature.getSimpleName(bounds[i]));
		if (i < bounds.length - 1)
			buf.append(" & "); //$NON-NLS-1$
	}
	return buf.toString();
}
 
Example #23
Source File: RefactoringExecutionStarter.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static RenameSupport createRenameSupport(IJavaElement element, String newName, int flags) throws CoreException {
	switch (element.getElementType()) {
		case IJavaElement.JAVA_PROJECT:
			return RenameSupport.create((IJavaProject) element, newName, flags);
		case IJavaElement.PACKAGE_FRAGMENT_ROOT:
			return RenameSupport.create((IPackageFragmentRoot) element, newName);
		case IJavaElement.PACKAGE_FRAGMENT:
			return RenameSupport.create((IPackageFragment) element, newName, flags);
		case IJavaElement.COMPILATION_UNIT:
			return RenameSupport.create((ICompilationUnit) element, newName, flags);
		case IJavaElement.TYPE:
			return RenameSupport.create((IType) element, newName, flags);
		case IJavaElement.METHOD:
			final IMethod method= (IMethod) element;
			if (method.isConstructor())
				return createRenameSupport(method.getDeclaringType(), newName, flags);
			else
				return RenameSupport.create((IMethod) element, newName, flags);
		case IJavaElement.FIELD:
			return RenameSupport.create((IField) element, newName, flags);
		case IJavaElement.TYPE_PARAMETER:
			return RenameSupport.create((ITypeParameter) element, newName, flags);
		case IJavaElement.LOCAL_VARIABLE:
			return RenameSupport.create((ILocalVariable) element, newName, flags);
	}
	return null;
}
 
Example #24
Source File: GenericRefactoringHandleTransplanter.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
protected ITypeParameter transplantHandle(IMember parent, ITypeParameter element) {
	switch (parent.getElementType()) {
		case IJavaElement.TYPE:
			return ((IType) parent).getTypeParameter(element.getElementName());
		case IJavaElement.METHOD:
			return ((IMethod) parent).getTypeParameter(element.getElementName());
		default:
			throw new IllegalStateException(element.toString());
	}
}
 
Example #25
Source File: TypeEnvironment.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private TypeVariable createTypeVariable(ITypeBinding binding) {
	IJavaElement javaElement= binding.getJavaElement();
	TypeVariable result= fTypeVariables.get(javaElement);
	if (result != null)
		return result;
	result= new TypeVariable(this);
	fTypeVariables.put(javaElement, result);
	result.initialize(binding, (ITypeParameter)javaElement);
	return result;
}
 
Example #26
Source File: TypeVariableUtil.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Returns a type variable mapping from a subclass to a superclass.
 *
 * @param type
 *        the type representing the subclass class
 * @return a type variable mapping. The mapping entries consist of simple type variable names.
 * @throws JavaModelException
 *         if the signature of one of the types involved could not be retrieved
 */
public static TypeVariableMaplet[] subTypeToInheritedType(final IType type) throws JavaModelException {
	Assert.isNotNull(type);

	final ITypeParameter[] domain= type.getTypeParameters();
	if (domain.length > 0) {
		final String signature= type.getSuperclassTypeSignature();
		if (signature != null) {
			final String[] range= getVariableSignatures(signature);
			if (range.length > 0)
				return parametersToSignatures(domain, range, false);
		}
	}
	return new TypeVariableMaplet[0];
}
 
Example #27
Source File: AbstractInfoView.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Computes the contents description that will be displayed for the current element.
 *
 * @param part the part that triggered the current element update, or <code>null</code>
 * @param selection the new selection, or <code>null</code>
 * @param inputElement the new java element that will be displayed
 * @param localASTMonitor a progress monitor
 * @return the contents description for the provided <code>inputElement</code>
 * @since 3.4
 */
protected String computeDescription(IWorkbenchPart part, ISelection selection, IJavaElement inputElement, IProgressMonitor localASTMonitor) {
	long flags;
	if (inputElement instanceof ILocalVariable)
		flags= LOCAL_VARIABLE_TITLE_FLAGS;
	else if (inputElement instanceof ITypeParameter)
		flags= TYPE_PARAMETER_TITLE_FLAGS;
	else
		flags= TITLE_FLAGS;

	return JavaElementLabels.getElementLabel(inputElement, flags);
}
 
Example #28
Source File: TypeVariableUtil.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Converts the specified type parameters to type variable names.
 *
 * @param parameters
 *        the type parameters to convert.
 * @return the converted type variable names
 * @see ITypeParameter#getElementName()
 */
private static String[] parametersToVariables(final ITypeParameter[] parameters) {
	Assert.isNotNull(parameters);

	String[] result= new String[parameters.length];
	for (int index= 0; index < parameters.length; index++)
		result[index]= parameters[index].getElementName();

	return result;
}
 
Example #29
Source File: TypeVariableUtil.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Creates a type variable mapping from a domain to a range.
 *
 * @param domain
 *        the domain of the mapping
 * @param range
 *        the range of the mapping
 * @param indexes
 *        <code>true</code> if the indexes should be compared, <code>false</code> if the names should be compared
 * @return a possibly empty type variable mapping
 */
private static TypeVariableMaplet[] parametersToSignatures(final ITypeParameter[] domain, final String[] range, final boolean indexes) {
	Assert.isNotNull(domain);
	Assert.isNotNull(range);

	final Set<TypeVariableMaplet> set= new HashSet<TypeVariableMaplet>();
	ITypeParameter source= null;
	String target= null;
	String element= null;
	String signature= null;
	for (int index= 0; index < domain.length; index++) {
		source= domain[index];
		for (int offset= 0; offset < range.length; offset++) {
			target= range[offset];
			element= source.getElementName();
			signature= Signature.toString(target);
			if (indexes) {
				if (offset == index)
					set.add(new TypeVariableMaplet(element, index, signature, offset));
			} else {
				if (element.equals(signature))
					set.add(new TypeVariableMaplet(element, index, signature, offset));
			}
		}
	}
	final TypeVariableMaplet[] result= new TypeVariableMaplet[set.size()];
	set.toArray(result);
	return result;
}
 
Example #30
Source File: BinaryMethod.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public ITypeParameter[] getTypeParameters() throws JavaModelException {
	String[] typeParameterSignatures = getTypeParameterSignatures();
	int length = typeParameterSignatures.length;
	if (length == 0) return TypeParameter.NO_TYPE_PARAMETERS;
	ITypeParameter[] typeParameters = new ITypeParameter[length];
	for (int i = 0; i < typeParameterSignatures.length; i++) {
		String typeParameterName = Signature.getTypeVariable(typeParameterSignatures[i]);
		typeParameters[i] = new TypeParameter(this, typeParameterName);
	}
	return typeParameters;
}