From f194d50476030da1590250fc3f5e9d0d084128c9 Mon Sep 17 00:00:00 2001 From: Andy Clement Date: Wed, 20 Jan 2016 12:43:13 -0800 Subject: [PATCH] refactoring to use generics, reduce warnings --- .../org/aspectj/tools/ajc/AjcTestCase.java | 7 +++--- .../testing/AutowiredXMLBasedAjcTestCase.java | 16 ++++++------- .../org/aspectj/testing/CompileSpec.java | 24 +++++++------------ .../org/aspectj/testing/MakeTestClass.java | 8 ++----- .../org/aspectj/testing/OutputSpec.java | 23 ++++++++---------- .../newsrc/org/aspectj/testing/RunSpec.java | 3 +++ .../newsrc/org/aspectj/testing/WeaveSpec.java | 15 ++++++------ .../aspectj/testing/XMLBasedAjcTestCase.java | 12 +++++----- 8 files changed, 48 insertions(+), 60 deletions(-) diff --git a/org.aspectj.ajdt.core/testsrc/org/aspectj/tools/ajc/AjcTestCase.java b/org.aspectj.ajdt.core/testsrc/org/aspectj/tools/ajc/AjcTestCase.java index d444c4d09..5954c40b4 100644 --- a/org.aspectj.ajdt.core/testsrc/org/aspectj/tools/ajc/AjcTestCase.java +++ b/org.aspectj.ajdt.core/testsrc/org/aspectj/tools/ajc/AjcTestCase.java @@ -231,7 +231,7 @@ public class AjcTestCase extends TestCase { } } if (seeAlsos != null) { - List extraLocations = message.getExtraSourceLocations(); + List extraLocations = message.getExtraSourceLocations(); if (extraLocations.size() != seeAlsos.length) { return false; } @@ -244,9 +244,8 @@ public class AjcTestCase extends TestCase { return true; } - private boolean hasAMatch(List srcLocations, ISourceLocation sLoc) { - for (Iterator iter = srcLocations.iterator(); iter.hasNext();) { - ISourceLocation thisLoc = (ISourceLocation) iter.next(); + private boolean hasAMatch(List srcLocations, ISourceLocation sLoc) { + for (ISourceLocation thisLoc: srcLocations) { if (thisLoc.getLine() == sLoc.getLine()) { if (thisLoc.getSourceFile().getPath().equals(sLoc.getSourceFile().getPath())) { return true; diff --git a/testing/newsrc/org/aspectj/testing/AutowiredXMLBasedAjcTestCase.java b/testing/newsrc/org/aspectj/testing/AutowiredXMLBasedAjcTestCase.java index 718f21f50..467d9f236 100644 --- a/testing/newsrc/org/aspectj/testing/AutowiredXMLBasedAjcTestCase.java +++ b/testing/newsrc/org/aspectj/testing/AutowiredXMLBasedAjcTestCase.java @@ -36,7 +36,7 @@ import org.aspectj.tools.ajc.Ajc; */ public abstract class AutowiredXMLBasedAjcTestCase extends XMLBasedAjcTestCase { - private Map testMap = new HashMap(); + private Map testMap = new HashMap(); public void addTest(AjcTest test) { testMap.put(test.getTitle(), test); @@ -45,11 +45,11 @@ public abstract class AutowiredXMLBasedAjcTestCase extends XMLBasedAjcTestCase { /* * Return a map from (String) test title -> AjcTest */ - protected Map getSuiteTests() { + protected Map getSuiteTests() { return testMap; } - public static Test loadSuite(Class testCaseClass) { + public static Test loadSuite(Class testCaseClass) { TestSuite suite = new TestSuite(testCaseClass.getName()); //suite.addTestSuite(testCaseClass); @@ -66,20 +66,20 @@ public abstract class AutowiredXMLBasedAjcTestCase extends XMLBasedAjcTestCase { } wired.ajc = new Ajc(); - Map ajTests = wired.getSuiteTests(); + Map ajTests = wired.getSuiteTests(); - for (Iterator iterator = ajTests.entrySet().iterator(); iterator.hasNext();) { - final Map.Entry entry = (Map.Entry) iterator.next(); + for (Iterator> iterator = ajTests.entrySet().iterator(); iterator.hasNext();) { + final Map.Entry entry = iterator.next(); suite.addTest( new TestCase(entry.getKey().toString()) { protected void runTest() { - ((AjcTest) entry.getValue()).runTest(wired); + entry.getValue().runTest(wired); } public String getName() { - return (String) entry.getKey(); + return entry.getKey(); } } ); diff --git a/testing/newsrc/org/aspectj/testing/CompileSpec.java b/testing/newsrc/org/aspectj/testing/CompileSpec.java index 540c678a5..714158a11 100644 --- a/testing/newsrc/org/aspectj/testing/CompileSpec.java +++ b/testing/newsrc/org/aspectj/testing/CompileSpec.java @@ -1,5 +1,5 @@ /* ******************************************************************* - * Copyright (c) 2004 IBM Corporation + * Copyright (c) 2004,2016 IBM Corporation * All rights reserved. * This program and the accompanying materials are made available * under the terms of the Eclipse Public License v1.0 @@ -13,7 +13,6 @@ package org.aspectj.testing; import java.io.File; import java.util.ArrayList; -import java.util.Iterator; import java.util.List; import java.util.StringTokenizer; @@ -21,14 +20,12 @@ import org.aspectj.tools.ajc.AjcTestCase; import org.aspectj.tools.ajc.CompilationResult; /** - * @author colyer - * - * TODO To change the template for this generated type comment go to - * Window - Preferences - Java - Code Style - Code Templates + * @author Adrian Colyer + * @author Andy Clement */ public class CompileSpec implements ITestStep { - private List expected = new ArrayList(); + private List expected = new ArrayList(); private String files; private boolean includeClassesDir; @@ -254,8 +251,8 @@ public class CompileSpec implements ITestStep { args.append(getExtdirs()); args.append(" "); } - List fileList = new ArrayList(); - List jarList = new ArrayList(); + List fileList = new ArrayList(); + List jarList = new ArrayList(); // convention that any jar on file list should be added to inpath String files = getFiles(); if (files == null) files = ""; @@ -271,15 +268,13 @@ public class CompileSpec implements ITestStep { if ((getInpath() != null) || !jarList.isEmpty()) { args.append("-inpath "); if (getInpath() != null) args.append(getInpath()); - for (Iterator iter = jarList.iterator(); iter.hasNext();) { - String jar = (String) iter.next(); + for (String jar: jarList) { args.append(File.pathSeparator); args.append(jar); } args.append(" "); } - for (Iterator iter = fileList.iterator(); iter.hasNext();) { - String file = (String) iter.next(); + for (String file: fileList) { args.append(file); args.append(" "); } @@ -298,8 +293,7 @@ public class CompileSpec implements ITestStep { List errors = new ArrayList(); List fails = new ArrayList(); List weaveInfos = new ArrayList(); - for (Iterator iter = expected.iterator(); iter.hasNext();) { - ExpectedMessageSpec exMsg = (ExpectedMessageSpec) iter.next(); + for (ExpectedMessageSpec exMsg: expected) { String kind = exMsg.getKind(); if (kind.equals("info")) { if (infos == null) infos = new ArrayList(); diff --git a/testing/newsrc/org/aspectj/testing/MakeTestClass.java b/testing/newsrc/org/aspectj/testing/MakeTestClass.java index b4634a697..721cb623b 100644 --- a/testing/newsrc/org/aspectj/testing/MakeTestClass.java +++ b/testing/newsrc/org/aspectj/testing/MakeTestClass.java @@ -1,8 +1,6 @@ /* * Created on 02-Aug-2004 * - * TODO To change the template for this generated file go to - * Window - Preferences - Java - Code Style - Code Templates */ package org.aspectj.testing; @@ -12,7 +10,6 @@ import java.io.InputStreamReader; import java.io.PrintStream; import java.text.NumberFormat; import java.util.ArrayList; -import java.util.Iterator; import java.util.List; import org.apache.commons.digester.Digester; @@ -63,7 +60,7 @@ public class MakeTestClass { private static final String FOOTER = "}\n"; - private List tests = new ArrayList(); + private List tests = new ArrayList(); private String className; private String suiteFile; @@ -97,8 +94,7 @@ public class MakeTestClass { int testNo = 1; NumberFormat nf = NumberFormat.getInstance(); nf.setMinimumIntegerDigits(3); - for (Iterator iter = tests.iterator(); iter.hasNext();) { - AjcTest test = (AjcTest) iter.next(); + for (AjcTest test: tests) { out.println(); out.print(" public void test"); out.print(nf.format(testNo++)); diff --git a/testing/newsrc/org/aspectj/testing/OutputSpec.java b/testing/newsrc/org/aspectj/testing/OutputSpec.java index b4bba2a99..ffb3362f3 100644 --- a/testing/newsrc/org/aspectj/testing/OutputSpec.java +++ b/testing/newsrc/org/aspectj/testing/OutputSpec.java @@ -19,7 +19,7 @@ import org.aspectj.tools.ajc.AjcTestCase; public class OutputSpec { - private List expectedOutputLines = new ArrayList(); + private List expectedOutputLines = new ArrayList(); public void addLine(OutputLine line) { expectedOutputLines.add(line.getText()); @@ -39,8 +39,7 @@ public class OutputSpec { StringTokenizer strTok = new StringTokenizer(output,"\n"); if (strTok.countTokens() == expectedOutputLines.size()) { matches = true; - for (Iterator iter = expectedOutputLines.iterator(); iter.hasNext();) { - String line = (String) iter.next(); + for (String line: expectedOutputLines) { lineNo++; String outputLine = strTok.nextToken().trim(); /* Avoid trying to match on ajSandbox source names that appear in messages */ @@ -56,18 +55,18 @@ public class OutputSpec { } public void unorderedMatchAgainst(String output) { - List outputFound = getOutputFound(output); + List outputFound = getOutputFound(output); if(outputFound.size() != expectedOutputLines.size()) { createFailureMessage(output, -1, outputFound.size()); return; } - List expected = new ArrayList(); + List expected = new ArrayList(); expected.addAll(expectedOutputLines); - List found = new ArrayList(); + List found = new ArrayList(); found.addAll(outputFound); - for (Iterator iterator = outputFound.iterator(); iterator.hasNext();) { + for (Iterator iterator = outputFound.iterator(); iterator.hasNext();) { String lineFound = (String) iterator.next(); - for (Iterator iterator2 = expectedOutputLines.iterator(); iterator2.hasNext();) { + for (Iterator iterator2 = expectedOutputLines.iterator(); iterator2.hasNext();) { String lineExpected = (String) iterator2.next(); if (lineFound.indexOf(lineExpected)!= -1) { found.remove(lineFound); @@ -84,9 +83,7 @@ public class OutputSpec { private void createFailureMessage(String output, int lineNo, int sizeFound) { StringBuffer failMessage = new StringBuffer(); failMessage.append("\n expecting output:\n"); - int l = 0; - for (Iterator iter = expectedOutputLines.iterator(); iter.hasNext();) { - String line = (String) iter.next(); + for (String line: expectedOutputLines) { failMessage.append(line); failMessage.append("\n"); } @@ -102,8 +99,8 @@ public class OutputSpec { AjcTestCase.fail(failMessage.toString()); } - private List getOutputFound(String output) { - List found = new ArrayList(); + private List getOutputFound(String output) { + List found = new ArrayList(); StringTokenizer strTok = new StringTokenizer(output,"\n"); while(strTok.hasMoreTokens()) { String outputLine = strTok.nextToken().trim(); diff --git a/testing/newsrc/org/aspectj/testing/RunSpec.java b/testing/newsrc/org/aspectj/testing/RunSpec.java index 8a2fea8b3..2309788bc 100644 --- a/testing/newsrc/org/aspectj/testing/RunSpec.java +++ b/testing/newsrc/org/aspectj/testing/RunSpec.java @@ -41,6 +41,9 @@ public class RunSpec implements ITestStep { private String vmargs; private String usefullltw; + public String toString() { + return "RunSpec: Running '"+classToRun+"' in directory '"+baseDir+"'. Classpath of '"+cpath+"'"; + } public RunSpec() { } diff --git a/testing/newsrc/org/aspectj/testing/WeaveSpec.java b/testing/newsrc/org/aspectj/testing/WeaveSpec.java index 31fb4d70a..2793b63ac 100644 --- a/testing/newsrc/org/aspectj/testing/WeaveSpec.java +++ b/testing/newsrc/org/aspectj/testing/WeaveSpec.java @@ -18,7 +18,6 @@ import java.io.IOException; import java.io.OutputStream; import java.util.ArrayList; import java.util.Collections; -import java.util.Iterator; import java.util.List; import java.util.StringTokenizer; import java.util.jar.JarEntry; @@ -37,7 +36,7 @@ public class WeaveSpec extends CompileSpec { private String classesFiles; private String aspectsFiles; - private List classFilesFromClasses; + private List classFilesFromClasses; /* (non-Javadoc) * @see org.aspectj.testing.ITestStep#execute(org.aspectj.tools.ajc.AjcTestCase) @@ -46,7 +45,7 @@ public class WeaveSpec extends CompileSpec { String failMessage = "test \"" + getTest().getTitle() + "\" failed"; try { File base = new File(getBaseDir()); - classFilesFromClasses = new ArrayList(); + classFilesFromClasses = new ArrayList(); setFiles(classesFiles); String[] args = buildArgs(); CompilationResult result = inTestCase.ajc(base,args); @@ -95,13 +94,12 @@ public class WeaveSpec extends CompileSpec { File outJar = new File(inDir,name); FileOutputStream fos = new FileOutputStream(outJar); JarOutputStream jarOut = new JarOutputStream(fos); - List classFiles = new ArrayList(); - List toExclude = isClasses ? Collections.EMPTY_LIST : classFilesFromClasses; + List classFiles = new ArrayList(); + List toExclude = isClasses ? Collections.emptyList() : classFilesFromClasses; collectClassFiles(inDir,classFiles,toExclude); if (isClasses) classFilesFromClasses = classFiles; String prefix = inDir.getPath() + File.separator; - for (Iterator iter = classFiles.iterator(); iter.hasNext();) { - File f = (File) iter.next(); + for (File f: classFiles) { String thisPath = f.getPath(); if (thisPath.startsWith(prefix)) { thisPath = thisPath.substring(prefix.length()); @@ -115,7 +113,7 @@ public class WeaveSpec extends CompileSpec { jarOut.close(); } - private void collectClassFiles(File inDir, List inList, List toExclude) { + private void collectClassFiles(File inDir, List inList, List toExclude) { File[] contents = inDir.listFiles(); for (int i = 0; i < contents.length; i++) { if (contents[i].getName().endsWith(".class")) { @@ -135,6 +133,7 @@ public class WeaveSpec extends CompileSpec { while((read = fis.read(buf)) != -1) { dest.write(buf,0,read); } + fis.close(); } private String[] buildWeaveArgs() { diff --git a/testing/newsrc/org/aspectj/testing/XMLBasedAjcTestCase.java b/testing/newsrc/org/aspectj/testing/XMLBasedAjcTestCase.java index 14952a537..6705a5847 100644 --- a/testing/newsrc/org/aspectj/testing/XMLBasedAjcTestCase.java +++ b/testing/newsrc/org/aspectj/testing/XMLBasedAjcTestCase.java @@ -50,10 +50,10 @@ import org.aspectj.util.FileUtil; */ public abstract class XMLBasedAjcTestCase extends AjcTestCase { - private static Map testMap = new HashMap(); + private static Map testMap = new HashMap(); private static boolean suiteLoaded = false; private AjcTest currentTest = null; - private Stack clearTestAfterRun = new Stack(); + private Stack clearTestAfterRun = new Stack(); public XMLBasedAjcTestCase() { } @@ -71,7 +71,7 @@ public abstract class XMLBasedAjcTestCase extends AjcTestCase { * @param testCaseClass * @return */ - public static Test loadSuite(Class testCaseClass) { + public static Test loadSuite(Class testCaseClass) { TestSuite suite = new TestSuite(testCaseClass.getName()); suite.addTestSuite(testCaseClass); TestSetup wrapper = new TestSetup(suite) { @@ -106,7 +106,7 @@ public abstract class XMLBasedAjcTestCase extends AjcTestCase { /* * Return a map from (String) test title -> AjcTest */ - protected Map getSuiteTests() { + protected Map getSuiteTests() { return testMap; } @@ -174,7 +174,7 @@ public abstract class XMLBasedAjcTestCase extends AjcTestCase { if (clearTestAfterRun.isEmpty()) { return false; } - boolean result = ((Boolean) clearTestAfterRun.peek()).booleanValue(); + boolean result = clearTestAfterRun.peek().booleanValue(); if (pop) { clearTestAfterRun.pop(); } @@ -241,7 +241,7 @@ public abstract class XMLBasedAjcTestCase extends AjcTestCase { protected void setUp() throws Exception { super.setUp(); if (!suiteLoaded) { - testMap = new HashMap(); + testMap = new HashMap(); System.out.println("LOADING SUITE: " + getSpecFile().getPath()); Digester d = getDigester(); try { -- 2.39.5