Example usage for org.eclipse.jdt.core IType getInitializer

List of usage examples for org.eclipse.jdt.core IType getInitializer

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IType getInitializer.

Prototype

IInitializer getInitializer(int occurrenceCount);

Source Link

Document

Returns the initializer with the specified position relative to the order they are defined in the source.

Usage

From source file:com.codenvy.ide.ext.java.server.internal.core.util.HandleFactory.java

License:Open Source License

/**
 * Create handle by adding child to parent obtained by recursing into parent scopes.
 *///w  w  w .  java 2 s  .c o m
private IJavaElement createElement(Scope scope, int elementPosition, ICompilationUnit unit,
        HashSet existingElements, HashMap knownScopes) {
    IJavaElement newElement = (IJavaElement) knownScopes.get(scope);
    if (newElement != null)
        return newElement;

    switch (scope.kind) {
    case Scope.COMPILATION_UNIT_SCOPE:
        newElement = unit;
        break;
    case Scope.CLASS_SCOPE:
        IJavaElement parentElement = createElement(scope.parent, elementPosition, unit, existingElements,
                knownScopes);
        switch (parentElement.getElementType()) {
        case IJavaElement.COMPILATION_UNIT:
            newElement = ((ICompilationUnit) parentElement)
                    .getType(new String(scope.enclosingSourceType().sourceName));
            break;
        case IJavaElement.TYPE:
            newElement = ((IType) parentElement).getType(new String(scope.enclosingSourceType().sourceName));
            break;
        case IJavaElement.FIELD:
        case IJavaElement.INITIALIZER:
        case IJavaElement.METHOD:
            IMember member = (IMember) parentElement;
            if (member.isBinary()) {
                return null;
            } else {
                newElement = member.getType(new String(scope.enclosingSourceType().sourceName), 1);
                // increment occurrence count if collision is detected
                if (newElement != null) {
                    while (!existingElements.add(newElement))
                        ((SourceRefElement) newElement).occurrenceCount++;
                }
            }
            break;
        }
        if (newElement != null) {
            knownScopes.put(scope, newElement);
        }
        break;
    case Scope.METHOD_SCOPE:
        IType parentType = (IType) createElement(scope.parent, elementPosition, unit, existingElements,
                knownScopes);
        MethodScope methodScope = (MethodScope) scope;
        if (methodScope.isInsideInitializer()) {
            // inside field or initializer, must find proper one
            TypeDeclaration type = methodScope.referenceType();
            int occurenceCount = 1;
            int length = type.fields == null ? 0 : type.fields.length;
            for (int i = 0; i < length; i++) {
                FieldDeclaration field = type.fields[i];
                if (field.declarationSourceStart <= elementPosition
                        && elementPosition <= field.declarationSourceEnd) {
                    switch (field.getKind()) {
                    case AbstractVariableDeclaration.FIELD:
                    case AbstractVariableDeclaration.ENUM_CONSTANT:
                        newElement = parentType.getField(new String(field.name));
                        break;
                    case AbstractVariableDeclaration.INITIALIZER:
                        newElement = parentType.getInitializer(occurenceCount);
                        break;
                    }
                    break;
                } else if (field.getKind() == AbstractVariableDeclaration.INITIALIZER) {
                    occurenceCount++;
                }
            }
        } else {
            // method element
            AbstractMethodDeclaration method = methodScope.referenceMethod();
            newElement = parentType.getMethod(new String(method.selector),
                    Util.typeParameterSignatures(method));
            if (newElement != null) {
                knownScopes.put(scope, newElement);
            }
        }
        break;
    case Scope.BLOCK_SCOPE:
        // standard block, no element per se
        newElement = createElement(scope.parent, elementPosition, unit, existingElements, knownScopes);
        break;
    }
    return newElement;
}

From source file:org.eclipse.che.jdt.internal.core.util.HandleFactory.java

License:Open Source License

/**
 * Create handle by adding child to parent obtained by recursing into parent scopes.
 *//*from ww w  .  j  a  v  a2 s .c o m*/
