Search in sources :

Example 1 with ITypeParameter

use of org.eclipse.jdt.core.ITypeParameter in project che by eclipse.

the class RenameTypeParameterTest method helper2.

private void helper2(String parameterName, String newParameterName, String typeName, String methodName, String[] methodSignature, boolean references) throws Exception {
    ParticipantTesting.reset();
    ICompilationUnit cu = createCUfromTestFile(getPackageP(), "A");
    IType declaringType = getType(cu, typeName);
    IMethod[] declaringMethods = getMethods(declaringType, new String[] { methodName }, new String[][] { methodSignature });
    ITypeParameter typeParameter = declaringMethods[0].getTypeParameter(parameterName);
    RenameTypeParameterProcessor processor = new RenameTypeParameterProcessor(typeParameter);
    RenameRefactoring refactoring = new RenameRefactoring(processor);
    processor.setNewElementName(newParameterName);
    processor.setUpdateReferences(references);
    RefactoringStatus result = performRefactoring(refactoring);
    assertEquals("was supposed to pass", null, result);
    assertEqualLines("invalid renaming", getFileContents(getOutputTestFileName("A")), cu.getSource());
    assertTrue("anythingToUndo", RefactoringCore.getUndoManager().anythingToUndo());
    assertTrue("! anythingToRedo", !RefactoringCore.getUndoManager().anythingToRedo());
    RefactoringCore.getUndoManager().performUndo(null, new NullProgressMonitor());
    assertEqualLines("invalid undo", getFileContents(getInputTestFileName("A")), cu.getSource());
    assertTrue("! anythingToUndo", !RefactoringCore.getUndoManager().anythingToUndo());
    assertTrue("anythingToRedo", RefactoringCore.getUndoManager().anythingToRedo());
    RefactoringCore.getUndoManager().performRedo(null, new NullProgressMonitor());
    assertEqualLines("invalid redo", getFileContents(getOutputTestFileName("A")), cu.getSource());
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) RenameRefactoring(org.eclipse.ltk.core.refactoring.participants.RenameRefactoring) RenameTypeParameterProcessor(org.eclipse.jdt.internal.corext.refactoring.rename.RenameTypeParameterProcessor) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) ITypeParameter(org.eclipse.jdt.core.ITypeParameter) IMethod(org.eclipse.jdt.core.IMethod) IType(org.eclipse.jdt.core.IType)

Example 2 with ITypeParameter

use of org.eclipse.jdt.core.ITypeParameter in project che by eclipse.

the class RenameTypeParameterTest method helper2.

private void helper2(String parameterName, String newParameterName, String typeName, boolean references) throws Exception {
    ParticipantTesting.reset();
    ICompilationUnit cu = createCUfromTestFile(getPackageP(), "A");
    IType declaringType = getType(cu, typeName);
    ITypeParameter typeParameter = declaringType.getTypeParameter(parameterName);
    RenameTypeParameterProcessor processor = new RenameTypeParameterProcessor(typeParameter);
    RenameRefactoring refactoring = new RenameRefactoring(processor);
    processor.setNewElementName(newParameterName);
    processor.setUpdateReferences(references);
    RefactoringStatus result = performRefactoring(refactoring);
    assertEquals("was supposed to pass", null, result);
    assertEqualLines("invalid renaming", getFileContents(getOutputTestFileName("A")), cu.getSource());
    assertTrue("anythingToUndo", RefactoringCore.getUndoManager().anythingToUndo());
    assertTrue("! anythingToRedo", !RefactoringCore.getUndoManager().anythingToRedo());
    RefactoringCore.getUndoManager().performUndo(null, new NullProgressMonitor());
    assertEqualLines("invalid undo", getFileContents(getInputTestFileName("A")), cu.getSource());
    assertTrue("! anythingToUndo", !RefactoringCore.getUndoManager().anythingToUndo());
    assertTrue("anythingToRedo", RefactoringCore.getUndoManager().anythingToRedo());
    RefactoringCore.getUndoManager().performRedo(null, new NullProgressMonitor());
    assertEqualLines("invalid redo", getFileContents(getOutputTestFileName("A")), cu.getSource());
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) RenameRefactoring(org.eclipse.ltk.core.refactoring.participants.RenameRefactoring) RenameTypeParameterProcessor(org.eclipse.jdt.internal.corext.refactoring.rename.RenameTypeParameterProcessor) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) ITypeParameter(org.eclipse.jdt.core.ITypeParameter) IType(org.eclipse.jdt.core.IType)

Example 3 with ITypeParameter

use of org.eclipse.jdt.core.ITypeParameter in project che by eclipse.

the class JavaElementLinks method resolveTypeVariable.

