Skip to content

Commit

Permalink
Merge pull request #43028 from Shadow-Devil/final-fields
Browse files Browse the repository at this point in the history
[Refactoring] Mark fields as final if they are not reassigned
  • Loading branch information
gimantha authored Oct 2, 2024
2 parents 5a52d89 + 465d1da commit 1869673
Show file tree
Hide file tree
Showing 274 changed files with 967 additions and 980 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
*/
public class BbeRecord {

private String name;
private String url;
private String verifyBuild;
private String verifyOutput;
private String isLearnByExample;
private final String name;
private final String url;
private final String verifyBuild;
private final String verifyOutput;
private final String isLearnByExample;

public BbeRecord(String name, String url, String verifyBuild, String verifyOutput, String isLearnByExample) {
this.name = name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@
*/
public class BbeTitle {

private String title;
private String column;
private String category;
private BbeRecord[] samples;
private final String title;
private final String column;
private final String category;
private final BbeRecord[] samples;

public BbeTitle(String title, String column, String category, BbeRecord[] samples) {
this.title = title;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
*/
public class BLock {

private ArrayDeque<Strand> current;
private final ArrayDeque<Strand> current;

private ArrayDeque<Strand> waitingForLock;
private final ArrayDeque<Strand> waitingForLock;

public BLock() {
this.current = new ArrayDeque<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class BLockStore {
/**
* The map of locks inferred.
*/
private Map<String, BLock> globalLockMap;
private final Map<String, BLock> globalLockMap;

public BLockStore() {
globalLockMap = new ConcurrentHashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,23 +46,23 @@ public class JsonGenerator implements Closeable {

private static final int DEFAULT_DEPTH = 10;

private Writer writer;
private final Writer writer;

private boolean[] levelInit = new boolean[DEFAULT_DEPTH];

private int currentLevel;

private boolean fieldActive;

private static boolean[] escChars = new boolean[93];
private static final boolean[] ESC_CHARS = new boolean[93];

static {
escChars['"'] = true;
escChars['\\'] = true;
escChars['\b'] = true;
escChars['\n'] = true;
escChars['\r'] = true;
escChars['\t'] = true;
ESC_CHARS['"'] = true;
ESC_CHARS['\\'] = true;
ESC_CHARS['\b'] = true;
ESC_CHARS['\n'] = true;
ESC_CHARS['\r'] = true;
ESC_CHARS['\t'] = true;
}

public JsonGenerator(OutputStream out) {
Expand Down Expand Up @@ -159,7 +159,7 @@ private void writeStringValue(String value) throws IOException {
char[] chs = value.toCharArray();
for (int i = 0; i < count; i++) {
ch = chs[i];
if (ch < escChars.length && escChars[ch]) {
if (ch < ESC_CHARS.length && ESC_CHARS[ch]) {
escaped = true;
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@
*/
public class TableJsonDataSource implements JsonDataSource {

private BTable<?, ?> tableValue;
private JSONObjectGenerator objGen;
private final BTable<?, ?> tableValue;
private final JSONObjectGenerator objGen;

public TableJsonDataSource(BTable<?, ?> tableValue) {
this(tableValue, new DefaultJSONObjectGenerator());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ public class TableOmDataSource extends AbstractPushOMDataSource {
private static final String DEFAULT_ROOT_WRAPPER = "results";
private static final String DEFAULT_ROW_WRAPPER = "result";

private TableValueImpl<?, ?> table;
private String rootWrapper;
private String rowWrapper;
private final TableValueImpl<?, ?> table;
private final String rootWrapper;
private final String rowWrapper;

public TableOmDataSource(TableValueImpl<?, ?> table, String rootWrapper, String rowWrapper) {
this.table = table;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,17 @@
public class XmlTreeBuilder {

// XMLInputFactory2
private static final XMLInputFactory xmlInputFactory;
private static final XMLInputFactory XML_INPUT_FACTORY;

static {
xmlInputFactory = XMLInputFactory.newInstance();
xmlInputFactory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
XML_INPUT_FACTORY = XMLInputFactory.newInstance();
XML_INPUT_FACTORY.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
}

private XMLStreamReader xmlStreamReader;
private Map<String, String> namespaces; // xml ns declarations from Bal source [xmlns "http://ns.com" as ns]
private Deque<BXmlSequence> seqDeque;
private Deque<List<BXml>> siblingDeque;
private final Map<String, String> namespaces; // xml ns declarations from Bal source [xmlns "http://ns.com" as ns]
private final Deque<BXmlSequence> seqDeque;
private final Deque<List<BXml>> siblingDeque;

public XmlTreeBuilder(String str) {
this(new StringReader(str));
Expand All @@ -89,7 +89,7 @@ public XmlTreeBuilder(Reader stringReader) {
seqDeque.push(new XmlSequence(siblings));

try {
xmlStreamReader = xmlInputFactory.createXMLStreamReader(stringReader);
xmlStreamReader = XML_INPUT_FACTORY.createXMLStreamReader(stringReader);
} catch (XMLStreamException e) {
handleXMLStreamException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
*/
public class ConfigException extends RuntimeException {

private ErrorCodes errorCode;
private final ErrorCodes errorCode;

private Object[] args;
private final Object[] args;

public ConfigException(ErrorCodes errorCode, Object... args) {
this.errorCode = errorCode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ public class RuntimeDiagnostic extends Diagnostic {

private final DiagnosticInfo diagnosticInfo;

private Object[] args;
private final Object[] args;

private RuntimeDiagnosticLocation location;
private final RuntimeDiagnosticLocation location;

public RuntimeDiagnostic(DiagnosticInfo diagnosticInfo, String location, Object[] args) {
this.diagnosticInfo = diagnosticInfo;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
*/
public class RuntimeDiagnosticLocation implements Location {

private String location;
private final String location;

public RuntimeDiagnosticLocation(String location) {
this.location = location;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
*/
public class RuntimeDiagnosticLog {

private List<RuntimeDiagnostic> diagnosticList = new LinkedList<>();
private final List<RuntimeDiagnostic> diagnosticList = new LinkedList<>();

private int errorCount = 0;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
*/
public class CharReader {

private char[] charBuffer;
private final char[] charBuffer;
private int offset = 0;
private int charBufferLength;
private final int charBufferLength;

private int lexemeStartPos;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class TokenReader {
private static final int BUFFER_SIZE = 20;

private final TreeTraverser treeTraverser;
private TokenBuffer tokensAhead = new TokenBuffer(BUFFER_SIZE);
private final TokenBuffer tokensAhead = new TokenBuffer(BUFFER_SIZE);

public TokenReader(TreeTraverser treeTraverser) {
this.treeTraverser = treeTraverser;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@
*/
public class TreeTraverser {

private CharReader reader;
private final CharReader reader;
private ParserMode mode;
private ArrayDeque<ParserMode> modeStack = new ArrayDeque<>();
private final ArrayDeque<ParserMode> modeStack = new ArrayDeque<>();

public TreeTraverser(CharReader charReader) {
this.reader = charReader;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
public abstract class AsyncFunctionCallback implements Callback {

private FutureValue future;
private Strand strand;
private final Strand strand;

public AsyncFunctionCallback(Strand strand) {
this.strand = strand;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
*/
public class Scheduler {

private static final PrintStream err = System.err;
private static final PrintStream ERR = System.err;

/**
* Scheduler does not get killed if the immortal value is true. Specific to services.
Expand All @@ -66,13 +66,13 @@ public class Scheduler {
*/
private final BlockingQueue<ItemGroup> runnableList = new LinkedBlockingDeque<>();

private static final ThreadLocal<StrandHolder> strandHolder = ThreadLocal.withInitial(StrandHolder::new);
private static final ConcurrentHashMap<Integer, Strand> currentStrands = new ConcurrentHashMap<>();
private static final ThreadLocal<StrandHolder> STRAND_HOLDER = ThreadLocal.withInitial(StrandHolder::new);
private static final ConcurrentHashMap<Integer, Strand> CURRENT_STRANDS = new ConcurrentHashMap<>();
private final Strand previousStrand;

private final AtomicInteger totalStrands = new AtomicInteger();

private static String poolSizeConf = System.getenv(RuntimeConstants.BALLERINA_MAX_POOL_SIZE_ENV_VAR);
private static final String POOL_SIZE_CONF = System.getenv(RuntimeConstants.BALLERINA_MAX_POOL_SIZE_ENV_VAR);

/**
* This can be changed by setting the BALLERINA_MAX_POOL_SIZE system variable.
Expand All @@ -84,7 +84,7 @@ public class Scheduler {

private Semaphore mainBlockSem;
private final RuntimeRegistry runtimeRegistry;
private AtomicReference<ItemGroup> objectGroup = new AtomicReference<>();
private final AtomicReference<ItemGroup> objectGroup = new AtomicReference<>();
private static Strand daemonStrand = null;

public static void setDaemonStrand(Strand strand) {
Expand All @@ -103,13 +103,13 @@ public Scheduler(int numThreads, boolean immortal) {
this.numThreads = numThreads;
this.immortal = immortal;
this.runtimeRegistry = new RuntimeRegistry(this);
this.previousStrand = numThreads == 1 ? strandHolder.get().strand : null;
this.previousStrand = numThreads == 1 ? STRAND_HOLDER.get().strand : null;
ItemGroup group = new ItemGroup();
objectGroup.set(group);
}

public static Strand getStrand() {
Strand strand = strandHolder.get().strand;
Strand strand = STRAND_HOLDER.get().strand;
if (strand == null) {
throw new IllegalStateException("strand is not accessible from non-strand-worker threads");
}
Expand All @@ -118,11 +118,11 @@ public static Strand getStrand() {

public static Strand getStrandNoException() {
// issue #22871 is opened to fix this
return strandHolder.get().strand;
return STRAND_HOLDER.get().strand;
}

public static Map<Integer, Strand> getCurrentStrands() {
return new HashMap<>(currentStrands);
return new HashMap<>(CURRENT_STRANDS);
}

/**
Expand Down Expand Up @@ -320,7 +320,7 @@ private void run() {
item = group.get();

try {
strandHolder.get().strand = item.future.strand;
STRAND_HOLDER.get().strand = item.future.strand;
result = item.execute();
} catch (Throwable e) {
panic = createError(e);
Expand All @@ -335,7 +335,7 @@ private void run() {
RuntimeUtils.printCrashLog(panic);
}
} finally {
strandHolder.get().strand = previousStrand;
STRAND_HOLDER.get().strand = previousStrand;
}
postProcess(item, result, panic);
group.lock();
Expand Down Expand Up @@ -462,7 +462,7 @@ private void cleanUp(Strand justCompleted) {
justCompleted.frames = null;
justCompleted.waitingContexts = null;

currentStrands.remove(justCompleted.getId());
CURRENT_STRANDS.remove(justCompleted.getId());
//TODO: more cleanup , eg channels
}

Expand Down Expand Up @@ -511,7 +511,7 @@ public FutureValue createFuture(Strand parent, Callback callback, Map<String, Ob
Type constraint, String name, StrandMetadata metadata) {
Strand newStrand = new Strand(name, metadata, this, parent, properties, parent != null ?
parent.currentTrxContext : null);
currentStrands.put(newStrand.getId(), newStrand);
CURRENT_STRANDS.put(newStrand.getId(), newStrand);
return createFuture(parent, callback, constraint, newStrand);
}

Expand Down Expand Up @@ -544,12 +544,12 @@ public RuntimeRegistry getRuntimeRegistry() {

private static int getPoolSize() {
try {
if (poolSizeConf != null) {
poolSize = Integer.parseInt(poolSizeConf);
if (POOL_SIZE_CONF != null) {
poolSize = Integer.parseInt(POOL_SIZE_CONF);
}
} catch (Throwable t) {
// Log and continue with default
err.println("ballerina: error occurred in scheduler while reading system variable:" +
ERR.println("ballerina: error occurred in scheduler while reading system variable:" +
RuntimeConstants.BALLERINA_MAX_POOL_SIZE_ENV_VAR + ", " + t.getMessage());
}
return poolSize;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
* @since 0.995.0
*/
class SchedulerItem {
private Function<Object[], ?> function;
private Object[] params;
private final Function<Object[], ?> function;
private final Object[] params;
final FutureValue future;
boolean parked;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@
public class BFiniteType extends BType implements FiniteType {

public Set<Object> valueSpace;
private int typeFlags;
private String originalName;
private final int typeFlags;
private final String originalName;

public BFiniteType(String typeName) {
this(typeName, new LinkedHashSet<>(), 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
*/
public class BParameterizedType extends BType implements ParameterizedType {

private Type paramValueType;
private int paramIndex;
private final Type paramValueType;
private final int paramIndex;

public BParameterizedType(Type paramValueType, int paramIndex) {
super(null, null, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
*/
public class BStreamType extends BType implements StreamType {

private Type constraint;
private Type completionType;
private final Type constraint;
private final Type completionType;

/**
* Creates a {@link BStreamType} which represents the stream type.
Expand Down
Loading

0 comments on commit 1869673

Please sign in to comment.