add @Override and some try-with-resources (#1449)

* add @Override and some try-with-resource
s

* add another try-with-resource to fix missing close introduced in last commit

* more try-with-resources
This commit is contained in:
RBRi 2024-03-04 08:03:48 +01:00 коммит произвёл GitHub
Родитель 6ec1085689
Коммит 6b0935930c
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
19 изменённых файлов: 98 добавлений и 36 удалений

Просмотреть файл

@ -59,6 +59,7 @@ public class BuiltinBenchmark {
@State(Scope.Thread)
public static class AnnotatedClassState extends AbstractClassState {
@Override
@Setup(Level.Trial)
public void init()
throws IllegalAccessException, InvocationTargetException, InstantiationException {
@ -122,6 +123,7 @@ public class BuiltinBenchmark {
@State(Scope.Thread)
public static class IdClassState extends AbstractClassState {
@Override
@Setup(Level.Trial)
public void init()
throws IllegalAccessException, InvocationTargetException, InstantiationException {
@ -314,6 +316,7 @@ public class BuiltinBenchmark {
@State(Scope.Thread)
public static class DumbLambdaState extends AbstractClassState {
@Override
@Setup(Level.Trial)
public void init()
throws IllegalAccessException, InvocationTargetException, InstantiationException {

Просмотреть файл

@ -139,6 +139,7 @@ public class DynamicScopes {
this.x = x;
}
@Override
public void run() {
// We need a new Context for this thread.
Context cx = Context.enter();

Просмотреть файл

@ -273,6 +273,7 @@ public class Hashtable implements Serializable, Iterable<Hashtable.Entry> {
map.clear();
}
@Override
public Iterator<Entry> iterator() {
return new Iter(first);
}

Просмотреть файл

@ -77,6 +77,7 @@ public class NativeJavaList extends NativeJavaObject {
return super.has(index, start);
}
@Override
public void delete(int index) {
if (isWithValidIndex(index)) {
list.set(index, null);

Просмотреть файл

@ -37,22 +37,24 @@ public class BuiltinsTest {
@Test
public void printStdoutAndCheckItPrints() throws Exception {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
PrintStream original = System.out;
PrintStream modified = new PrintStream(bos, false);
System.setOut(modified);
// Now Get A SimpleContext
ScriptContext sc = new SimpleScriptContext();
try {
// this was a broken test
engine.eval("print('Hello, World!');", sc);
// this has been hard work https://github.com/mozilla/rhino/issues/1356
Assert.assertEquals("Hello, World!\n", bos.toString());
} finally {
// revert the sys out
System.setOut(original);
modified.close();
bos.close();
try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
PrintStream original = System.out;
try (PrintStream modified = new PrintStream(bos, false)) {
System.setOut(modified);
try {
// Now Get A SimpleContext
ScriptContext sc = new SimpleScriptContext();
// this was a broken test
engine.eval("print('Hello, World!');", sc);
// this has been hard work https://github.com/mozilla/rhino/issues/1356
Assert.assertEquals("Hello, World!\n", bos.toString());
} finally {
// revert the sys out
System.setOut(original);
}
}
}
}

Просмотреть файл

@ -92,18 +92,21 @@ public class ToolErrorReporter implements ErrorReporter {
return msg;
}
@Override
public void warning(
String message, String sourceName, int line, String lineSource, int lineOffset) {
if (!reportWarnings) return;
reportErrorMessage(message, sourceName, line, lineSource, lineOffset, true);
}
@Override
public void error(
String message, String sourceName, int line, String lineSource, int lineOffset) {
hasReportedErrorFlag = true;
reportErrorMessage(message, sourceName, line, lineSource, lineOffset, false);
}
@Override
public EvaluatorException runtimeError(
String message, String sourceName, int line, String lineSource, int lineOffset) {
return new EvaluatorException(message, sourceName, line, lineSource, lineOffset);

Просмотреть файл

@ -805,6 +805,7 @@ public class Dim {
// ContextAction
/** Performs the action given by {@link #type}. */
@Override
public Object run(Context cx) {
switch (type) {
case IPROXY_COMPILE_SCRIPT:
@ -863,6 +864,7 @@ public class Dim {
// ContextFactory.Listener
/** Called when a Context is created. */
@Override
public void contextCreated(Context cx) {
if (type != IPROXY_LISTEN) Kit.codeBug();
ContextData contextData = new ContextData();
@ -873,6 +875,7 @@ public class Dim {
}
/** Called when a Context is destroyed. */
@Override
public void contextReleased(Context cx) {
if (type != IPROXY_LISTEN) Kit.codeBug();
}
@ -880,6 +883,7 @@ public class Dim {
// Debugger
/** Returns a StackFrame for the given function or script. */
@Override
public DebugFrame getFrame(Context cx, DebuggableScript fnOrScript) {
if (type != IPROXY_DEBUG) Kit.codeBug();
@ -892,6 +896,7 @@ public class Dim {
}
/** Called when compilation is finished. */
@Override
public void handleCompilationDone(Context cx, DebuggableScript fnOrScript, String source) {
if (type != IPROXY_DEBUG) Kit.codeBug();
@ -984,6 +989,7 @@ public class Dim {
}
/** Called when the stack frame is entered. */
@Override
public void onEnter(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
contextData.pushFrame(this);
this.scope = scope;
@ -994,6 +1000,7 @@ public class Dim {
}
/** Called when the current position has changed. */
@Override
public void onLineChange(Context cx, int lineno) {
this.lineNumber = lineno;
@ -1013,11 +1020,13 @@ public class Dim {
}
/** Called when an exception has been thrown. */
@Override
public void onExceptionThrown(Context cx, Throwable exception) {
dim.handleExceptionThrown(cx, exception, this);
}
/** Called when the stack frame has been left. */
@Override
public void onExit(Context cx, boolean byThrow, Object resultOrException) {
if (dim.breakOnReturn && !byThrow) {
dim.handleBreakpointHit(this, cx);
@ -1026,6 +1035,7 @@ public class Dim {
}
/** Called when a 'debugger' statement is executed. */
@Override
public void onDebuggerStatement(Context cx) {
dim.handleBreakpointHit(this, cx);
}

Просмотреть файл

@ -307,6 +307,7 @@ public class Main {
// ContextAction
/** Exit action. */
@Override
public void run() {
if (type != EXIT_ACTION) Kit.codeBug();
System.exit(0);
@ -315,6 +316,7 @@ public class Main {
// ScopeProvider
/** Returns the scope for script evaluations. */
@Override
public Scriptable getScope() {
if (type != SCOPE_PROVIDER) Kit.codeBug();
if (scope == null) Kit.codeBug();

Просмотреть файл

@ -41,28 +41,35 @@ public class AbstractCellEditor implements CellEditor {
protected EventListenerList listenerList = new EventListenerList();
@Override
public Object getCellEditorValue() {
return null;
}
@Override
public boolean isCellEditable(EventObject e) {
return true;
}
@Override
public boolean shouldSelectCell(EventObject anEvent) {
return false;
}
@Override
public boolean stopCellEditing() {
return true;
}
@Override
public void cancelCellEditing() {}
@Override
public void addCellEditorListener(CellEditorListener l) {
listenerList.add(CellEditorListener.class, l);
}
@Override
public void removeCellEditorListener(CellEditorListener l) {
listenerList.remove(CellEditorListener.class, l);
}

Просмотреть файл

@ -201,6 +201,7 @@ public class JTreeTable extends JTable {
}
/** TreeCellRenderer method. Overridden to update the visible row. */
@Override
public Component getTableCellRendererComponent(
JTable table,
Object value,
@ -218,6 +219,7 @@ public class JTreeTable extends JTable {
/** TreeTableCellEditor implementation. Component returned is the JTree. */
public class TreeTableCellEditor extends AbstractCellEditor implements TableCellEditor {
@Override
public Component getTableCellEditorComponent(
JTable table, Object value, boolean isSelected, int r, int c) {
return tree;
@ -351,6 +353,7 @@ public class JTreeTable extends JTable {
* the list changse.
*/
class ListSelectionHandler implements ListSelectionListener {
@Override
public void valueChanged(ListSelectionEvent e) {
updateSelectedPathsFromSelectedRows();
}

Просмотреть файл

@ -64,10 +64,12 @@ public class TreeTableModelAdapter extends AbstractTableModel {
new TreeExpansionListener() {
// Don't use fireTableRowsInserted() here; the selection model
// would get updated twice.
@Override
public void treeExpanded(TreeExpansionEvent event) {
fireTableDataChanged();
}
@Override
public void treeCollapsed(TreeExpansionEvent event) {
fireTableDataChanged();
}
@ -79,18 +81,22 @@ public class TreeTableModelAdapter extends AbstractTableModel {
// the event before us.
treeTableModel.addTreeModelListener(
new TreeModelListener() {
@Override
public void treeNodesChanged(TreeModelEvent e) {
delayedFireTableDataChanged();
}
@Override
public void treeNodesInserted(TreeModelEvent e) {
delayedFireTableDataChanged();
}
@Override
public void treeNodesRemoved(TreeModelEvent e) {
delayedFireTableDataChanged();
}
@Override
public void treeStructureChanged(TreeModelEvent e) {
delayedFireTableDataChanged();
}
@ -99,6 +105,7 @@ public class TreeTableModelAdapter extends AbstractTableModel {
// Wrappers, implementing TableModel interface.
@Override
public int getColumnCount() {
return treeTableModel.getColumnCount();
}
@ -113,6 +120,7 @@ public class TreeTableModelAdapter extends AbstractTableModel {
return treeTableModel.getColumnClass(column);
}
@Override
public int getRowCount() {
return tree.getRowCount();
}
@ -122,6 +130,7 @@ public class TreeTableModelAdapter extends AbstractTableModel {
return treePath.getLastPathComponent();
}
@Override
public Object getValueAt(int row, int column) {
return treeTableModel.getValueAt(nodeForRow(row), column);
}
@ -143,6 +152,7 @@ public class TreeTableModelAdapter extends AbstractTableModel {
protected void delayedFireTableDataChanged() {
SwingUtilities.invokeLater(
new Runnable() {
@Override
public void run() {
fireTableDataChanged();
}

Просмотреть файл

@ -240,11 +240,8 @@ public class Main {
byte[] bytes = (byte[]) compiled[j + 1];
try {
File outfile = getOutputFile(targetTopDir, className);
FileOutputStream os = new FileOutputStream(outfile);
try {
try (FileOutputStream os = new FileOutputStream(outfile)) {
os.write(bytes);
} finally {
os.close();
}
} catch (IOException ioe) {
addFormatedError(ioe.toString());

Просмотреть файл

@ -30,6 +30,7 @@ class ConsoleWrite implements Runnable {
this.str = str;
}
@Override
public void run() {
textArea.write(str);
}
@ -149,6 +150,7 @@ public class ConsoleTextArea extends JTextArea implements KeyListener, DocumentL
console1.flush();
}
@Override
public void keyPressed(KeyEvent e) {
int code = e.getKeyCode();
if (code == KeyEvent.VK_BACK_SPACE || code == KeyEvent.VK_LEFT) {
@ -213,6 +215,7 @@ public class ConsoleTextArea extends JTextArea implements KeyListener, DocumentL
}
}
@Override
public void keyTyped(KeyEvent e) {
int keyChar = e.getKeyChar();
if (keyChar == 0x8 /* KeyEvent.VK_BACK_SPACE */) {
@ -224,6 +227,7 @@ public class ConsoleTextArea extends JTextArea implements KeyListener, DocumentL
}
}
@Override
public synchronized void keyReleased(KeyEvent e) {}
public synchronized void write(String str) {
@ -233,6 +237,7 @@ public class ConsoleTextArea extends JTextArea implements KeyListener, DocumentL
select(outputMark, outputMark);
}
@Override
public synchronized void insertUpdate(DocumentEvent e) {
int len = e.getLength();
int off = e.getOffset();
@ -241,6 +246,7 @@ public class ConsoleTextArea extends JTextArea implements KeyListener, DocumentL
}
}
@Override
public synchronized void removeUpdate(DocumentEvent e) {
int len = e.getLength();
int off = e.getOffset();
@ -260,6 +266,7 @@ public class ConsoleTextArea extends JTextArea implements KeyListener, DocumentL
select(outputMark, outputMark);
}
@Override
public synchronized void changedUpdate(DocumentEvent e) {}
public InputStream getIn() {

Просмотреть файл

@ -340,9 +340,9 @@ public class Global extends ImporterTopLevel {
String filename = Context.toString(args[1]);
FileOutputStream fos = new FileOutputStream(filename);
Scriptable scope = ScriptableObject.getTopLevelScope(thisObj);
ScriptableOutputStream out = new ScriptableOutputStream(fos, scope);
out.writeObject(obj);
out.close();
try (ScriptableOutputStream out = new ScriptableOutputStream(fos, scope)) {
out.writeObject(obj);
}
}
public static Object deserialize(Context cx, Scriptable thisObj, Object[] args, Function funObj)
@ -351,12 +351,13 @@ public class Global extends ImporterTopLevel {
throw Context.reportRuntimeError("Expected a filename to read the serialization from");
}
String filename = Context.toString(args[0]);
FileInputStream fis = new FileInputStream(filename);
Scriptable scope = ScriptableObject.getTopLevelScope(thisObj);
ObjectInputStream in = new ScriptableInputStream(fis, scope);
Object deserialized = in.readObject();
in.close();
return Context.toObject(deserialized, scope);
try (FileInputStream fis = new FileInputStream(filename)) {
Scriptable scope = ScriptableObject.getTopLevelScope(thisObj);
try (ObjectInputStream in = new ScriptableInputStream(fis, scope)) {
Object deserialized = in.readObject();
return Context.toObject(deserialized, scope);
}
}
}
public String[] getPrompts(Context cx) {
@ -1033,14 +1034,12 @@ public class Global extends ImporterTopLevel {
is = new FileInputStream(f);
}
Reader r;
if (charCoding == null) {
r = new InputStreamReader(is);
} else {
r = new InputStreamReader(is, charCoding);
try (Reader r =
new InputStreamReader(
is,
charCoding == null ? Charset.defaultCharset().name() : charCoding)) {
return readReader(r, chunkLength);
}
return readReader(r, chunkLength);
} finally {
if (is != null) is.close();
}
@ -1144,10 +1143,12 @@ class Runner implements Runnable, ContextAction<Object> {
s = script;
}
@Override
public void run() {
factory.call(this);
}
@Override
public Object run(Context cx) {
if (f != null) return f.call(cx, scope, scope, args);
else return s.exec(cx, scope);

Просмотреть файл

@ -144,6 +144,7 @@ public class JSConsole extends JFrame implements ActionListener {
Main.main(args);
}
@Override
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
String plaf_name = null;

Просмотреть файл

@ -39,10 +39,12 @@ public class JavaPolicySecurity extends SecurityProxy {
this.domain = domain;
}
@Override
public Class<?> defineClass(String name, byte[] data) {
return super.defineClass(name, data, 0, data.length, domain);
}
@Override
public void linkClass(Class<?> cl) {
resolveClass(cl);
}
@ -85,10 +87,12 @@ public class JavaPolicySecurity extends SecurityProxy {
@Override
public Enumeration<Permission> elements() {
return new Enumeration<Permission>() {
@Override
public boolean hasMoreElements() {
return false;
}
@Override
public Permission nextElement() {
return null;
}
@ -123,6 +127,7 @@ public class JavaPolicySecurity extends SecurityProxy {
final Context cx, final Scriptable scope, final String filename) {
AccessController.doPrivileged(
new PrivilegedAction<Object>() {
@Override
public Object run() {
URL url = getUrlObj(filename);
ProtectionDomain staticDomain = getUrlDomain(url);
@ -172,6 +177,7 @@ public class JavaPolicySecurity extends SecurityProxy {
final ProtectionDomain domain = (ProtectionDomain) securityDomain;
return AccessController.doPrivileged(
new PrivilegedAction<Loader>() {
@Override
public Loader run() {
return new Loader(parentLoader, domain);
}
@ -220,6 +226,7 @@ public class JavaPolicySecurity extends SecurityProxy {
PrivilegedAction<Object> action =
new PrivilegedAction<Object>() {
@Override
public Object run() {
return callable.call(cx, scope, thisObj, args);
}

Просмотреть файл

@ -744,6 +744,7 @@ class XMLList extends XMLObjectImpl implements Function {
return null;
}
@Override
public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
// This XMLList is being called as a Function.
// Let's find the real Function object.
@ -782,6 +783,7 @@ class XMLList extends XMLObjectImpl implements Function {
return ((Callable) func).call(cx, scope, thisObj, args);
}
@Override
public Scriptable construct(Context cx, Scriptable scope, Object[] args) {
throw ScriptRuntime.typeErrorById("msg.not.ctor", "XMLList");
}

Просмотреть файл

@ -312,6 +312,7 @@ class XmlNode implements Serializable {
static class XmlNodeUserDataHandler implements UserDataHandler, Serializable {
private static final long serialVersionUID = 4666895518900769588L;
@Override
public void handle(short operation, String key, Object data, Node src, Node dest) {}
}

Просмотреть файл

@ -133,14 +133,17 @@ class XmlProcessor implements Serializable {
throw ScriptRuntime.constructError("TypeError", e.getMessage(), e.getLineNumber() - 1);
}
@Override
public void error(SAXParseException e) {
throwError(e);
}
@Override
public void fatalError(SAXParseException e) {
throwError(e);
}
@Override
public void warning(SAXParseException e) {
Context.reportWarning(e.getMessage());
}