public IJavaElement createElement(Scope scope, int elementPosition, ICompilationUnit unit,
        HashSet existingElements, HashMap knownScopes) {
    IJavaElement newElement = (IJavaElement) knownScopes.get(scope);
    if (newElement != null)
        return newElement;

    switch (scope.kind) {
    case Scope.COMPILATION_UNIT_SCOPE:
        newElement = unit;
        break;
    case Scope.CLASS_SCOPE:
        IJavaElement parentElement = createElement(scope.parent, elementPosition, unit, existingElements,
                knownScopes);
        switch (parentElement.getElementType()) {
        case IJavaElement.COMPILATION_UNIT:
            newElement = ((ICompilationUnit) parentElement)
                    .getType(new String(scope.enclosingSourceType().sourceName));
            break;
        case IJavaElement.TYPE:
            newElement = ((IType) parentElement).getType(new String(scope.enclosingSourceType().sourceName));
            break;
        case IJavaElement.FIELD:
        case IJavaElement.INITIALIZER:
        case IJavaElement.METHOD:
            IMember member = (IMember) parentElement;
            if (member.isBinary()) {
                return null;
            } else {
                newElement = member.getType(new String(scope.enclosingSourceType().sourceName), 1);
                // increment occurrence count if collision is detected
                if (newElement != null) {
                    while (!existingElements.add(newElement))
                        ((SourceRefElement) newElement).occurrenceCount++;
                }
            }
            break;
        }
        if (newElement != null) {
            knownScopes.put(scope, newElement);
        }
        break;
    case Scope.METHOD_SCOPE:
        if (scope.isLambdaScope()) {
            parentElement = createElement(scope.parent, elementPosition, unit, existingElements, knownScopes);
            LambdaExpression expression = (LambdaExpression) scope.originalReferenceContext();
            if (expression.resolvedType != null && expression.resolvedType.isValidBinding()
                    && !(expression.descriptor instanceof ProblemMethodBinding)) { // chain in lambda element only if resolved properly.
                //newElement = new org.eclipse.jdt.internal.core.SourceLambdaExpression((JavaElement) parentElement, expression)
                // .getMethod();

                newElement = LambdaFactory.createLambdaExpression((JavaElement) parentElement, expression)
                        .getMethod();
                knownScopes.put(scope, newElement);
                return newElement;
            }
            return parentElement;
        }
        IType parentType = (IType) createElement(scope.parent, elementPosition, unit, existingElements,
                knownScopes);
        MethodScope methodScope = (MethodScope) scope;
        if (methodScope.isInsideInitializer()) {
            // inside field or initializer, must find proper one
            TypeDeclaration type = methodScope.referenceType();
            int occurenceCount = 1;
            int length = type.fields == null ? 0 : type.fields.length;
            for (int i = 0; i < length; i++) {
                FieldDeclaration field = type.fields[i];
                if (field.declarationSourceStart <= elementPosition
                        && elementPosition <= field.declarationSourceEnd) {
                    switch (field.getKind()) {
                    case AbstractVariableDeclaration.FIELD:
                    case AbstractVariableDeclaration.ENUM_CONSTANT:
                        newElement = parentType.getField(new String(field.name));
                        break;
                    case AbstractVariableDeclaration.INITIALIZER:
                        newElement = parentType.getInitializer(occurenceCount);
                        break;
                    }
                    break;
                } else if (field.getKind() == AbstractVariableDeclaration.INITIALIZER) {
                    occurenceCount++;
                }
            }
        } else {
            // method element
            AbstractMethodDeclaration method = methodScope.referenceMethod();
            newElement = parentType.getMethod(new String(method.selector),
                    Util.typeParameterSignatures(method));
            if (newElement != null) {
                knownScopes.put(scope, newElement);
            }
        }
        break;
    case Scope.BLOCK_SCOPE:
        // standard block, no element per se
        newElement = createElement(scope.parent, elementPosition, unit, existingElements, knownScopes);
        break;
    }
    return newElement;
}

From source file:org.eclipse.che.jdt.refactoring.RenameTypeTest.java

License:Open Source License

