From 72194b7982ddfa8e9864d0a9934905bb76b90f33 Mon Sep 17 00:00:00 2001 From: Lars Grefer Date: Sat, 8 Aug 2020 03:06:37 +0200 Subject: 'for' loop replaceable with enhanced 'for' loop Reports for loops which iterate over collections or arrays, and can be replaced with an enhanced for loop (i.e. the foreach iteration syntax). Signed-off-by: Lars Grefer --- .../main/java/org/aspectj/weaver/loadtime/Aj.java | 6 +- .../weaver/loadtime/ClassLoaderWeavingAdaptor.java | 37 ++-- .../weaver/loadtime/ConcreteAspectCodeGen.java | 191 ++++++++++----------- .../java/org/aspectj/weaver/loadtime/Options.java | 6 +- .../aspectj/weaver/loadtime/JRockitAgentTest.java | 4 +- 5 files changed, 117 insertions(+), 127 deletions(-) (limited to 'loadtime') diff --git a/loadtime/src/main/java/org/aspectj/weaver/loadtime/Aj.java b/loadtime/src/main/java/org/aspectj/weaver/loadtime/Aj.java index 652ea0932..abd8ecc87 100644 --- a/loadtime/src/main/java/org/aspectj/weaver/loadtime/Aj.java +++ b/loadtime/src/main/java/org/aspectj/weaver/loadtime/Aj.java @@ -199,8 +199,7 @@ public class Aj implements ClassPreProcessor { System.err.println("Weaver adaptors before queue processing:"); Map m = WeaverContainer.weavingAdaptors; Set keys = m.keySet(); - for (Iterator iterator = keys.iterator(); iterator.hasNext();) { - Object object = iterator.next(); + for (Object object : keys) { System.err.println(object + " = " + WeaverContainer.weavingAdaptors.get(object)); } } @@ -223,8 +222,7 @@ public class Aj implements ClassPreProcessor { System.err.println("Weaver adaptors after queue processing:"); Map m = WeaverContainer.weavingAdaptors; Set keys = m.keySet(); - for (Iterator iterator = keys.iterator(); iterator.hasNext();) { - Object object = iterator.next(); + for (Object object : keys) { System.err.println(object + " = " + WeaverContainer.weavingAdaptors.get(object)); } } diff --git a/loadtime/src/main/java/org/aspectj/weaver/loadtime/ClassLoaderWeavingAdaptor.java b/loadtime/src/main/java/org/aspectj/weaver/loadtime/ClassLoaderWeavingAdaptor.java index 21f81720e..173310591 100644 --- a/loadtime/src/main/java/org/aspectj/weaver/loadtime/ClassLoaderWeavingAdaptor.java +++ b/loadtime/src/main/java/org/aspectj/weaver/loadtime/ClassLoaderWeavingAdaptor.java @@ -219,8 +219,7 @@ public class ClassLoaderWeavingAdaptor extends WeavingAdaptor { */ List getAspectClassNames(List definitions) { List aspects = new LinkedList(); - for (Iterator it = definitions.iterator(); it.hasNext(); ) { - Definition def = it.next(); + for (Definition def : definitions) { List defAspects = def.getAspectClassNames(); if (defAspects != null) { aspects.addAll(defAspects); @@ -598,9 +597,9 @@ public class ClassLoaderWeavingAdaptor extends WeavingAdaptor { private void registerIncludeExclude(final BcelWeaver weaver, final ClassLoader loader, final List definitions) { String fastMatchInfo = null; for (Definition definition : definitions) { - for (Iterator iterator1 = definition.getIncludePatterns().iterator(); iterator1.hasNext();) { + for (String value : definition.getIncludePatterns()) { hasIncludes = true; - String include = iterator1.next(); + String include = value; fastMatchInfo = looksLikeStartsWith(include); if (fastMatchInfo != null) { includeStartsWith.add(fastMatchInfo); @@ -613,9 +612,9 @@ public class ClassLoaderWeavingAdaptor extends WeavingAdaptor { includeTypePattern.add(includePattern); } } - for (Iterator iterator1 = definition.getExcludePatterns().iterator(); iterator1.hasNext();) { + for (String s : definition.getExcludePatterns()) { hasExcludes = true; - String exclude = iterator1.next(); + String exclude = s; fastMatchInfo = looksLikeStartsWith(exclude); if (fastMatchInfo != null) { excludeStartsWith.add(fastMatchInfo); @@ -628,8 +627,8 @@ public class ClassLoaderWeavingAdaptor extends WeavingAdaptor { } else if (exclude .equals("org.codehaus.groovy..* && !org.codehaus.groovy.grails.web.servlet.mvc.SimpleGrailsController*")) { // TODO need a more sophisticated analysis here, to allow for similar situations - excludeSpecial.add(new String[] { "org.codehaus.groovy.", - "org.codehaus.groovy.grails.web.servlet.mvc.SimpleGrailsController" }); + excludeSpecial.add(new String[]{"org.codehaus.groovy.", + "org.codehaus.groovy.grails.web.servlet.mvc.SimpleGrailsController"}); // for the related test: // } else if (exclude.equals("testdata..* && !testdata.sub.Oran*")) { // excludeSpecial.add(new String[] { "testdata.", "testdata.sub.Oran" }); @@ -737,8 +736,7 @@ public class ClassLoaderWeavingAdaptor extends WeavingAdaptor { */ private void registerDump(final BcelWeaver weaver, final ClassLoader loader, final List definitions) { for (Definition definition : definitions) { - for (Iterator iterator1 = definition.getDumpPatterns().iterator(); iterator1.hasNext();) { - String dump = iterator1.next(); + for (String dump : definition.getDumpPatterns()) { TypePattern pattern = new PatternParser(dump).parseTypePattern(); dumpTypePattern.add(pattern); } @@ -828,9 +826,9 @@ public class ClassLoaderWeavingAdaptor extends WeavingAdaptor { } } boolean fastAccept = false;// defaults to false if no fast include - for (int i = 0; i < includeStartsWith.size(); i++) { + for (String s : includeStartsWith) { didSomeIncludeMatching = true; - fastAccept = fastClassName.startsWith(includeStartsWith.get(i)); + fastAccept = fastClassName.startsWith(s); if (fastAccept) { return true; } @@ -866,9 +864,9 @@ public class ClassLoaderWeavingAdaptor extends WeavingAdaptor { } } } - for (int i = 0; i < includeStartsWith.size(); i++) { + for (String s : includeStartsWith) { didSomeIncludeMatching = true; - boolean fastaccept = fastClassName.startsWith(includeStartsWith.get(i)); + boolean fastaccept = fastClassName.startsWith(s); if (fastaccept) { return true; } @@ -898,14 +896,14 @@ public class ClassLoaderWeavingAdaptor extends WeavingAdaptor { // still try to avoid ResolvedType if we have simple patterns // EXCLUDE: if one match then reject String fastClassName = aspectClassName.replace('/', '.').replace('.', '$'); - for (int i = 0; i < aspectExcludeStartsWith.size(); i++) { - if (fastClassName.startsWith(aspectExcludeStartsWith.get(i))) { + for (String value : aspectExcludeStartsWith) { + if (fastClassName.startsWith(value)) { return false; } } // INCLUDE: if one match then accept - for (int i = 0; i < aspectIncludeStartsWith.size(); i++) { - if (fastClassName.startsWith(aspectIncludeStartsWith.get(i))) { + for (String s : aspectIncludeStartsWith) { + if (fastClassName.startsWith(s)) { return true; } } @@ -946,8 +944,7 @@ public class ClassLoaderWeavingAdaptor extends WeavingAdaptor { // TODO AV - optimize for className.startWith only ResolvedType classInfo = weaver.getWorld().resolve(UnresolvedType.forName(className), true); // dump - for (Iterator iterator = dumpTypePattern.iterator(); iterator.hasNext();) { - TypePattern typePattern = iterator.next(); + for (TypePattern typePattern : dumpTypePattern) { if (typePattern.matchesStatically(classInfo)) { // dump match return true; diff --git a/loadtime/src/main/java/org/aspectj/weaver/loadtime/ConcreteAspectCodeGen.java b/loadtime/src/main/java/org/aspectj/weaver/loadtime/ConcreteAspectCodeGen.java index 15e1e9fd4..6f5b58702 100644 --- a/loadtime/src/main/java/org/aspectj/weaver/loadtime/ConcreteAspectCodeGen.java +++ b/loadtime/src/main/java/org/aspectj/weaver/loadtime/ConcreteAspectCodeGen.java @@ -173,8 +173,7 @@ public class ConcreteAspectCodeGen { } if (parent.isParameterizedType()) { UnresolvedType[] typeParameters = parent.getTypeParameters(); - for (int i = 0; i < typeParameters.length; i++) { - UnresolvedType typeParameter = typeParameters[i]; + for (UnresolvedType typeParameter : typeParameters) { if (typeParameter instanceof ResolvedType && ((ResolvedType) typeParameter).isMissing()) { reportError("Unablet to resolve type parameter '" + typeParameter.getName() + "' from " + stringify()); return false; @@ -306,8 +305,7 @@ public class ConcreteAspectCodeGen { } ResolvedMember[] rms = type.getDeclaredMethods(); if (rms != null) { - for (int i = 0; i < rms.length; i++) { - ResolvedMember member = rms[i]; + for (ResolvedMember member : rms) { String key = member.getName() + member.getSignature(); if (member.isAbstract()) { collector.put(key, member); @@ -340,8 +338,8 @@ public class ConcreteAspectCodeGen { if (as == null || as.length == 0) { return false; } - for (int i = 0; i < as.length; i++) { - if (as[i].getTypeSignature().equals("Lorg/aspectj/lang/annotation/Pointcut;")) { + for (AnnotationAJ a : as) { + if (a.getTypeSignature().equals("Lorg/aspectj/lang/annotation/Pointcut;")) { return true; } } @@ -438,8 +436,7 @@ public class ConcreteAspectCodeGen { cbody.append(InstructionConstants.RETURN); cg.addMethodGen(init); - for (Iterator it = concreteAspect.pointcuts.iterator(); it.hasNext();) { - Definition.Pointcut abstractPc = (Definition.Pointcut) it.next(); + for (Definition.Pointcut abstractPc : concreteAspect.pointcuts) { // TODO AV - respect visibility instead of opening up as public? LazyMethodGen mg = new LazyMethodGen(Modifier.PUBLIC, Type.VOID, abstractPc.name, EMPTY_TYPES, EMPTY_STRINGS, cg); SimpleElementValue svg = new SimpleElementValue(ElementValue.STRING, cg.getConstantPool(), abstractPc.expression); @@ -646,113 +643,113 @@ public class ConcreteAspectCodeGen { value = value.substring(equalsIndex+1).trim(); } boolean keyIsOk = false; - for (int m=0;m iterator = flags.iterator(); iterator.hasNext();) { - String arg = iterator.next(); + for (String arg : flags) { if (arg.startsWith(OPTIONVALUED_messageHandler)) { if (arg.length() > OPTIONVALUED_messageHandler.length()) { String handlerClass = arg.substring(OPTIONVALUED_messageHandler.length()).trim(); @@ -77,8 +76,7 @@ public class Options { } // configure the other options - for (Iterator iterator = flags.iterator(); iterator.hasNext();) { - String arg = iterator.next(); + for (String arg : flags) { if (arg.equals(OPTION_15)) { weaverOption.java5 = true; } else if (arg.equalsIgnoreCase(OPTION_lazyTjp)) { diff --git a/loadtime/src/test/java/org/aspectj/weaver/loadtime/JRockitAgentTest.java b/loadtime/src/test/java/org/aspectj/weaver/loadtime/JRockitAgentTest.java index 1c5a8536c..cd5590914 100644 --- a/loadtime/src/test/java/org/aspectj/weaver/loadtime/JRockitAgentTest.java +++ b/loadtime/src/test/java/org/aspectj/weaver/loadtime/JRockitAgentTest.java @@ -94,9 +94,9 @@ public class JRockitAgentTest extends TestCase { super(clone.getParent()); URL[] urls = clone.getURLs(); - for (int i = 0; i < urls.length; i++) { + for (URL value : urls) { Object pathElement; - URL url = urls[i]; + URL url = value; if (debug) System.out.println("JRockitClassLoader.JRockitClassLoader() url=" + url.getPath()); File file = new File(encode(url.getFile())); -- cgit v1.2.3