From 55efe46a7b2c7a3de63955488567ae86d0e0cc75 Mon Sep 17 00:00:00 2001 From: Aman Sharma Date: Sun, 11 Apr 2021 04:17:11 +0530 Subject: [PATCH] Add utility to get the entire program instead of the first class Signed-off-by: Aman Sharma --- src/main/java/com/diffmin/App.java | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/diffmin/App.java b/src/main/java/com/diffmin/App.java index 08cddd9..ed14927 100644 --- a/src/main/java/com/diffmin/App.java +++ b/src/main/java/com/diffmin/App.java @@ -7,12 +7,16 @@ import gumtree.spoon.diff.operations.Operation; import gumtree.spoon.diff.operations.OperationKind; import java.io.File; +import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.List; import spoon.Launcher; +import spoon.compiler.SpoonResource; +import spoon.compiler.SpoonResourceHelper; import spoon.reflect.CtModel; import spoon.reflect.declaration.CtCompilationUnit; import spoon.reflect.declaration.CtElement; +import spoon.reflect.declaration.CtPackage; import spoon.reflect.declaration.CtType; /** @@ -35,6 +39,21 @@ public App(String prevFilePath) { modelToBeModified = launcher.buildModel(); } + /** + * Returns the root package of the file. + * + * @param file File whose all {@link CtPackage} needs to returned + * @return Root package of the file + * @throws FileNotFoundException Exception raise via {@link SpoonResourceHelper} + */ + public CtPackage getPackage(File file) throws FileNotFoundException { + final SpoonResource resource = SpoonResourceHelper.createResource(file); + final Launcher launcher = new Launcher(); + launcher.addInputResource(resource); + CtModel model = launcher.buildModel(); + return model.getRootPackage(); + } + /** * Computes the list of operations which is used for patching `f2`. * @@ -44,7 +63,9 @@ public App(String prevFilePath) { * @throws Exception Exception raised via {@link AstComparator} */ public List getOperations(File prevFile, File newFile) throws Exception { - return new AstComparator().compare(prevFile, newFile).getRootOperations(); + CtElement prevPackage = getPackage(prevFile); + CtElement newPackage = getPackage(newFile); + return new AstComparator().compare(prevPackage, newPackage).getRootOperations(); } /**