Skip to content

Commit

Permalink
Merge pull request ballerina-platform#42905 from Shadow-Devil/for-loo…
Browse files Browse the repository at this point in the history
…p-to-enhanced-for

[Refactoring] Replace 'for loop' with 'enhanced for'
  • Loading branch information
warunalakshitha authored Sep 6, 2024
2 parents 54a7ef1 + 1663d93 commit a6a3526
Show file tree
Hide file tree
Showing 31 changed files with 112 additions and 124 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,7 @@ public static Object parseMapExpressionStringValue(String exprValue, BLink paren
}
CycleUtils.Node node = new CycleUtils.Node(eleMap, parent);
Set<Type> typeSet = new HashSet<>();
for (int i = 0; i < list.size(); i++) {
String e = list.get(i);
for (String e : list) {
int colonIndex = e.indexOf(':');
int quotesCount = 0;
for (int j = 0; j < e.length(); j++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,31 +188,31 @@ public class XmlValidator {
}

// remove special characters
for (int i = 0; i < specialChar.length; i++) {
CHARS[specialChar[i]] = (byte) (CHARS[specialChar[i]] & ~MASK_CONTENT);
for (int k : specialChar) {
CHARS[k] = (byte) (CHARS[k] & ~MASK_CONTENT);
}

// set space characters
for (int i = 0; i < spaceChar.length; i++) {
CHARS[spaceChar[i]] |= MASK_SPACE;
for (int k : spaceChar) {
CHARS[k] |= MASK_SPACE;
}

// set name start characters
for (int i = 0; i < nameStartChar.length; i++) {
CHARS[nameStartChar[i]] |= MASK_NAME_START | MASK_NAME | MASK_NCNAME_START | MASK_NCNAME;
for (int k : nameStartChar) {
CHARS[k] |= MASK_NAME_START | MASK_NAME | MASK_NCNAME_START | MASK_NCNAME;
}
for (int i = 0; i < letterRange.length; i += 2) {
for (int j = letterRange[i]; j <= letterRange[i + 1]; j++) {
CHARS[j] |= MASK_NAME_START | MASK_NAME | MASK_NCNAME_START | MASK_NCNAME;
}
}
for (int i = 0; i < letterChar.length; i++) {
CHARS[letterChar[i]] |= MASK_NAME_START | MASK_NAME | MASK_NCNAME_START | MASK_NCNAME;
for (int k : letterChar) {
CHARS[k] |= MASK_NAME_START | MASK_NAME | MASK_NCNAME_START | MASK_NCNAME;
}

// set name characters
for (int i = 0; i < nameChar.length; i++) {
CHARS[nameChar[i]] |= MASK_NAME | MASK_NCNAME;
for (int k : nameChar) {
CHARS[k] |= MASK_NAME | MASK_NCNAME;
}
for (int i = 0; i < digitRange.length; i += 2) {
for (int j = digitRange[i]; j <= digitRange[i + 1]; j++) {
Expand All @@ -224,24 +224,24 @@ public class XmlValidator {
CHARS[j] |= MASK_NAME | MASK_NCNAME;
}
}
for (int i = 0; i < combiningCharChar.length; i++) {
CHARS[combiningCharChar[i]] |= MASK_NAME | MASK_NCNAME;
for (int k : combiningCharChar) {
CHARS[k] |= MASK_NAME | MASK_NCNAME;
}
for (int i = 0; i < extenderRange.length; i += 2) {
for (int j = extenderRange[i]; j <= extenderRange[i + 1]; j++) {
CHARS[j] |= MASK_NAME | MASK_NCNAME;
}
}
for (int i = 0; i < extenderChar.length; i++) {
CHARS[extenderChar[i]] |= MASK_NAME | MASK_NCNAME;
for (int k : extenderChar) {
CHARS[k] |= MASK_NAME | MASK_NCNAME;
}

// remove ':' from allowable MASK_NCNAME_START and MASK_NCNAME chars
CHARS[':'] &= ~(MASK_NCNAME_START | MASK_NCNAME);

// set Pubid characters
for (int i = 0; i < pubidChar.length; i++) {
CHARS[pubidChar[i]] |= MASK_PUBID;
for (int k : pubidChar) {
CHARS[k] |= MASK_PUBID;
}
for (int i = 0; i < pubidRange.length; i += 2) {
for (int j = pubidRange[i]; j <= pubidRange[i + 1]; j++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,10 @@ private TransactionLocalContext createTrxContextBranch(TransactionLocalContext c
}

public void handleChannelError(ChannelDetails[] channels, ErrorValue error) {
for (int i = 0; i < channels.length; i++) {
ChannelDetails channelDetails = channels[i];
for (ChannelDetails channelDetails : channels) {
WorkerDataChannel channel = getWorkerDataChannel(channelDetails);

if (channels[i].send) {
if (channelDetails.send) {
channel.setSendError(error);
} else {
channel.setReceiveError(error);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import io.ballerina.runtime.api.Module;
import io.ballerina.runtime.api.types.MethodType;
import io.ballerina.runtime.api.types.Parameter;
import io.ballerina.runtime.api.types.ResourceMethodType;
import io.ballerina.runtime.api.types.Type;

Expand Down Expand Up @@ -53,9 +54,9 @@ public String toString() {
}
StringJoiner sj = new StringJoiner(",", "resource function " + accessor + " " + rp.toString() +
"(", ") returns (" + type.retType + ")");
for (int i = 0; i < parameters.length; i++) {
Type type = parameters[i].type;
sj.add(type.getName() + " " + parameters[i].name);
for (Parameter parameter : parameters) {
Type type = parameter.type;
sj.add(type.getName() + " " + parameter.name);
}
return sj.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -410,8 +410,8 @@ private String createExpressionStringValueDataEntry(Iterator<Map.Entry<Long, Lis
StringJoiner sj = new StringJoiner(",");
StringJoiner keyJoiner = new StringJoiner(",");
String[] keysList = tableType.getFieldNames();
for (int i = 0; i < keysList.length; i++) {
keyJoiner.add(keysList[i]);
for (String string : keysList) {
keyJoiner.add(string);
}
while (itr.hasNext()) {
Map.Entry<Long, List<V>> struct = itr.next();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -643,8 +643,8 @@ private void initializeIteratorNextReturnType() {
childrenType = children.get(0).getType();
} else {
Set<Type> types = new HashSet<>();
for (int i = 0; i < children.size(); i++) {
types.add(children.get(i).getType());
for (BXml child : children) {
types.add(child.getType());
}
childrenType = new BUnionType(new ArrayList<>(types));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ public void testVerifyJavaCompatibilityNegative() {
// versions in the format" {compiledVersion, runtimeVersion, supportedVersionRange}
String[][] versions = { { "1.8.0_144", "1.7.0_1", "1.8.*" }, { "1.8.0_144", "9", "1.8.*" },
{ "9.0.1", "10.0.2.1", "9.0.*" }, { "9", "10.0.2.1", "9.0.*" }, { "10", "9", "10.0.*" } };
for (int i = 0; i < versions.length; i++) {
testVersionsNegative(versions[i][0], versions[i][1], versions[i][2]);
for (String[] version : versions) {
testVersionsNegative(version[0], version[1], version[2]);
}
}

Expand All @@ -59,8 +59,8 @@ public void testVerifyJavaCompatibilityPositive() {
// versions in the format" {compiledVersion, runtimeVersion}
String[][] versions = { { "1.8.0_144", "1.8.0_144" }, { "1.8.0_144", "1.8.0_221" }, { "9.0_221", "9" },
{ "9", "9.0_221" }, { "10.0.1", "10.0.2.1" }, { "10", "10" } };
for (int i = 0; i < versions.length; i++) {
testVersionsPositive(versions[i][0], versions[i][1]);
for (String[] version : versions) {
testVersionsPositive(version[0], version[1]);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,12 +191,12 @@ public static void createReflectConfig(Path nativeConfigPath, Package currentPac
}
HashSet<String> methodSet = getMethodSet(qualifiedTestClass);
originalTestFileRefConfClz = new ReflectConfigClass(qualifiedTestClassName);
for (int i = 0; i < mockedFunctions.length; i++) {
if (!methodSet.contains(mockedFunctions[i])) {
for (String mockedFunction : mockedFunctions) {
if (!methodSet.contains(mockedFunction)) {
continue;
}
originalTestFileRefConfClz.addReflectConfigClassMethod(
new ReflectConfigClassMethod(mockedFunctions[i]));
new ReflectConfigClassMethod(mockedFunction));
originalTestFileRefConfClz.setUnsafeAllocated(true);
originalTestFileRefConfClz.setAllDeclaredFields(true);
originalTestFileRefConfClz.setQueryAllDeclaredMethods(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ private void overwriteSize(byte[] bytes) {

private void writeToStream(DataOutputStream stream) throws IOException {
stream.writeInt(-1);

// cpEntries are modified while iterating over them
// noinspection ForLoopReplaceableByForEach
for (int i = 0; i < cpEntries.size(); i++) {
CPEntry cpEntry = cpEntries.get(i);
stream.writeByte(cpEntry.entryType.value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ void rewritePackageAnnotations(BLangPackage pkgNode, SymbolEnv env) {
private void defineClassAnnotations(BLangPackage pkgNode, SymbolEnv env2, BLangFunction initFunction) {
List<TopLevelNode> topLevelNodes = pkgNode.topLevelNodes;

// topLevelNodes are modified while iterating over them
// noinspection ForLoopReplaceableByForEach
for (int i = 0, topLevelNodesSize = topLevelNodes.size(); i < topLevelNodesSize; i++) {
TopLevelNode topLevelNode = topLevelNodes.get(i);
if (topLevelNode.getKind() != NodeKind.CLASS_DEFN) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -978,8 +978,8 @@ private List<BLangVariable> desugarGlobalVariables(BLangPackage pkgNode, BLangBl
List<BLangStatement> statements = ((BLangBlockStmt) blockStatementNode).stmts;

int statementSize = statements.size();
for (int i = 0; i < statementSize; i++) {
addToGlobalVariableList(statements.get(i), initFnBody, globalVar, desugaredGlobalVarList);
for (BLangStatement bLangStatement : statements) {
addToGlobalVariableList(bLangStatement, initFnBody, globalVar, desugaredGlobalVarList);
}
break;
case RECORD_VARIABLE:
Expand Down Expand Up @@ -4374,8 +4374,8 @@ private List<String> getKeysToRemove(BLangMappingMatchPattern mappingMatchPatter
private List<String> getKeysToRemove(BLangMappingBindingPattern mappingBindingPattern) {
List<String> keysToRemove = new ArrayList<>();
List<BLangFieldBindingPattern> fieldBindingPatterns = mappingBindingPattern.fieldBindingPatterns;
for (int i = 0; i < fieldBindingPatterns.size(); i++) {
keysToRemove.add(fieldBindingPatterns.get(i).fieldName.value);
for (BLangFieldBindingPattern fieldBindingPattern : fieldBindingPatterns) {
keysToRemove.add(fieldBindingPattern.fieldName.value);
}
return keysToRemove;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,9 @@ public void visit(BLangTestablePackage node, AnalyzerData data) {

private void analyzeTopLevelNodes(BLangPackage pkgNode, AnalyzerData data) {
List<TopLevelNode> topLevelNodes = pkgNode.topLevelNodes;

// topLevelNodes are modified while iterating over them
// noinspection ForLoopReplaceableByForEach
for (int i = 0; i < topLevelNodes.size(); i++) {
analyzeNode((BLangNode) topLevelNodes.get(i), data);
}
Expand Down Expand Up @@ -3684,8 +3687,8 @@ private void checkAccessSymbol(BSymbol symbol, PackageID pkgID, Location positio
}

private <E extends BLangExpression> void analyzeExprs(List<E> nodeList, AnalyzerData data) {
for (int i = 0; i < nodeList.size(); i++) {
analyzeExpr(nodeList.get(i), data);
for (E e : nodeList) {
analyzeExpr(e, data);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,9 @@ public void visit(BLangPackage pkgNode, AnalyzerData data) {

// Visit constants first.
List<TopLevelNode> topLevelNodes = pkgNode.topLevelNodes;

// topLevelNodes are modified while iterating over them
// noinspection ForLoopReplaceableByForEach
for (int i = 0; i < topLevelNodes.size(); i++) {
TopLevelNode constant = topLevelNodes.get(i);
if (constant.getKind() == NodeKind.CONSTANT) {
Expand All @@ -331,9 +334,9 @@ public void visit(BLangPackage pkgNode, AnalyzerData data) {
validateEnumMemberMetadata(pkgNode.constants);

// Then resolve user defined types without analyzing type definitions that get added while analyzing other nodes
for (int i = 0; i < copyOfOriginalTopLevelNodes.size(); i++) {
if (copyOfOriginalTopLevelNodes.get(i).getKind() == NodeKind.TYPE_DEFINITION) {
analyzeNode((BLangNode) copyOfOriginalTopLevelNodes.get(i), data);
for (TopLevelNode copyOfOriginalTopLevelNode : copyOfOriginalTopLevelNodes) {
if (copyOfOriginalTopLevelNode.getKind() == NodeKind.TYPE_DEFINITION) {
analyzeNode((BLangNode) copyOfOriginalTopLevelNode, data);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2401,8 +2401,7 @@ public void visit(BLangTupleVariable varNode) {
if (varNode.restVariable != null) {
memberVariables.add(varNode.restVariable);
}
for (int i = 0; i < memberVariables.size(); i++) {
BLangVariable memberVar = memberVariables.get(i);
for (BLangVariable memberVar : memberVariables) {
memberVar.isDeclaredWithVar = true;
defineNode(memberVar, env);
}
Expand Down Expand Up @@ -4213,9 +4212,7 @@ private void defineUndefinedReadOnlyTypes(List<BLangTypeDefinition> typeDefNodes
}

private void populateImmutableTypeFieldsAndMembers(List<BLangTypeDefinition> typeDefNodes, SymbolEnv pkgEnv) {
int size = typeDefNodes.size();
for (int i = 0; i < size; i++) {
BLangTypeDefinition typeDef = typeDefNodes.get(i);
for (BLangTypeDefinition typeDef : typeDefNodes) {
NodeKind nodeKind = typeDef.typeNode.getKind();
if (nodeKind == NodeKind.OBJECT_TYPE) {
if (((BObjectType) typeDef.symbol.type).mutableType == null) {
Expand All @@ -4231,7 +4228,7 @@ private void populateImmutableTypeFieldsAndMembers(List<BLangTypeDefinition> typ

SymbolEnv typeDefEnv = SymbolEnv.createTypeEnv(typeDef.typeNode, typeDef.symbol.scope, pkgEnv);
ImmutableTypeCloner.defineUndefinedImmutableFields(typeDef, types, typeDefEnv, symTable,
anonymousModelHelper, names);
anonymousModelHelper, names);

if (nodeKind != NodeKind.OBJECT_TYPE) {
continue;
Expand All @@ -4241,14 +4238,12 @@ private void populateImmutableTypeFieldsAndMembers(List<BLangTypeDefinition> typ
BObjectType mutableObjectType = immutableObjectType.mutableType;

ImmutableTypeCloner.defineObjectFunctions((BObjectTypeSymbol) immutableObjectType.tsymbol,
(BObjectTypeSymbol) mutableObjectType.tsymbol, names, symTable);
(BObjectTypeSymbol) mutableObjectType.tsymbol, names, symTable);
}
}

private void validateFieldsAndSetReadOnlyType(List<BLangNode> typeDefNodes, SymbolEnv pkgEnv) {
int origSize = typeDefNodes.size();
for (int i = 0; i < origSize; i++) {
BLangNode typeDefOrClass = typeDefNodes.get(i);
for (BLangNode typeDefOrClass : typeDefNodes) {
if (typeDefOrClass.getKind() == NodeKind.CLASS_DEFN) {
BLangClassDefinition classDefinition = (BLangClassDefinition) typeDefOrClass;
if (isObjectCtor(classDefinition)) {
Expand Down Expand Up @@ -4882,8 +4877,7 @@ private BAttachedFunction createResourceFunction(BLangFunction funcNode, BInvoka
int resourcePathCount = pathSegments.size();
List<BResourcePathSegmentSymbol> pathSegmentSymbols = new ArrayList<>(resourcePathCount);
BResourcePathSegmentSymbol parentResource = null;
for (int i = 0; i < resourcePathCount; i++) {
BLangResourcePathSegment pathSegment = pathSegments.get(i);
for (BLangResourcePathSegment pathSegment : pathSegments) {
Name resourcePathSymbolName = Names.fromString(pathSegment.name.value);
BType resourcePathSegmentType = pathSegment.typeNode == null ?
symTable.noType : symResolver.resolveTypeNode(pathSegment.typeNode, env);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,31 +183,31 @@ public class XMLValidator {
}

// remove special characters
for (int i = 0; i < specialChar.length; i++) {
CHARS[specialChar[i]] = (byte) (CHARS[specialChar[i]] & ~MASK_CONTENT);
for (int k : specialChar) {
CHARS[k] = (byte) (CHARS[k] & ~MASK_CONTENT);
}

// set space characters
for (int i = 0; i < spaceChar.length; i++) {
CHARS[spaceChar[i]] |= MASK_SPACE;
for (int k : spaceChar) {
CHARS[k] |= MASK_SPACE;
}

// set name start characters
for (int i = 0; i < nameStartChar.length; i++) {
CHARS[nameStartChar[i]] |= MASK_NAME_START | MASK_NAME | MASK_NCNAME_START | MASK_NCNAME;
for (int k : nameStartChar) {
CHARS[k] |= MASK_NAME_START | MASK_NAME | MASK_NCNAME_START | MASK_NCNAME;
}
for (int i = 0; i < letterRange.length; i += 2) {
for (int j = letterRange[i]; j <= letterRange[i + 1]; j++) {
CHARS[j] |= MASK_NAME_START | MASK_NAME | MASK_NCNAME_START | MASK_NCNAME;
}
}
for (int i = 0; i < letterChar.length; i++) {
CHARS[letterChar[i]] |= MASK_NAME_START | MASK_NAME | MASK_NCNAME_START | MASK_NCNAME;
for (int k : letterChar) {
CHARS[k] |= MASK_NAME_START | MASK_NAME | MASK_NCNAME_START | MASK_NCNAME;
}

// set name characters
for (int i = 0; i < nameChar.length; i++) {
CHARS[nameChar[i]] |= MASK_NAME | MASK_NCNAME;
for (int k : nameChar) {
CHARS[k] |= MASK_NAME | MASK_NCNAME;
}
for (int i = 0; i < digitRange.length; i += 2) {
for (int j = digitRange[i]; j <= digitRange[i + 1]; j++) {
Expand All @@ -219,24 +219,24 @@ public class XMLValidator {
CHARS[j] |= MASK_NAME | MASK_NCNAME;
}
}
for (int i = 0; i < combiningCharChar.length; i++) {
CHARS[combiningCharChar[i]] |= MASK_NAME | MASK_NCNAME;
for (int k : combiningCharChar) {
CHARS[k] |= MASK_NAME | MASK_NCNAME;
}
for (int i = 0; i < extenderRange.length; i += 2) {
for (int j = extenderRange[i]; j <= extenderRange[i + 1]; j++) {
CHARS[j] |= MASK_NAME | MASK_NCNAME;
}
}
for (int i = 0; i < extenderChar.length; i++) {
CHARS[extenderChar[i]] |= MASK_NAME | MASK_NCNAME;
for (int k : extenderChar) {
CHARS[k] |= MASK_NAME | MASK_NCNAME;
}

// remove ':' from allowable MASK_NCNAME_START and MASK_NCNAME chars
CHARS[':'] &= ~(MASK_NCNAME_START | MASK_NCNAME);

// set Pubid characters
for (int i = 0; i < pubidChar.length; i++) {
CHARS[pubidChar[i]] |= MASK_PUBID;
for (int k : pubidChar) {
CHARS[k] |= MASK_PUBID;
}
for (int i = 0; i < pubidRange.length; i += 2) {
for (int j = pubidRange[i]; j <= pubidRange[i + 1]; j++) {
Expand Down
Loading

0 comments on commit a6a3526

Please sign in to comment.