/** * 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())); } }
/** * Adds the type in a certain file * * @param summary the file summary * @param list the linked list to add to */ private void add(FileSummary summary, LinkedList list) { Iterator iter = summary.getTypes(); if (iter != null) { while (iter.hasNext()) { TypeSummary typeSummary = (TypeSummary) iter.next(); if (isIncluded(typeSummary)) { list.add(typeSummary.getName()); } } } }
/** * Creates a new interface file. * * @return Description of the Returned Value */ private File createInterfaceFile() { File newFile = null; TypeSummary ts = (TypeSummary) m_summaryList.elementAt(0); PackageSummary ps = ts.getPackageSummary(); if (m_packageName == null) { m_packageName = ps.getName(); } CreateNewInterface cni = new CreateNewInterface(ts, m_packageName, m_interfaceName); try { newFile = cni.run(); } catch (RefactoringException re) { re.printStackTrace(); return null; } m_complexTransform.createFile(newFile); return newFile; }
/** * Gets the Object attribute of the AddAbstractParent object * * @param item Description of Parameter * @return The Object value */ private boolean isObject(TypeSummary item) { if (item == null) { return true; } if (item.getName().equals("Object")) { return true; } return false; }
/** * Gets the SameParent attribute of the AddAbstractParent object * * @param one Description of Parameter * @param two Description of Parameter * @return The SameParent value */ private boolean isSameParent(TypeSummary one, TypeSummary two) { if (isObject(one)) { return isObject(two); } if (isObject(two)) { return false; } return one.equals(two); }
/** * Gets a list of public method summaries that are common to all classes for which an interface is * being extracted. * * @return The MethodSummaries value */ private Vector getMethodSummaries() { Vector firstClassMethods = new Vector(); // Add all relevant methods from the first class. TypeSummary ts = (TypeSummary) m_summaryList.elementAt(0); Iterator methods = ts.getMethods(); while (methods.hasNext()) { MethodSummary ms = (MethodSummary) methods.next(); ModifierHolder mh = ms.getModifiers(); /* * Include only public, non-static, non-constructor methods. * Private and protected methods are not allowed in interfaces and * methods that are package-protected in an interface need to be * implemented by public methods in implementing classes (I think). */ if (mh.isPublic() && (!ms.isConstructor()) && (!mh.isStatic())) { // synchronized modifier is not allowed for interfaces. mh.setSynchronized(false); firstClassMethods.addElement(ms); } } return commonMethods(firstClassMethods); }
/** * Eliminates methods that don't occurr in all classes and returns the resulting Vector of common * methods. * * @param initialMethods Description of Parameter * @return Description of the Returned Value */ private Vector commonMethods(Vector initialMethods) { Vector result = new Vector(); for (int i = 0; i < initialMethods.size(); i++) { boolean keep = true; outerloop: for (int j = 1; j < m_summaryList.size(); j++) { TypeSummary ts = (TypeSummary) m_summaryList.elementAt(j); Iterator methods = ts.getMethods(); while (methods.hasNext()) { MethodSummary ms = (MethodSummary) methods.next(); if (ms.equals((MethodSummary) initialMethods.elementAt(i))) { continue outerloop; } } keep = false; } if (keep) { MethodSummary ms = (MethodSummary) initialMethods.elementAt(i); result.addElement(initialMethods.elementAt(i)); } } return result; }