@Test
public void testSimilarElements22() throws Exception {
    // Test transplanter for fields in types inside of initializers

    ParticipantTesting.reset();/* w w  w.ja v  a  2  s .  c  om*/
    ICompilationUnit cu = createCUfromTestFile(getPackageP(), "SomeClass");
    IType someClass = getType(cu, "SomeClass");

    List handleList = new ArrayList();
    List argumentList = new ArrayList();

    List similarOldHandleList = new ArrayList();
    List similarNewNameList = new ArrayList();
    List similarNewHandleList = new ArrayList();

    final String newName = "SomeNewClass";

    // field in class in initializer
    IField inInitializer = someClass.getInitializer(1).getType("InInitializer", 1)
            .getField("someClassInInitializer");
    similarOldHandleList.add(inInitializer.getHandleIdentifier());
    similarNewNameList.add("someNewClassInInitializer");
    similarNewHandleList.add("Lp/SomeNewClass$InInitializer;.someNewClassInInitializer");

    // Type Stuff
    handleList.add(someClass);
    argumentList.add(new RenameArguments(newName, true));
    handleList.add(cu);
    argumentList.add(new RenameArguments(newName + ".java", true));
    handleList.add(cu.getResource());
    argumentList.add(new RenameArguments(newName + ".java", true));

    String[] handles = ParticipantTesting.createHandles(handleList.toArray());
    RenameArguments[] arguments = (RenameArguments[]) argumentList.toArray(new RenameArguments[0]);

    RenameJavaElementDescriptor descriptor = createRefactoringDescriptor(someClass, newName);
    setTheOptions(descriptor, true, false, true, null, RenamingNameSuggestor.STRATEGY_EMBEDDED);
    RefactoringStatus status = performRefactoring(descriptor);
    assertNull("was supposed to pass", status);

    checkResultInClass(newName);

    ParticipantTesting.testRename(handles, arguments);
    ParticipantTesting.testSimilarElements(similarOldHandleList, similarNewNameList, similarNewHandleList);

}

From source file:org.eclipse.che.plugin.java.server.jdt.refactoring.RenameTypeTest.java

License:Open Source License

@Test
public void testSimilarElements22() throws Exception {
    // Test transplanter for fields in types inside of initializers

    ParticipantTesting.reset();/*ww  w . j  ava2s  .  com*/
    ICompilationUnit cu = createCUfromTestFile(getPackageP(), "SomeClass");
    IType someClass = getType(cu, "SomeClass");

    List handleList = new ArrayList();
    List argumentList = new ArrayList();

    List similarOldHandleList = new ArrayList();
    List similarNewNameList = new ArrayList();
    List similarNewHandleList = new ArrayList();

    final String newName = "SomeNewClass";

    // field in class in initializer
    IField inInitializer = someClass.getInitializer(1).getType("InInitializer", 1)
            .getField("someClassInInitializer");
    similarOldHandleList.add(inInitializer.getHandleIdentifier());
    similarNewNameList.add("someNewClassInInitializer");
    similarNewHandleList.add("Lp/SomeNewClass$InInitializer;.someNewClassInInitializer");

    // Type Stuff
    handleList.add(someClass);
    argumentList.add(new RenameArguments(newName, true));
    handleList.add(cu);
    argumentList.add(new RenameArguments(newName + ".java", true));
    handleList.add(cu.getResource());
    argumentList.add(new RenameArguments(newName + ".java", true));

    String[] handles = ParticipantTesting.createHandles(handleList.toArray());
    RenameArguments[] arguments = (RenameArguments[]) argumentList.toArray(new RenameArguments[0]);

    RenameJavaElementDescriptor descriptor = createRefactoringDescriptor(someClass, newName);
    setTheOptions(descriptor, true, false, true, null, RenamingNameSuggestor.STRATEGY_EMBEDDED);
    RefactoringStatus status = performRefactoring(descriptor);
    Assert.assertNull("was supposed to pass", status);

    checkResultInClass(newName);

    ParticipantTesting.testRename(handles, arguments);
    ParticipantTesting.testSimilarElements(similarOldHandleList, similarNewNameList, similarNewHandleList);

}