Search in sources :

Example 11 with IField

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

the class RenameTypeTest method testSimilarElements23.

@Test
public void testSimilarElements23() throws Exception {
    // Test transplanter for elements inside types inside fields
    ParticipantTesting.reset();
    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";
    // some field
    IField anotherSomeClass = someClass.getField("anotherSomeClass");
    similarOldHandleList.add(anotherSomeClass.getHandleIdentifier());
    similarNewNameList.add("anotherSomeNewClass");
    similarNewHandleList.add("Lp/SomeNewClass;.anotherSomeNewClass");
    // field in class in method in field declaration ;)
    IField inInner = anotherSomeClass.getType("", 1).getMethod("foo", new String[0]).getType("X", 1).getField("someClassInInner");
    similarOldHandleList.add(inInner.getHandleIdentifier());
    similarNewNameList.add("someNewClassInInner");
    similarNewHandleList.add("Lp/SomeNewClass$1$X;.someNewClassInInner");
    // 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);
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) RenameArguments(org.eclipse.ltk.core.refactoring.participants.RenameArguments) ArrayList(java.util.ArrayList) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) ArrayList(java.util.ArrayList) List(java.util.List) IField(org.eclipse.jdt.core.IField) RenameJavaElementDescriptor(org.eclipse.jdt.core.refactoring.descriptors.RenameJavaElementDescriptor) IType(org.eclipse.jdt.core.IType) Test(org.junit.Test) BaseTest(org.eclipse.che.plugin.java.server.che.BaseTest)

Example 12 with IField

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

the class RenamePrivateFieldTest method helper1_0.

private void helper1_0(String fieldName, String newFieldName, String typeName, boolean renameGetter, boolean renameSetter) throws Exception {
    IType declaringType = getType(createCUfromTestFile(getPackageP(), "A"), typeName);
    IField field = declaringType.getField(fieldName);
    RenameJavaElementDescriptor descriptor = RefactoringSignatureDescriptorFactory.createRenameJavaElementDescriptor(IJavaRefactorings.RENAME_FIELD);
    descriptor.setJavaElement(field);
    descriptor.setNewName(newFieldName);
    descriptor.setUpdateReferences(true);
    descriptor.setRenameGetters(renameGetter);
    descriptor.setRenameSetters(renameSetter);
    RefactoringStatus result = performRefactoring(descriptor);
    assertNotNull("precondition was supposed to fail", result);
}
Also used : RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) IField(org.eclipse.jdt.core.IField) RenameJavaElementDescriptor(org.eclipse.jdt.core.refactoring.descriptors.RenameJavaElementDescriptor) IType(org.eclipse.jdt.core.IType)

Example 13 with IField

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

the class ExtractClassDescriptor method getFields.

/**
	 * Creates {@link Field} objects for all instance fields of the type
	 *
	 * @param type the type declaring the field that will be moved to the extracted class
	 * @return an instance of {@link Field} for every field declared in type that is not static
	 * @throws JavaModelException if the type does not exist or if an exception occurs while accessing its corresponding resource.
	 */
public static Field[] getFields(IType type) throws JavaModelException {
    IField[] fields = type.getFields();
    ArrayList result = new ArrayList();
    for (int i = 0; i < fields.length; i++) {
        IField field = fields[i];
        if (!Flags.isStatic(field.getFlags()) && !field.isEnumConstant())
            result.add(new Field(field.getElementName()));
    }
    return (Field[]) result.toArray(new Field[result.size()]);
}
Also used : IField(org.eclipse.jdt.core.IField) ArrayList(java.util.ArrayList) IField(org.eclipse.jdt.core.IField)

Example 14 with IField

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

the class JavaNavigation method convertToDTOField.