private static ITypeParameter resolveTypeVariable(IJavaElement baseElement, String typeVariableName) throws JavaModelException {
    while (baseElement != null) {
        switch(baseElement.getElementType()) {
            case IJavaElement.METHOD:
                IMethod method = (IMethod) baseElement;
                ITypeParameter[] typeParameters = method.getTypeParameters();
                for (int i = 0; i < typeParameters.length; i++) {
                    ITypeParameter typeParameter = typeParameters[i];
                    if (typeParameter.getElementName().equals(typeVariableName)) {
                        return typeParameter;
                    }
                }
                break;
            case IJavaElement.TYPE:
                IType type = (IType) baseElement;
                typeParameters = type.getTypeParameters();
                for (int i = 0; i < typeParameters.length; i++) {
                    ITypeParameter typeParameter = typeParameters[i];
                    if (typeParameter.getElementName().equals(typeVariableName)) {
                        return typeParameter;
                    }
                }
                break;
            case IJavaElement.JAVA_MODEL:
            case IJavaElement.JAVA_PROJECT:
            case IJavaElement.PACKAGE_FRAGMENT:
            case IJavaElement.PACKAGE_FRAGMENT_ROOT:
            case IJavaElement.CLASS_FILE:
            case IJavaElement.COMPILATION_UNIT:
            case IJavaElement.PACKAGE_DECLARATION:
            case IJavaElement.IMPORT_CONTAINER:
            case IJavaElement.IMPORT_DECLARATION:
                return null;
            default:
                break;
        }
        // look for type parameters in enclosing members:
        baseElement = baseElement.getParent();
    }
    return null;
}
Also used : ITypeParameter(org.eclipse.jdt.core.ITypeParameter) IMethod(org.eclipse.jdt.core.IMethod) IType(org.eclipse.jdt.core.IType)

Example 4 with ITypeParameter

use of org.eclipse.jdt.core.ITypeParameter in project che by eclipse.

the class LazyGenericTypeProposal method mapTypeParameterIndex.

/**
	 * For the type parameter at <code>paramIndex</code> in the type at <code>path[pathIndex]</code>
	 * , this method computes the corresponding type parameter index in the type at
	 * <code>path[0]</code>. If the type parameter does not map to a type parameter of the super
	 * type, <code>-1</code> is returned.
	 *
	 * @param path the type inheritance path, a non-empty array of consecutive sub types
	 * @param pathIndex an index into <code>path</code> specifying the type to start with
	 * @param paramIndex the index of the type parameter to map - <code>path[pathIndex]</code> must
	 *            have a type parameter at that index, lest an
	 *            <code>ArrayIndexOutOfBoundsException</code> is thrown
	 * @return the index of the type parameter in <code>path[0]</code> corresponding to the type
	 *         parameter at <code>paramIndex</code> in <code>path[pathIndex]</code>, or -1 if there
	 *         is no corresponding type parameter
	 * @throws org.eclipse.jdt.core.JavaModelException if this element does not exist or if an exception occurs while
	 *             accessing its corresponding resource
	 * @throws ArrayIndexOutOfBoundsException if <code>path[pathIndex]</code> has &lt;=
	 *             <code>paramIndex</code> parameters
	 */
private 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);
}
Also used : ITypeParameter(org.eclipse.jdt.core.ITypeParameter) Point(org.eclipse.swt.graphics.Point) IType(org.eclipse.jdt.core.IType)

Example 5 with ITypeParameter

use of org.eclipse.jdt.core.ITypeParameter in project flux by eclipse.

the class MethodOverrideTester method getMethodSubstitions.

/*
	 * Returns the substitutions for a method's type parameters
	 */
private Substitutions getMethodSubstitions(IMethod method) throws JavaModelException {
    if (fMethodSubstitutions == null) {
        fMethodSubstitutions = new LRUMap<IMethod, Substitutions>(3);
    }
    Substitutions s = fMethodSubstitutions.get(method);
    if (s == null) {
        ITypeParameter[] typeParameters = method.getTypeParameters();
        if (typeParameters.length == 0) {
            s = Substitutions.EMPTY_SUBST;
        } else {
            IType instantiatedType = method.getDeclaringType();
            s = new Substitutions();
            for (int i = 0; i < typeParameters.length; i++) {
                ITypeParameter curr = typeParameters[i];
                s.addSubstitution(curr.getElementName(), '+' + String.valueOf(i), getTypeParameterErasure(curr, instantiatedType));
            }
        }
        fMethodSubstitutions.put(method, s);
    }
    return s;
}
Also used : ITypeParameter(org.eclipse.jdt.core.ITypeParameter) IMethod(org.eclipse.jdt.core.IMethod) IType(org.eclipse.jdt.core.IType)

Aggregations

ITypeParameter (org.eclipse.jdt.core.ITypeParameter)16 IType (org.eclipse.jdt.core.IType)14 IMethod (org.eclipse.jdt.core.IMethod)6 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)2 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)2 ISourceRange (org.eclipse.jdt.core.ISourceRange)2 JavaModelException (org.eclipse.jdt.core.JavaModelException)2 SourceRange (org.eclipse.jdt.core.SourceRange)2 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)2 RenameTypeParameterProcessor (org.eclipse.jdt.internal.corext.refactoring.rename.RenameTypeParameterProcessor)2 RefactoringStatus (org.eclipse.ltk.core.refactoring.RefactoringStatus)2 RenameRefactoring (org.eclipse.ltk.core.refactoring.participants.RenameRefactoring)2 Point (org.eclipse.swt.graphics.Point)2 HashSet (java.util.HashSet)1 IAnnotation (org.eclipse.jdt.core.IAnnotation)1 IClassFile (org.eclipse.jdt.core.IClassFile)1 IField (org.eclipse.jdt.core.IField)1 IJavaElement (org.eclipse.jdt.core.IJavaElement)1 IJavaProject (org.eclipse.jdt.core.IJavaProject)1 ILocalVariable (org.eclipse.jdt.core.ILocalVariable)1