use of org.eclipse.jdt.core.ITypeParameter in project flux by eclipse.
the class MethodOverrideTester method computeSubstitutions.
private void computeSubstitutions(IType instantiatedType, IType instantiatingType, String[] typeArguments) throws JavaModelException {
Substitutions s = new Substitutions();
fTypeVariableSubstitutions.put(instantiatedType, s);
ITypeParameter[] typeParameters = instantiatedType.getTypeParameters();
if (instantiatingType == null) {
// the focus type
for (int i = 0; i < typeParameters.length; i++) {
ITypeParameter curr = typeParameters[i];
// use star to make type variables different from type refs
s.addSubstitution(curr.getElementName(), '*' + curr.getElementName(), getTypeParameterErasure(curr, instantiatedType));
}
} else {
if (typeParameters.length == typeArguments.length) {
for (int i = 0; i < typeParameters.length; i++) {
ITypeParameter curr = typeParameters[i];
// substitute in the context of the instantiatingType
String substString = getSubstitutedTypeName(typeArguments[i], instantiatingType);
// get the erasure from the type argument
String erasure = getErasedTypeName(typeArguments[i], instantiatingType);
s.addSubstitution(curr.getElementName(), substString, erasure);
}
} else if (typeArguments.length == 0) {
// raw type reference
for (int i = 0; i < typeParameters.length; i++) {
ITypeParameter curr = typeParameters[i];
String erasure = getTypeParameterErasure(curr, instantiatedType);
s.addSubstitution(curr.getElementName(), erasure, erasure);
}
} else {
// code with errors
}
}
String superclassTypeSignature = instantiatedType.getSuperclassTypeSignature();
if (superclassTypeSignature != null) {
String[] superTypeArguments = Signature.getTypeArguments(superclassTypeSignature);
IType superclass = fHierarchy.getSuperclass(instantiatedType);
if (superclass != null && !fTypeVariableSubstitutions.containsKey(superclass)) {
computeSubstitutions(superclass, instantiatedType, superTypeArguments);
}
}
String[] superInterfacesTypeSignature;
if (instantiatedType.isAnonymous()) {
// special case: superinterface is also returned by IType#getSuperclassTypeSignature()
superInterfacesTypeSignature = new String[] { superclassTypeSignature };
} else {
superInterfacesTypeSignature = instantiatedType.getSuperInterfaceTypeSignatures();
}
int nInterfaces = superInterfacesTypeSignature.length;
if (nInterfaces > 0) {
IType[] superInterfaces = fHierarchy.getSuperInterfaces(instantiatedType);
if (superInterfaces.length == nInterfaces) {
for (int i = 0; i < nInterfaces; i++) {
String[] superTypeArguments = Signature.getTypeArguments(superInterfacesTypeSignature[i]);
IType superInterface = superInterfaces[i];
if (!fTypeVariableSubstitutions.containsKey(superInterface)) {
computeSubstitutions(superInterface, instantiatedType, superTypeArguments);
}
}
}
}
}
use of org.eclipse.jdt.core.ITypeParameter in project flux by eclipse.
the class TypeProposalUtils method mapTypeParameterIndex.
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);
}
use of org.eclipse.jdt.core.ITypeParameter in project che by eclipse.
the class JavaElementLinks method parseURI.
public static IJavaElement parseURI(String ssp, JavaProject project) {
// String ssp= uri.getSchemeSpecificPart();
String[] segments = ssp.split(String.valueOf(LINK_SEPARATOR));
// replace '[' manually, since URI confuses it for an IPv6 address as per RFC 2732:
IJavaElement element = JavaCore.create(segments[1].replace(LINK_BRACKET_REPLACEMENT, '['));
if (segments.length > 2) {
String refTypeName = segments[2];
if (refTypeName.indexOf('.') == -1) {
try {
ITypeParameter resolvedTypeVariable = resolveTypeVariable(element, refTypeName);
if (resolvedTypeVariable != null)
return resolvedTypeVariable;
} catch (JavaModelException e) {
LOG.error(e.getMessage(), e);
}
}
if (element instanceof IAnnotation) {
element = element.getParent();
}
if (element instanceof ILocalVariable) {
element = ((ILocalVariable) element).getDeclaringMember();
} else if (element instanceof ITypeParameter) {
element = ((ITypeParameter) element).getDeclaringMember();
}
if (element instanceof IMember && !(element instanceof IType)) {
element = ((IMember) element).getDeclaringType();
}
if (element instanceof IPackageFragment) {
try {
IPackageFragment root = (IPackageFragment) element;
element = resolvePackageInfoType(root, refTypeName);
if (element == null) {
// find it as package
IJavaProject javaProject = root.getJavaProject();
return JavaModelUtil.findTypeContainer(javaProject, refTypeName);
}
} catch (JavaModelException e) {
LOG.error(e.getMessage(), e);
}
}
if (element instanceof IType) {
try {
IType type = (IType) element;
if (refTypeName.length() > 0) {
type = resolveType(type, refTypeName);
if (type == null) {
IPackageFragment pack = JavaModelUtil.getPackageFragmentRoot(element).getPackageFragment(refTypeName);
if (pack.exists())
return pack;
}
}
if (type != null) {
element = type;
if (segments.length > 3) {
String refMemberName = segments[3];
if (segments.length > 4) {
String[] paramSignatures = new String[segments[4].length() == 0 ? 0 : segments.length - 4];
for (int i = 0; i < paramSignatures.length; i++) {
paramSignatures[i] = Signature.createTypeSignature(segments[i + 4], false);
}
IMethod method = type.getMethod(refMemberName, paramSignatures);
IMethod[] methods = type.findMethods(method);
if (methods != null) {
return methods[0];
} else {
//TODO: methods whose signature contains type parameters can not be found
// easily, since the Javadoc references are erasures
//Shortcut: only check name and parameter count:
methods = type.getMethods();
for (int i = 0; i < methods.length; i++) {
method = methods[i];
if (method.getElementName().equals(refMemberName) && method.getNumberOfParameters() == paramSignatures.length)
return method;
}
// // reference can also point to method from supertype:
// ITypeHierarchy hierarchy= SuperTypeHierarchyCache.getTypeHierarchy(type);
// method= JavaModelUtil.findMethodInHierarchy(hierarchy, type, refMemberName, paramSignatures, false);
// if (method != null)
// return method;
}
} else {
IField field = type.getField(refMemberName);
if (field.exists()) {
return field;
} else {
IMethod[] methods = type.getMethods();
for (int i = 0; i < methods.length; i++) {
IMethod method = methods[i];
if (method.getElementName().equals(refMemberName))
return method;
}
}
}
}
} else {
// FIXME: either remove or show dialog
// JavaPlugin.logErrorMessage("JavaElementLinks could not resolve " + uri); //$NON-NLS-1$
}
return type;
} catch (JavaModelException e) {
LOG.error(e.getMessage(), e);
}
}
}
return element;
}
use of org.eclipse.jdt.core.ITypeParameter in project che by eclipse.
the class RenameJavaElementDescriptor method populateArgumentMap.
/**
* {@inheritDoc}
*/
protected void populateArgumentMap() {
super.populateArgumentMap();
JavaRefactoringDescriptorUtil.setString(fArguments, ATTRIBUTE_NAME, fName);
if (getID().equals(IJavaRefactorings.RENAME_TYPE_PARAMETER)) {
final ITypeParameter parameter = (ITypeParameter) fJavaElement;
JavaRefactoringDescriptorUtil.setJavaElement(fArguments, ATTRIBUTE_INPUT, getProject(), parameter.getDeclaringMember());
JavaRefactoringDescriptorUtil.setString(fArguments, ATTRIBUTE_PARAMETER, parameter.getElementName());
} else
JavaRefactoringDescriptorUtil.setJavaElement(fArguments, ATTRIBUTE_INPUT, getProject(), fJavaElement);
final int type = fJavaElement.getElementType();
if (type != IJavaElement.PACKAGE_FRAGMENT_ROOT)
JavaRefactoringDescriptorUtil.setBoolean(fArguments, ATTRIBUTE_REFERENCES, fReferences);
if (type == IJavaElement.FIELD) {
JavaRefactoringDescriptorUtil.setBoolean(fArguments, ATTRIBUTE_RENAME_GETTER, fRenameGetter);
JavaRefactoringDescriptorUtil.setBoolean(fArguments, ATTRIBUTE_RENAME_SETTER, fRenameSetter);
}
switch(type) {
case IJavaElement.PACKAGE_FRAGMENT:
case IJavaElement.TYPE:
case IJavaElement.FIELD:
JavaRefactoringDescriptorUtil.setBoolean(fArguments, ATTRIBUTE_TEXTUAL_MATCHES, fTextual);
break;
default:
break;
}
switch(type) {
case IJavaElement.METHOD:
case IJavaElement.FIELD:
JavaRefactoringDescriptorUtil.setBoolean(fArguments, ATTRIBUTE_DEPRECATE, fDeprecate);
JavaRefactoringDescriptorUtil.setBoolean(fArguments, ATTRIBUTE_DELEGATE, fDelegate);
break;
default:
break;
}
switch(type) {
case IJavaElement.PACKAGE_FRAGMENT:
case IJavaElement.TYPE:
JavaRefactoringDescriptorUtil.setBoolean(fArguments, ATTRIBUTE_QUALIFIED, fQualified);
JavaRefactoringDescriptorUtil.setString(fArguments, ATTRIBUTE_PATTERNS, fPatterns);
break;
default:
break;
}
switch(type) {
case IJavaElement.TYPE:
JavaRefactoringDescriptorUtil.setBoolean(fArguments, ATTRIBUTE_SIMILAR_DECLARATIONS, fSimilarDeclarations);
JavaRefactoringDescriptorUtil.setInt(fArguments, ATTRIBUTE_MATCH_STRATEGY, fMatchStrategy);
break;
default:
break;
}
switch(type) {
case IJavaElement.PACKAGE_FRAGMENT:
JavaRefactoringDescriptorUtil.setBoolean(fArguments, ATTRIBUTE_HIERARCHICAL, fHierarchical);
break;
default:
break;
}
}
use of org.eclipse.jdt.core.ITypeParameter in project che by eclipse.
the class MethodOverrideTester method computeSubstitutions.
private void computeSubstitutions(IType instantiatedType, IType instantiatingType, String[] typeArguments) throws JavaModelException {
Substitutions s = new Substitutions();
fTypeVariableSubstitutions.put(instantiatedType, s);
ITypeParameter[] typeParameters = instantiatedType.getTypeParameters();
if (instantiatingType == null) {
// the focus type
for (int i = 0; i < typeParameters.length; i++) {
ITypeParameter curr = typeParameters[i];
// use star to make type variables different from type refs
s.addSubstitution(curr.getElementName(), '*' + curr.getElementName(), getTypeParameterErasure(curr, instantiatedType));
}
} else {
if (typeParameters.length == typeArguments.length) {
for (int i = 0; i < typeParameters.length; i++) {
ITypeParameter curr = typeParameters[i];
// substitute in the context of the instantiatingType
String substString = getSubstitutedTypeName(typeArguments[i], instantiatingType);
// get the erasure from the type argument
String erasure = getErasedTypeName(typeArguments[i], instantiatingType);
s.addSubstitution(curr.getElementName(), substString, erasure);
}
} else if (typeArguments.length == 0) {
// raw type reference
for (int i = 0; i < typeParameters.length; i++) {
ITypeParameter curr = typeParameters[i];
String erasure = getTypeParameterErasure(curr, instantiatedType);
s.addSubstitution(curr.getElementName(), erasure, erasure);
}
} else {
// code with errors
}
}
String superclassTypeSignature = instantiatedType.getSuperclassTypeSignature();
if (superclassTypeSignature != null) {
String[] superTypeArguments = Signature.getTypeArguments(superclassTypeSignature);
IType superclass = fHierarchy.getSuperclass(instantiatedType);
if (superclass != null && !fTypeVariableSubstitutions.containsKey(superclass)) {
computeSubstitutions(superclass, instantiatedType, superTypeArguments);
}
}
String[] superInterfacesTypeSignature;
if (instantiatedType.isAnonymous()) {
// special case: superinterface is also returned by IType#getSuperclassTypeSignature()
superInterfacesTypeSignature = new String[] { superclassTypeSignature };
} else {
superInterfacesTypeSignature = instantiatedType.getSuperInterfaceTypeSignatures();
}
int nInterfaces = superInterfacesTypeSignature.length;
if (nInterfaces > 0) {
IType[] superInterfaces = fHierarchy.getSuperInterfaces(instantiatedType);
if (superInterfaces.length == nInterfaces) {
for (int i = 0; i < nInterfaces; i++) {
String[] superTypeArguments = Signature.getTypeArguments(superInterfacesTypeSignature[i]);
IType superInterface = superInterfaces[i];
if (!fTypeVariableSubstitutions.containsKey(superInterface)) {
computeSubstitutions(superInterface, instantiatedType, superTypeArguments);
}
}
}
}
}
Aggregations