Skip to content

Commit

Permalink
Merge pull request #27 from saledouble/common_duplication
Browse files Browse the repository at this point in the history
Reduce duplication
  • Loading branch information
Grace Tang authored Jun 27, 2018
2 parents 44b64a7 + ff36322 commit 76ce48f
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 132 deletions.
3 changes: 2 additions & 1 deletion edu.cuny.hunter.log.core/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ Bundle-Name: Core
Bundle-SymbolicName: edu.cuny.hunter.log.core;singleton:=true
Bundle-Version: 1.0.0.qualifier
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Import-Package: org.eclipse.core.runtime;version="3.5.0",
Import-Package: edu.cuny.citytech.refactoring.common.core,
org.eclipse.core.runtime;version="3.5.0",
org.eclipse.jdt.core,
org.eclipse.jdt.core.dom,
org.eclipse.jdt.core.refactoring,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import org.eclipse.jdt.core.refactoring.descriptors.JavaRefactoringDescriptor;

@SuppressWarnings("unchecked")
public class LogDescriptor extends JavaRefactoringDescriptor {

public static final String REFACTORING_ID = "edu.cuny.hunter.logging.evolution"; //$NON-NLS-1$
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,24 @@
import java.util.Map;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.SubProgressMonitor;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.core.ITypeRoot;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.refactoring.CompilationUnitChange;
import org.eclipse.ltk.core.refactoring.Change;
import org.eclipse.ltk.core.refactoring.RefactoringDescriptor;
import org.eclipse.ltk.core.refactoring.RefactoringStatus;
import org.eclipse.ltk.core.refactoring.participants.CheckConditionsContext;
import org.eclipse.ltk.core.refactoring.participants.RefactoringParticipant;
import org.eclipse.ltk.core.refactoring.participants.RefactoringProcessor;
import org.eclipse.ltk.core.refactoring.participants.SharableParticipants;
import org.eclipse.jdt.internal.corext.codemanipulation.CodeGenerationSettings;
import org.eclipse.jdt.internal.corext.refactoring.changes.DynamicValidationRefactoringChange;
import org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite;
import org.eclipse.jdt.internal.corext.refactoring.util.RefactoringASTParser;
import org.eclipse.jdt.internal.corext.refactoring.util.TextEditBasedChangeManager;

import edu.cuny.citytech.refactoring.common.core.RefactoringProcessor;
import edu.cuny.hunter.log.core.analysis.LogAnalyzer;
import edu.cuny.hunter.log.core.analysis.LogInvocation;
import edu.cuny.hunter.log.core.descriptors.LogDescriptor;
Expand All @@ -41,29 +35,26 @@
@SuppressWarnings({ "restriction", "deprecation" })
public class LogRefactoringProcessor extends RefactoringProcessor {

private Set<LogInvocation> logInvocationSet = new HashSet<>();
private IJavaProject[] javaProjects;
private CodeGenerationSettings settings;
private Map<ITypeRoot, CompilationUnit> typeRootToCompilationUnitMap = new HashMap<>();
private Map<ICompilationUnit, CompilationUnitRewrite> compilationUnitToCompilationUnitRewriteMap = new HashMap<>();

private Set<LogInvocation> logInvocationSet = new HashSet<>();

private static final Logger LOGGER = Logger.getLogger(LoggerNames.LOGGER_NAME);

public LogRefactoringProcessor(final CodeGenerationSettings settings) {
super(settings);
}

public LogRefactoringProcessor(IJavaProject[] javaProjects, final CodeGenerationSettings settings,
Optional<IProgressMonitor> monitor) {
super(settings);
try {
this.javaProjects = javaProjects;
this.settings = settings;
} finally {
monitor.ifPresent(IProgressMonitor::done);
}
}

@Override
public Object[] getElements() {
return null;
}

@Override
public String getIdentifier() {
return LogDescriptor.REFACTORING_ID;
Expand All @@ -74,23 +65,6 @@ public String getProcessorName() {
return Messages.Name;
}

@Override
public boolean isApplicable() throws CoreException {
return true;
}

@Override
public RefactoringStatus checkInitialConditions(IProgressMonitor pm)
throws CoreException, OperationCanceledException {
this.clearCaches();
RefactoringStatus status = new RefactoringStatus();
return status;
}

public IJavaProject[] getJavaProjects() {
return this.javaProjects;
}

@Override
public RefactoringStatus checkFinalConditions(final IProgressMonitor monitor, final CheckConditionsContext context)
throws CoreException, OperationCanceledException {
Expand All @@ -108,7 +82,7 @@ public RefactoringStatus checkFinalConditions(final IProgressMonitor monitor, fi
IPackageFragment fragment = (IPackageFragment) child;
ICompilationUnit[] units = fragment.getCompilationUnits();
for (ICompilationUnit unit : units) {
CompilationUnit compilationUnit = this.getCompilationUnit(unit, monitor);
CompilationUnit compilationUnit = super.getCompilationUnit(unit, monitor);
compilationUnit.accept(analyzer);
}
}
Expand All @@ -127,17 +101,8 @@ public RefactoringStatus checkFinalConditions(final IProgressMonitor monitor, fi
}
}

private CompilationUnit getCompilationUnit(ITypeRoot root, IProgressMonitor pm) {
CompilationUnit compilationUnit = this.getTypeRootToCompilationUnitMap().get(root);
if (compilationUnit == null) {
compilationUnit = RefactoringASTParser.parseWithASTProvider(root, true, pm);
this.getTypeRootToCompilationUnitMap().put(root, compilationUnit);
}
return compilationUnit;
}

protected Map<ITypeRoot, CompilationUnit> getTypeRootToCompilationUnitMap() {
return this.typeRootToCompilationUnitMap;
private IJavaProject[] getJavaProjects() {
return this.javaProjects;
}

@Override
Expand All @@ -148,12 +113,12 @@ public Change createChange(IProgressMonitor pm) throws CoreException, OperationC
final TextEditBasedChangeManager manager = new TextEditBasedChangeManager();

// save the source changes.
ICompilationUnit[] units = this.getCompilationUnitToCompilationUnitRewriteMap().keySet().stream()
ICompilationUnit[] units = getCompilationUnitToCompilationUnitRewriteMap().keySet().stream()
.filter(cu -> !manager.containsChangesIn(cu)).toArray(ICompilationUnit[]::new);

for (ICompilationUnit cu : units) {
CompilationUnit compilationUnit = this.getCompilationUnit(cu, pm);
this.manageCompilationUnit(manager, this.getCompilationUnitRewrite(cu, compilationUnit),
CompilationUnit compilationUnit = getCompilationUnit(cu, pm);
super.manageCompilationUnit(manager, super.getCompilationUnitRewrite(cu, compilationUnit),
Optional.of(new SubProgressMonitor(pm, IProgressMonitor.UNKNOWN)));
}

Expand All @@ -167,50 +132,32 @@ public Change createChange(IProgressMonitor pm) throws CoreException, OperationC
return new DynamicValidationRefactoringChange(descriptor, this.getProcessorName(), manager.getAllChanges());
} finally {
pm.done();
this.clearCaches();
clearCaches();
}
}

private void manageCompilationUnit(final TextEditBasedChangeManager manager, CompilationUnitRewrite rewrite,
Optional<IProgressMonitor> monitor) throws CoreException {
monitor.ifPresent(m -> m.beginTask("Creating change ...", IProgressMonitor.UNKNOWN));
CompilationUnitChange change = rewrite.createChange(false, monitor.orElseGet(NullProgressMonitor::new));

if (change != null)
change.setTextType("java");

manager.manage(rewrite.getCu(), change);
protected void setLogInvocationSet(Set<LogInvocation> logInvocationSet) {
this.logInvocationSet = logInvocationSet;
}

private CompilationUnitRewrite getCompilationUnitRewrite(ICompilationUnit unit, CompilationUnit root) {
CompilationUnitRewrite rewrite = this.getCompilationUnitToCompilationUnitRewriteMap().get(unit);
if (rewrite == null) {
rewrite = new CompilationUnitRewrite(unit, root);
this.getCompilationUnitToCompilationUnitRewriteMap().put(unit, rewrite);
}
return rewrite;
public Set<LogInvocation> getLogInvocationSet() {
return this.logInvocationSet;
}

public void clearCaches() {
this.getTypeRootToCompilationUnitMap().clear();
@Override
public Object[] getElements() {
return null;
}

protected Map<ICompilationUnit, CompilationUnitRewrite> getCompilationUnitToCompilationUnitRewriteMap() {
return this.compilationUnitToCompilationUnitRewriteMap;
@Override
public boolean isApplicable() throws CoreException {
return false;
}

@Override
public RefactoringParticipant[] loadParticipants(RefactoringStatus status, SharableParticipants sharedParticipants)
throws CoreException {
return new RefactoringParticipant[0];
}

protected void setLogInvocationSet(Set<LogInvocation> logInvocationSet) {
this.logInvocationSet = logInvocationSet;
}

public Set<LogInvocation> getLogInvocationSet() {
return this.logInvocationSet;
return null;
}

}
3 changes: 2 additions & 1 deletion edu.cuny.hunter.log.evalution/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ Require-Bundle: org.eclipse.ui,
org.eclipse.core.runtime;bundle-version="3.13.0",
edu.cuny.hunter.log.ui;bundle-version="1.0.0"
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Import-Package: edu.cuny.hunter.log.core.analysis,
Import-Package: edu.cuny.citytech.refactoring.common.core,
edu.cuny.hunter.log.core.analysis,
edu.cuny.hunter.log.core.refactorings,
edu.cuny.hunter.log.core.utils,
org.eclipse.jdt.core,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.eclipse.jdt.internal.ui.preferences.JavaPreferencesSettings;
import org.eclipse.jdt.internal.ui.util.SelectionUtil;
import org.eclipse.ui.handlers.HandlerUtil;
import edu.cuny.citytech.refactoring.common.core.RefactoringProcessor;
import edu.cuny.hunter.log.core.analysis.LogInvocation;
import edu.cuny.hunter.log.core.refactorings.LogRefactoringProcessor;
import edu.cuny.hunter.log.core.utils.LoggerNames;
Expand Down Expand Up @@ -69,6 +70,7 @@ public Object execute(ExecutionEvent event) throws ExecutionException {
.getCodeGenerationSettings(javaProjectList.get(0));

try {

CSVPrinter resultPrinter = createCSVPrinter("result.csv",
new String[] { "subject raw", "log expression", "start pos", "logging level",
"type FQN", "enclosing method", "DOI" });
Expand All @@ -79,7 +81,7 @@ public Object execute(ExecutionEvent event) throws ExecutionException {
LogRefactoringProcessor logRefactoringProcessor = new LogRefactoringProcessor(
new IJavaProject[] { project }, settings, monitor);

new ProcessorBasedRefactoring(logRefactoringProcessor)
new ProcessorBasedRefactoring((RefactoringProcessor) logRefactoringProcessor)
.checkAllConditions(new NullProgressMonitor());

Iterator<LogInvocation> logInvocationIterator = logRefactoringProcessor
Expand Down
8 changes: 5 additions & 3 deletions edu.cuny.hunter.log.tests/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ Bundle-Name: Tests
Bundle-SymbolicName: edu.cuny.hunter.log.tests
Bundle-Version: 1.0.0.qualifier
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Import-Package: edu.cuny.hunter.log.core.analysis,
Import-Package: edu.cuny.citytech.refactoring.common.core,
edu.cuny.citytech.refactoring.common.tests,
edu.cuny.hunter.log.core.analysis,
org.eclipse.core.resources,
org.eclipse.core.runtime;version="3.5.0",
org.eclipse.jdt.core,
Expand All @@ -13,5 +15,5 @@ Import-Package: edu.cuny.hunter.log.core.analysis,
org.eclipse.jdt.testplugin,
org.eclipse.jdt.ui.tests.refactoring
Require-Bundle: org.junit;bundle-version="4.12.0",
edu.cuny.citytech.refactoring.common.core;bundle-version="1.1.0",
edu.cuny.citytech.refactoring.common.tests;bundle-version="1.6.0"
org.eclipse.ltk.core.refactoring,
edu.cuny.hunter.log.core
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,50 @@
import junit.framework.Test;
import junit.framework.TestSuite;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Set;
import java.util.logging.Level;

import javax.tools.JavaCompiler;
import javax.tools.ToolProvider;
import java.util.logging.Logger;

import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IMethod;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.ASTParser;
import org.eclipse.jdt.ui.tests.refactoring.Java18Setup;
import org.eclipse.jdt.ui.tests.refactoring.RefactoringTest;
import org.eclipse.ltk.core.refactoring.Refactoring;

import edu.cuny.citytech.refactoring.common.tests.RefactoringTest;
import edu.cuny.hunter.log.core.analysis.LogAnalyzer;
import edu.cuny.hunter.log.core.analysis.LogInvocation;
import edu.cuny.hunter.log.core.utils.LoggerNames;
import edu.cuny.hunter.log.core.analysis.PreconditionFailure;

@SuppressWarnings("restriction")
public class LogEvolutionRefactoringTest extends RefactoringTest {

private static final String REFACTORING_PATH = "LogEvolution/";

private static final String RESOURCE_PATH = "resources";
@Override
protected Logger getLogger() {
return Logger.getLogger(LoggerNames.LOGGER_NAME);
}

@Override
public String getRefactoringPath() {
return REFACTORING_PATH;
}

@Override
protected Refactoring getRefactoring(IMethod... methods) throws JavaModelException {
return null;
}

public LogEvolutionRefactoringTest(String name) {
super(name);
}
Expand All @@ -56,31 +63,11 @@ public static Test setUpTest(Test test) {
return new Java18Setup(test);
}

/**
* Compile the test case
*/
private static boolean compiles(String source, Path path) throws IOException {
// Save source in .java file.
File sourceFile = new File(path.toFile(), "bin/p/A.java");
sourceFile.getParentFile().mkdirs();
Files.write(sourceFile.toPath(), source.getBytes());

// Compile source file.
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
boolean compileSuccess = compiler.run(null, null, null, sourceFile.getPath()) == 0;

sourceFile.delete();
return compileSuccess;
}

@Override
protected ICompilationUnit createCUfromTestFile(IPackageFragment pack, String cuName) throws Exception {

ICompilationUnit unit = super.createCUfromTestFile(pack, cuName);

if (!unit.isStructureKnown())
throw new IllegalArgumentException(cuName + " has structural errors.");

Path directory = Paths.get(unit.getParent().getParent().getParent().getResource().getLocation().toString());

assertTrue("Should compile the testing cases:", compiles(unit.getSource(), directory));
Expand Down Expand Up @@ -141,21 +128,9 @@ public void testArgumentLogRecord() throws Exception {
protected void tearDown() throws Exception {
super.tearDown();
}

/*
* This method could fix the issue that the bundle has no entry.
*/

@Override
public String getFileContents(String fileName) throws IOException {
Path absolutePath = getAbsolutePath(fileName);
byte[] encoded = Files.readAllBytes(absolutePath);
return new String(encoded, Charset.defaultCharset());
}

private static Path getAbsolutePath(String fileName) {
Path path = Paths.get(RESOURCE_PATH, fileName);
Path absolutePath = path.toAbsolutePath();
return absolutePath;
public void testPlainMethod() throws Exception {
}

}
Loading

0 comments on commit 76ce48f

Please sign in to comment.