/** * Adds the name of the newly created interface to the implements clause of each class selected * for the refactoring. */ private void addInterfaceToClasses() { for (int i = 0; i < m_summaryList.size(); i++) { TypeSummary ts = (TypeSummary) m_summaryList.elementAt(i); FileSummary fileSummary = (FileSummary) ts.getParent(); File file = fileSummary.getFile(); ASTName interfaceName = new ASTName(0); String currentPackageName = ts.getPackageSummary().getName(); /* * If the interface package differs from the class package, then * specify the interface package name */ if ((m_packageName.length() > 0) && !(currentPackageName.equals(m_packageName))) { interfaceName.fromString(m_packageName + "." + m_interfaceName); } else { interfaceName.fromString(m_interfaceName); } m_complexTransform.clear(); // Very Important so we don't re-apply the interface transforms m_complexTransform.add(new AddImplementedInterfaceTransform(interfaceName)); if (!m_packageName.equals(currentPackageName)) { m_complexTransform.add(new AddImportTransform(interfaceName)); } m_complexTransform.apply(file, new File(file.getAbsolutePath())); } }
/** this performs the refactoring */ protected void transform() { File newFile = createInterfaceFile(); // Add declarations of the common methods to the interface Vector methodSummaries = getMethodSummaries(); for (int i = 0; i < methodSummaries.size(); i++) { MethodSummary ms = (MethodSummary) methodSummaries.elementAt(i); m_complexTransform.add(new AddConcreteMethod(ms)); } // Add necessary import statements to support parameter and return types Iterator importTypes = getImportTypes(methodSummaries); while ((importTypes != null) && (importTypes.hasNext())) { TypeDeclSummary decl = (TypeDeclSummary) importTypes.next(); TypeSummary type = GetTypeSummary.query(decl); // If the type is not found, don't attempt to add an import statement if (type != null) { m_complexTransform.add(new AddImportTransform(type)); } } m_complexTransform.apply(newFile, newFile); /* * Delete the backup file for the intermediate new interface file to * ensure that an 'undo' does not recover it. */ newFile = new File(newFile.getAbsolutePath() + ".0"); newFile.delete(); addInterfaceToClasses(); }