]> git.uio.no Git - ifi-stolz-refaktor.git/commitdiff
Adding some JavaDoc and cleaning up a bit.
authorErlend Kristiansen <erlenkr@ifi.uio.no>
Thu, 7 Nov 2013 19:18:37 +0000 (20:18 +0100)
committerErlend Kristiansen <erlenkr@ifi.uio.no>
Thu, 7 Nov 2013 19:18:37 +0000 (20:18 +0100)
software/no.uio.ifi.refaktor/src/no/uio/ifi/refaktor/changers/ExtractAndMoveMethodChanger.java
software/no.uio.ifi.refaktor/src/no/uio/ifi/refaktor/changers/RefaktorChanger.java
software/no.uio.ifi.refaktor/src/no/uio/ifi/refaktor/extractors/CommonPrefixExtractor.java
software/no.uio.ifi.refaktor/src/no/uio/ifi/refaktor/extractors/ExtractAndMoveMethodPrefixesExtractor.java
software/no.uio.ifi.refaktor/src/no/uio/ifi/refaktor/extractors/Prefix.java
software/no.uio.ifi.refaktor/src/no/uio/ifi/refaktor/extractors/PrefixSet.java
software/no.uio.ifi.refaktor/src/no/uio/ifi/refaktor/extractors/PropertyExtractor.java
software/no.uio.ifi.refaktor/src/no/uio/ifi/refaktor/extractors/UnionOfLongestCommonPrefixesExtractor.java
software/no.uio.ifi.refaktor/src/no/uio/ifi/refaktor/utils/SmartTextSelection.java

index 20a02f5da4fd1d4c9d4f47ed42e9acdb7a33863a..36a3b162f1f611b60c7455ee49af1bf88e57428e 100644 (file)
@@ -42,6 +42,21 @@ import org.eclipse.ltk.core.refactoring.IUndoManager;
 import org.eclipse.ltk.core.refactoring.Refactoring;
 import org.eclipse.ltk.core.refactoring.participants.MoveRefactoring;
 