private Field convertToDTOField(IField iField) throws JavaModelException {
    Field field = DtoFactory.newDto(Field.class);
    setRootPath(iField, field);
    field.setFileRegion(convertToDTORegion(iField.getNameRange()));
    field.setElementName(iField.getElementName());
    field.setHandleIdentifier(iField.getHandleIdentifier());
    field.setFlags(iField.getFlags());
    field.setLabel(org.eclipse.jdt.ui.JavaElementLabels.getElementLabel(iField, org.eclipse.jdt.ui.JavaElementLabels.ALL_DEFAULT));
    return field;
}
Also used : Field(org.eclipse.che.ide.ext.java.shared.dto.model.Field) IField(org.eclipse.jdt.core.IField)

Example 15 with IField

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

the class ASTNodeDeleteUtil method getNodesToDelete.

private static ASTNode[] getNodesToDelete(IJavaElement element, CompilationUnit cuNode) throws JavaModelException {
    // fields are different because you don't delete the whole declaration but only a fragment of it
    if (element.getElementType() == IJavaElement.FIELD) {
        if (JdtFlags.isEnum((IField) element))
            return new ASTNode[] { ASTNodeSearchUtil.getEnumConstantDeclaration((IField) element, cuNode) };
        else
            return new ASTNode[] { ASTNodeSearchUtil.getFieldDeclarationFragmentNode((IField) element, cuNode) };
    }
    if (element.getElementType() == IJavaElement.TYPE && ((IType) element).isLocal()) {
        IType type = (IType) element;
        if (type.isAnonymous()) {
            if (type.getParent().getElementType() == IJavaElement.FIELD) {
                EnumConstantDeclaration enumDecl = ASTNodeSearchUtil.getEnumConstantDeclaration((IField) element.getParent(), cuNode);
                if (enumDecl != null && enumDecl.getAnonymousClassDeclaration() != null) {
                    return new ASTNode[] { enumDecl.getAnonymousClassDeclaration() };
                }
            }
            ClassInstanceCreation creation = ASTNodeSearchUtil.getClassInstanceCreationNode(type, cuNode);
            if (creation != null) {
                if (creation.getLocationInParent() == ExpressionStatement.EXPRESSION_PROPERTY) {
                    return new ASTNode[] { creation.getParent() };
                } else if (creation.getLocationInParent() == VariableDeclarationFragment.INITIALIZER_PROPERTY) {
                    return new ASTNode[] { creation };
                }
                return new ASTNode[] { creation.getAnonymousClassDeclaration() };
            }
            return new ASTNode[0];
        } else {
            ASTNode[] nodes = ASTNodeSearchUtil.getDeclarationNodes(element, cuNode);
            // we have to delete the TypeDeclarationStatement
            nodes[0] = nodes[0].getParent();
            return nodes;
        }
    }
    return ASTNodeSearchUtil.getDeclarationNodes(element, cuNode);
}
Also used : ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) EnumConstantDeclaration(org.eclipse.jdt.core.dom.EnumConstantDeclaration) ASTNode(org.eclipse.jdt.core.dom.ASTNode) IField(org.eclipse.jdt.core.IField) IType(org.eclipse.jdt.core.IType)

Aggregations

IField (org.eclipse.jdt.core.IField)47 IType (org.eclipse.jdt.core.IType)30 IMethod (org.eclipse.jdt.core.IMethod)15 RefactoringStatus (org.eclipse.ltk.core.refactoring.RefactoringStatus)15 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)14 IJavaElement (org.eclipse.jdt.core.IJavaElement)14 ArrayList (java.util.ArrayList)13 RenameJavaElementDescriptor (org.eclipse.jdt.core.refactoring.descriptors.RenameJavaElementDescriptor)9 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)6 Test (org.junit.Test)6 List (java.util.List)5 ILocalVariable (org.eclipse.jdt.core.ILocalVariable)5 JavaModelException (org.eclipse.jdt.core.JavaModelException)5 RenameArguments (org.eclipse.ltk.core.refactoring.participants.RenameArguments)5 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)4 IJavaProject (org.eclipse.jdt.core.IJavaProject)4 IMember (org.eclipse.jdt.core.IMember)4 ISourceRange (org.eclipse.jdt.core.ISourceRange)4 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)4 Field (org.eclipse.che.ide.ext.java.shared.dto.model.Field)3