Skip to content

Commit

Permalink
Refactor of ReflectionUtils to reduce need for type casting
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Apr 22, 2018
1 parent 39959cc commit 02993bf
Show file tree
Hide file tree
Showing 39 changed files with 225 additions and 217 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,63 +80,82 @@ public final void setUpTestCase() {
@After
public final void tearDownTestCase() {
env.resetWorkspace();
// Discard primary working copies and copies with owner left from failed tests
ICompilationUnit[] wcs = null;
int i = 0;
do {
wcs = JavaModelManager.getJavaModelManager().getWorkingCopies(null, true);
if (wcs != null) {
for (ICompilationUnit workingCopy : wcs) {
try {
workingCopy.discardWorkingCopy();
workingCopy.close();
} catch (Exception e) {
e.printStackTrace();
try {
// discard primary working copies and copies with owner left from failed tests
ICompilationUnit[] wcs = null;
int i = 0;
do {
wcs = JavaModelManager.getJavaModelManager().getWorkingCopies(null, true);
if (wcs != null) {
for (ICompilationUnit workingCopy : wcs) {
try {
workingCopy.discardWorkingCopy();
workingCopy.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
i += 1;
if (i > 20 && wcs != null) {
Assert.fail("Could not delete working copies " + wcs);
}
} while (wcs != null && wcs.length > 0);
Assert.assertTrue("ModuleNodeMapper should be empty when there are no working copies", moduleNodeMapperCacheSize >= ModuleNodeMapper.size());
JavaCore.setOptions(JavaCore.getDefaultOptions());
i += 1;
if (i > 20 && wcs != null) {
Assert.fail("Could not delete working copies " + wcs);
}
} while (wcs != null && wcs.length > 0);

Assert.assertTrue("ModuleNodeMapper should be empty when there are no working copies", moduleNodeMapperCacheSize >= ModuleNodeMapper.size());
} finally {
JavaCore.setOptions(JavaCore.getDefaultOptions());
}
}

protected final void cleanBuild() {
debugRequestor.clearResult();
debugRequestor.activate();
env.cleanBuild();
debugRequestor.deactivate();
try {
env.cleanBuild();
} finally {
debugRequestor.deactivate();
}
}

protected final void fullBuild() {
debugRequestor.clearResult();
debugRequestor.activate();
env.fullBuild();
debugRequestor.deactivate();
try {
env.fullBuild();
} finally {
debugRequestor.deactivate();
}
}

protected final void fullBuild(IPath projectPath) {
debugRequestor.clearResult();
debugRequestor.activate();
env.fullBuild(projectPath);
debugRequestor.deactivate();
try {
env.fullBuild(projectPath);
} finally {
debugRequestor.deactivate();
}
}

protected final void incrementalBuild() {
debugRequestor.clearResult();
debugRequestor.activate();
env.incrementalBuild();
debugRequestor.deactivate();
try {
env.incrementalBuild();
} finally {
debugRequestor.deactivate();
}
}

protected final void incrementalBuild(IPath projectPath) {
debugRequestor.clearResult();
debugRequestor.activate();
env.incrementalBuild(projectPath);
debugRequestor.deactivate();
try {
env.incrementalBuild(projectPath);
} finally {
debugRequestor.deactivate();
}
}

protected void executeClass(IPath projectPath, String className, String expectingOutput, String expectedError) {
Expand All @@ -163,7 +182,7 @@ protected void executeClass(IPath projectPath, String className, String expectin
if (expectedError == null && actualError.length() != 0) {
if (actualError.trim().endsWith(
"WARNING: Module [groovy-all] - Unable to load extension class [org.codehaus.groovy.runtime.NioGroovyMethods]")) {
// Allow this it indicates (usually) running the tests with groovy 2.3 on a pre 1.7 vm
// allow this it indicates (usually) running the tests with groovy 2.3 on a pre 1.7 vm
} else {
Assert.fail("unexpected error : " + actualError);
}
Expand All @@ -187,27 +206,25 @@ protected void executeClass(IPath projectPath, String className, String expectin
//--------------------------------------------------------------------------

protected final void expectingCompiledClasses(String... expected) {
String[] actual = (String[]) ReflectionUtils.executeNoArgPrivateMethod(debugRequestor.getClass(), "getCompiledClasses", debugRequestor);
String[] actual = ReflectionUtils.executePrivateMethod(debugRequestor.getClass(), "getCompiledClasses", debugRequestor);
Util.sort(actual);
Util.sort(expected);
expectingCompiling(actual, expected, "unexpected recompiled units. lenExpected=" + expected.length + " lenActual=" + actual.length);
}

private void expectingCompiling(String[] actual, String[] expected, String message) {
StringBuilder actualBuffer = new StringBuilder("{");
for (int i = 0; i < actual.length; i++) {
if (i > 0)
actualBuffer.append(",");
for (int i = 0; i < actual.length; i += 1) {
if (i > 0) actualBuffer.append(",");
actualBuffer.append(actual[i]);
}
actualBuffer.append('}');
actualBuffer.append("}");
StringBuilder expectedBuffer = new StringBuilder("{");
for (int i = 0; i < expected.length; i++) {
if (i > 0)
expectedBuffer.append(",");
for (int i = 0; i < expected.length; i += 1) {
if (i > 0) expectedBuffer.append(",");
expectedBuffer.append(expected[i]);
}
expectedBuffer.append('}');
expectedBuffer.append("}");
Assert.assertEquals(message, expectedBuffer.toString(), actualBuffer.toString());
}

Expand Down Expand Up @@ -235,7 +252,7 @@ protected final void expectingSpecificProblemFor(IPath root, Problem problem) {

protected void expectingSpecificProblemsFor(IPath root, Problem[] problems) {
Problem[] rootProblems = env.getProblemsFor(root);
next : for (int i = 0; i < problems.length; i += 1) {
next: for (int i = 0; i < problems.length; i += 1) {
Problem problem = problems[i];
for (int j = 0; j < rootProblems.length; j += 1) {
Problem rootProblem = rootProblems[j];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2017 the original author or authors.
* Copyright 2009-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -235,8 +235,7 @@ public void testGRECLIPSE887_ImportStatements() throws Exception {
GroovyCompilationUnitDeclaration cud = new GroovyCompilationUnitDeclaration(null, null, -1, null, null, null) {{
GroovyCompilationUnitDeclaration.UnitPopulator cup = new GroovyCompilationUnitDeclaration.UnitPopulator();
ReflectionUtils.setPrivateField(cup.getClass(), "unitDeclaration", cup, this);
ReflectionUtils.executePrivateMethod(cup.getClass(), "createImportDeclarations",
new Class[] {ModuleNode.class}, cup, new Object[] {module});
ReflectionUtils.executePrivateMethod(cup.getClass(), "createImportDeclarations", new Class[] {ModuleNode.class}, cup, new Object[] {module});
}};

assertEquals(module.getImport("List").getStart(), cud.imports[0].declarationSourceStart);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,8 @@ protected final boolean isAtLeastJava(long level) {
return complianceLevel >= level;
}

@SuppressWarnings("unchecked")
protected final Map<String, String> getCompilerOptions() {
return (Map<String, String>) ReflectionUtils.executeNoArgPrivateMethod(AbstractRegressionTest.class, "getCompilerOptions", testDriver);
return ReflectionUtils.executePrivateMethod(AbstractRegressionTest.class, "getCompilerOptions", testDriver);
}

protected final void runConformTest(String[] sources) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public class MultiplexingIndexingParser extends IndexingParser {
public MultiplexingIndexingParser(ISourceElementRequestor requestor, IProblemFactory problemFactory, CompilerOptions options,
boolean reportLocalDeclarations, boolean optimizeStringLiterals, boolean useSourceJavadocParser) {
super(requestor, problemFactory, options, reportLocalDeclarations, optimizeStringLiterals, useSourceJavadocParser);
this.notifier = (SourceElementNotifier) ReflectionUtils.getPrivateField(SourceElementParser.class, "notifier", this);
this.notifier = ReflectionUtils.getPrivateField(SourceElementParser.class, "notifier", this);
this.groovyReportReferenceInfo = reportLocalDeclarations;
this.requestor = requestor;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2017 the original author or authors.
* Copyright 2009-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -77,9 +77,9 @@ protected void notifySourceElementRequestor(ImportReference importReference, boo
super.notifySourceElementRequestor(importReference, isPackage);
if (!isPackage && importReference.annotations != null) {
try {
ImportContainerInfo importContainerInfo = (ImportContainerInfo) ReflectionUtils.getPrivateField(CompilationUnitStructureRequestor.class, "importContainerInfo", compUnitStructureRequestor);
ImportContainerInfo importContainerInfo = ReflectionUtils.getPrivateField(CompilationUnitStructureRequestor.class, "importContainerInfo", compUnitStructureRequestor);
for (Annotation annotation : importReference.annotations) {
IJavaElement[] imports = (IJavaElement[]) ReflectionUtils.throwableExecutePrivateMethod(CompilationUnitStructureRequestor.class, "getChildren", new Class[] {Object.class}, compUnitStructureRequestor, new Object[] {importContainerInfo});
IJavaElement[] imports = ReflectionUtils.throwableExecutePrivateMethod(CompilationUnitStructureRequestor.class, "getChildren", new Class[] {Object.class}, compUnitStructureRequestor, new Object[] {importContainerInfo});

//requestor.acceptAnnotation(Annotation annotation, AnnotatableInfo parentInfo, JavaElement parentHandle);
ReflectionUtils.throwableExecutePrivateMethod(CompilationUnitStructureRequestor.class, "acceptAnnotation",
Expand Down Expand Up @@ -115,7 +115,7 @@ public CompilationUnitDeclaration parseCompilationUnit(ICompilationUnit unit, bo
GroovyParser groovyParser = new GroovyParser(this.groovyParser.requestor, options, problemReporter, false, true);
CompilationUnitDeclaration cud = groovyParser.dietParse(unit, compilationResult);

SourceElementNotifier notifier = (SourceElementNotifier) ReflectionUtils.getPrivateField(SourceElementParser.class, "notifier", this);
SourceElementNotifier notifier = ReflectionUtils.getPrivateField(SourceElementParser.class, "notifier", this);
notifier.notifySourceElementRequestor(cud, 0, unit.getContents().length, groovyReportReferenceInfo, createSourceEnds(cud), Collections.EMPTY_MAP); // we don't care about the @category tag, so pass empty map

return cud;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ private GroovyClassLoader[] getBatchGroovyClassLoaders(CompilerConfiguration com
nameEnvironment = ((INameEnvironment[]) ReflectionUtils.getPrivateField(nameEnvironment.getClass(), "classLibs", nameEnvironment))[0];
}
if (nameEnvironment instanceof FileSystem) {
FileSystem.Classpath[] classpaths = (FileSystem.Classpath[]) ReflectionUtils.getPrivateField(FileSystem.class, "classpaths", nameEnvironment);
FileSystem.Classpath[] classpaths = ReflectionUtils.getPrivateField(FileSystem.class, "classpaths", nameEnvironment);
if (classpaths != null) {
batchLoader = new GroovyClassLoader();
for (FileSystem.Classpath classpath : classpaths) {
Expand Down Expand Up @@ -194,7 +194,7 @@ private static void calculateClasspath(IJavaProject javaProject, Set<String> cla

private static IRuntimeClasspathEntry[] resolveRuntimeClasspathEntry(IRuntimeClasspathEntry classpathEntry, IJavaProject javaProject) throws Exception {
//return JavaRuntime.resolveRuntimeClasspathEntry(classpathEntry, javaProject); // indirect dependency on org.eclipse.debug.core.ILaunchConfiguration
return (IRuntimeClasspathEntry[]) ReflectionUtils.throwableExecutePrivateMethod(JavaRuntime.class, "resolveRuntimeClasspathEntry", new Class[] {IRuntimeClasspathEntry.class, IJavaProject.class}, JavaRuntime.class, new Object[] {classpathEntry, javaProject});
return ReflectionUtils.throwableExecutePrivateMethod(JavaRuntime.class, "resolveRuntimeClasspathEntry", new Class[] {IRuntimeClasspathEntry.class, IJavaProject.class}, JavaRuntime.class, new Object[] {classpathEntry, javaProject});
}

private static String getAbsoluteLocation(IRuntimeClasspathEntry classpathEntry) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2017 the original author or authors.
* Copyright 2009-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -157,7 +157,7 @@ protected TypeBinding toRawType(TypeBinding tb) {
return fb;
} else if (tb instanceof BinaryTypeBinding) {
if (tb.isGenericType()) {
LookupEnvironment le = (LookupEnvironment) ReflectionUtils.getPrivateField(BinaryTypeBinding.class, "environment", tb);
LookupEnvironment le = ReflectionUtils.getPrivateField(BinaryTypeBinding.class, "environment", tb);
return le.convertToRawType(tb, false);
} else {
return tb;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2017 the original author or authors.
* Copyright 2009-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -61,6 +61,6 @@ protected char getHandleMementoDelimiter() {

@Override
public ISourceRange getNameRange() throws JavaModelException {
return (ISourceRange) ReflectionUtils.executeNoArgPrivateMethod(ImportDeclarationElementInfo.class, "getNameRange", getElementInfo());
return ReflectionUtils.executePrivateMethod(ImportDeclarationElementInfo.class, "getNameRange", getElementInfo());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ protected boolean buildStructure(OpenableElementInfo info, IProgressMonitor pm,
createAST = ((Integer) ReflectionUtils.getPrivateField(ASTHolderCUInfo.class, "astLevel", astHolder)) != NO_AST;
resolveBindings = (Boolean) ReflectionUtils.getPrivateField(ASTHolderCUInfo.class, "resolveBindings", astHolder);
reconcileFlags = (Integer) ReflectionUtils.getPrivateField(ASTHolderCUInfo.class, "reconcileFlags", astHolder);
problems = HashMap.class.cast(ReflectionUtils.getPrivateField(ASTHolderCUInfo.class, "problems", astHolder));
problems = ReflectionUtils.getPrivateField(ASTHolderCUInfo.class, "problems", astHolder);
} else {
createAST = false;
resolveBindings = false;
Expand Down
Loading

0 comments on commit 02993bf

Please sign in to comment.