+/**
+ * This class composes the refactorings known as
+ * Extract Method and Move Method.
+ * 
+ * Before extracting, it finds the possible targets for the move 
+ * with the help of a PropertyExtractor (see {@link ExtractAndMoveMethodPrefixesExtractor}), 
+ * that extracts both the candidates, in the form prefixes (see {@link Prefix}), 
+ * and the non-candidates, called unfixes. They are collected into sets
+ * of prefixes (see {@link PrefixSet}).The set of prefixes that 
+ * are not enclosing any unfixes is put in the set of safe prefixes.
+ * 
+ * The changer then tries to analyze which of the safe prefixes 
+ * that is the best candidate. The best candidate is used to find the
+ * target of the composed refactoring.
+ */
 @SuppressWarnings("restriction")
 public class ExtractAndMoveMethodChanger extends RefaktorChanger {
        
@@ -194,7 +209,6 @@ public class ExtractAndMoveMethodChanger extends RefaktorChanger {
 
 
        private ExtractMethodRefactoring makeExtractMethodRefactoring(Prefix prefix, ICompilationUnit cu) throws CoreException {
-               //              ExtractMethodRefactoring refactoring = new ExtractMethodRefactoring(cu, prefix.statementStartPosition(), prefix.statementLength());
                ExtractMethodRefactoring refactoring = new ExtractMethodRefactoring(cu, extractor.getSelection().getOffset(), extractor.getSelection().getLength());
 
                refactoring.setMethodName(getNewMethodName());
index 7f0659a9ca5bc14aa58259d73e6b90dc9743ab36..93481af2bf6490caeec7c115f87bde598d7f65f5 100644 (file)
@@ -11,6 +11,10 @@ import org.eclipse.ltk.core.refactoring.IUndoManager;
 import org.eclipse.ltk.core.refactoring.PerformChangeOperation;
 import org.eclipse.ltk.core.refactoring.RefactoringCore;
 
+/**
+ * A range of classes that is responsible for composing 
+ * refactorings that are to be executed on a workspace.
+ */
 public abstract class RefaktorChanger {
 
        private boolean isExecuted = false;
index 6e7a54ebdc1b1f17760c53c30504625f719b6642..0c1a447205bb0777e8c48ed085825f78b6a6a536 100644 (file)
@@ -121,7 +121,7 @@ public abstract class CommonPrefixExtractor extends PropertyExtractor {
                Expression expression = statement.getExpression();
                // TODO: support more expression types
                if (expression instanceof MethodInvocation)
-                       return new Prefix(((MethodInvocation) expression).getExpression(), statement, getDocument());
+                       return new Prefix(((MethodInvocation) expression).getExpression(), getDocument());
                else
                        return null;
        }
index ed28b3b69f0e4df5aab0f8bf19c88e2c73e615f2..fa696e9139767ebb3e4c9aad6fe5807a56378ed8 100644 (file)
@@ -4,13 +4,18 @@ import no.uio.ifi.refaktor.utils.RefaktorDebug;
 import no.uio.ifi.refaktor.utils.SmartTextSelection;
 
 import org.eclipse.jdt.core.ICompilationUnit;
-import org.eclipse.jdt.core.dom.ASTNode;
 import org.eclipse.jdt.core.dom.Assignment;
 import org.eclipse.jdt.core.dom.Expression;
-import org.eclipse.jdt.core.dom.ExpressionStatement;
 import org.eclipse.jdt.core.dom.Name;
 import org.eclipse.jdt.core.dom.SimpleName;
 
+/**
+ * A property extractor that extracts both the union of expression prefixes 
+ * (see {@link UnionOfLongestCommonPrefixesExtractor}), but also the unfixes 
+ * that are not candidates for a move refactoring. It is responsible for 
+ * calculating the safe targets of an Extract and Move Method refactoring
+ * on the base of this knowledge.
+ */
 public class ExtractAndMoveMethodPrefixesExtractor extends UnionOfLongestCommonPrefixesExtractor {
 
        private final PrefixSet unfixes = new PrefixSet();
@@ -35,9 +40,7 @@ public class ExtractAndMoveMethodPrefixesExtractor extends UnionOfLongestCommonP
                Name name = (Name) lhs;
                if (name.isSimpleName()) {
                        SimpleName simpleName = (SimpleName) name;
-                       ASTNode parent = node.getParent();
-                       assert parent instanceof ExpressionStatement;
-                       Prefix p = new Prefix(simpleName, (ExpressionStatement) parent, getDocument());
+                       Prefix p = new Prefix(simpleName, getDocument());
                        RefaktorDebug.println("Unfix string: " + p.toString());
                        unfixes.add(p);
                }
@@ -51,7 +54,7 @@ public class ExtractAndMoveMethodPrefixesExtractor extends UnionOfLongestCommonP
        }
 
        private PrefixSet createSafePrefixes() {
-               return getPrefixSet().minusShadowedPrefixes(unfixes);
+               return getPrefixSet().minusEnclosedPrefixesFrom(unfixes);
        }
 
        @Override
index 2d6f771d31ecf7b507b2f7c1498a25b800a5f819..af04adba68ccf591cc22b6e47ad32b13411ce09d 100644 (file)
@@ -16,9 +16,13 @@ import org.eclipse.jdt.core.dom.Expression;
 import org.eclipse.jdt.core.dom.IBinding;
 import org.eclipse.jdt.core.dom.IVariableBinding;
 import org.eclipse.jdt.core.dom.Name;
-import org.eclipse.jdt.core.dom.Statement;
 import org.eclipse.jface.text.IDocument;
 
+/**
+ * A class for representing a prefix of an Expression/Statement.
+ * 
+ * It has an Expression member for the prefix.
+ */
 public class Prefix {
 
        private class SubExpressionsFinder extends ASTVisitor {
@@ -53,18 +57,17 @@ public class Prefix {
        private final IDocument document;
        private final String prefixString;
        private int count;
-       private final Statement statement;
        private PrefixSet possiblePrefixes;
 
        /**
         * Represent a prefix on the base of an Expression.
         * 
         * @param expression the Expression that is the base of the prefix.
-        * @param document the IDocument that is the base of the expression.
+        * @param document the IDocument that is the base of the source code
+        *              that contains the expression.
         */
-       public Prefix(Expression expression, Statement statement, IDocument document) {
+       public Prefix(Expression expression, IDocument document) {
                this.prefixExpression = expression;
-               this.statement = statement;
                this.document = document;
                this.count = 1;
 
@@ -100,13 +103,13 @@ public class Prefix {
         */
        public Prefix intersectWith(Prefix other) {
                if (prefixIsEmpty(other))
-                       return new Prefix(null, null, document);
+                       return new Prefix(null, document);
                if (this.equals(other))
                        // TODO: return this?
-                       return new Prefix(prefixExpression, statement, document);
+                       return new Prefix(prefixExpression, document);
 
                // TODO: statement??
-               return new Prefix(longestCommonExpressionWith(other), statement, document);
+               return new Prefix(longestCommonExpressionWith(other), document);
        }
 
        private boolean prefixIsEmpty(Prefix prefix) {
@@ -176,7 +179,7 @@ public class Prefix {
                if (possiblePrefixes == null) {
                        possiblePrefixes = new PrefixSet();
                        for (Expression exp: getSubExpressions(prefixExpression))
-                               possiblePrefixes.add(new Prefix(exp, statement, document));
+                               possiblePrefixes.add(new Prefix(exp, document));
                }
                return possiblePrefixes;
        }
@@ -234,7 +237,14 @@ public class Prefix {
                return subExpressions.get(0);
        }
 
-       public boolean shadows(Prefix other) {
+       /**
+        * Tests if this prefix encloses another prefix.
+        * 
+        * Example: "a.b.c" encloses "a.b"
+        * @param other
+        * @return
+        */
+       public boolean encloses(Prefix other) {
                return getSubPrefixes().contains(other);
        }
 
index 16f94769151f31ec303f9ab2d24c18c0f3accecc..622beea46d4e09816c092276546755c31b1e6dd9 100644 (file)
@@ -7,7 +7,17 @@ import java.util.LinkedList;
 import java.util.Map;
 import java.util.Set;
 
-// TODO: move more functionality into this class
+/**
+ * A set for collecting prefixes (see {@link Prefix}}).
+ * 
+ * It is based on a regular Set<Prefix>, but is used to
+ * add some more functionality to the basic set, like 
+ * incrementing a counter in a prefix each time an equal
+ * prefix is registered with the set. In addition
+ * it can produce the set that is this set, minus the 
+ * prefixes from another set that are enclosed by the 
+ * prefixes in this set.
+ */
 public class PrefixSet implements Iterable<Prefix> {
 
        private final Set<Prefix> prefixes = new HashSet<Prefix>();
@@ -23,11 +33,7 @@ public class PrefixSet implements Iterable<Prefix> {
                return prefixes.add(prefix);
        }
 
-       public void addAll(PrefixSet other) {
-               prefixes.addAll(other.prefixes);
-       }
-
-       public void remove(Prefix prefix) {
+       private void remove(Prefix prefix) {
                prefixes.remove(prefix);
        }
 
@@ -68,7 +74,7 @@ public class PrefixSet implements Iterable<Prefix> {
                return null;
        }
 
-       public void registerAllSubPrefixes(Prefix prefix) {
+       public void registerAllSubPrefixesOf(Prefix prefix) {
                for (Prefix p: prefix.getSubPrefixes())
                        register(p);
        }
@@ -89,15 +95,23 @@ public class PrefixSet implements Iterable<Prefix> {
                return map;
        }
 
-       public PrefixSet shallowCopy() {
+       private PrefixSet shallowCopy() {
                return new PrefixSet(this);
        }
 
-       public PrefixSet minusShadowedPrefixes(PrefixSet subtractSet) {
+       /**
+        * Creates a set of prefixes that are the prefixes of this set,
+        * minus the prefixes that are enclosing the prefixes of the 
+        * other set.
+        * 
+        * @param other The set of prefixes that are to be checked against this one.
+        * @return The set of prefixes that are not enclosing the ones in the subtract set.
+        */
+       public PrefixSet minusEnclosedPrefixesFrom(PrefixSet other) {
                PrefixSet prefixSet = shallowCopy();
-               for (Prefix subtrahend: subtractSet) {
+               for (Prefix subtrahend: other) {
                        for (Prefix prefix: this) {
-                               if (prefix.shadows(subtrahend))
+                               if (prefix.encloses(subtrahend))
                                        prefixSet.remove(prefix);
                        }
                }
index e7cd76636c394862646230978cd5318a7d125cb2..16e06e7caf6b6a960e256157e68e2ae46f9e3d47 100644 (file)
@@ -9,6 +9,10 @@ import org.eclipse.jdt.core.dom.NodeFinder;
 import org.eclipse.jface.text.IDocument;
 import org.eclipse.jface.text.ITextSelection;
 
+/**
+ * A hierarchy of classes that are used 
+ * to extract properties from source code.
+ */
 public abstract class PropertyExtractor extends ASTVisitor {
        
        private SmartTextSelection selection;
index e12da09a7f603fce872868c8d706ea855a04a4b5..8bf114cfa5e5476f5173af5650e0ed457c802f62 100644 (file)
@@ -6,6 +6,10 @@ import no.uio.ifi.refaktor.utils.SmartTextSelection;
 
 import org.eclipse.jdt.core.ICompilationUnit;
 
+/**
+ * This class extracts the union of the prefixes found among the expressions
+ * within the selection.
+ */
 public class UnionOfLongestCommonPrefixesExtractor extends CommonPrefixExtractor {
 
        private final PrefixSet prefixes = new PrefixSet();
@@ -19,7 +23,7 @@ public class UnionOfLongestCommonPrefixesExtractor extends CommonPrefixExtractor
        }
 
        protected void registerPrefix(Prefix prefix) {
-               prefixes.registerAllSubPrefixes(prefix);
+               prefixes.registerAllSubPrefixesOf(prefix);
        }
 
        public PrefixSet getPrefixSet() {
index 92dfd2d3c386f2cb180c06c02a7b88a7cc5b465e..16121118a1e015dbd3f9ca13e487f65fa8ffdd7b 100644 (file)
@@ -6,6 +6,10 @@ import org.eclipse.jface.text.IDocument;
 import org.eclipse.jface.text.ITextSelection;
 import org.eclipse.jface.text.TextSelection;
 
+/**
+ * A custom TextSelection that enforces the presence of
+ * a document to back the selection.
+ */
 public class SmartTextSelection extends TextSelection implements ITextSelection {
 
        public SmartTextSelection(IDocument document, int offset, int length) {