Skip to content

Commit

Permalink
add @OverRide and some try-with-resources (#1449)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
rbri authored Mar 4, 2024
1 parent 6ec1085 commit 6b09359
Show file tree
Hide file tree
Showing 19 changed files with 98 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public void close() {
@State(Scope.Thread)
public static class AnnotatedClassState extends AbstractClassState {

@Override
@Setup(Level.Trial)
public void init()
throws IllegalAccessException, InvocationTargetException, InstantiationException {
Expand Down Expand Up @@ -122,6 +123,7 @@ public int getValue() {
@State(Scope.Thread)
public static class IdClassState extends AbstractClassState {

@Override
@Setup(Level.Trial)
public void init()
throws IllegalAccessException, InvocationTargetException, InstantiationException {
Expand Down Expand Up @@ -314,6 +316,7 @@ protected int findPrototypeId(String s) {
@State(Scope.Thread)
public static class DumbLambdaState extends AbstractClassState {

@Override
@Setup(Level.Trial)
public void init()
throws IllegalAccessException, InvocationTargetException, InstantiationException {
Expand Down
1 change: 1 addition & 0 deletions examples/DynamicScopes.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ static class PerThread implements Runnable {
this.x = x;
}

@Override
public void run() {
// We need a new Context for this thread.
Context cx = Context.enter();
Expand Down
1 change: 1 addition & 0 deletions src/org/mozilla/javascript/Hashtable.java
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ public void clear() {
map.clear();
}

@Override
public Iterator<Entry> iterator() {
return new Iter(first);
}
Expand Down
1 change: 1 addition & 0 deletions src/org/mozilla/javascript/NativeJavaList.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public boolean has(int index, Scriptable start) {
return super.has(index, start);
}

@Override
public void delete(int index) {
if (isWithValidIndex(index)) {
list.set(index, null);
Expand Down
34 changes: 18 additions & 16 deletions testsrc/org/mozilla/javascript/tests/scriptengine/BuiltinsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,24 @@ public void printStdout() throws ScriptException {

@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);
}
}
}
}

Expand Down
3 changes: 3 additions & 0 deletions toolsrc/org/mozilla/javascript/tools/ToolErrorReporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,18 +92,21 @@ private static String getExceptionMessage(RhinoException ex) {
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);
Expand Down
10 changes: 10 additions & 0 deletions toolsrc/org/mozilla/javascript/tools/debugger/Dim.java
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,7 @@ private DimIProxy(Dim dim, int type) {
// ContextAction

/** Performs the action given by {@link #type}. */
@Override
public Object run(Context cx) {
switch (type) {
case IPROXY_COMPILE_SCRIPT:
Expand Down Expand Up @@ -863,6 +864,7 @@ private void withContext() {
// ContextFactory.Listener

/** Called when a Context is created. */
@Override
public void contextCreated(Context cx) {
if (type != IPROXY_LISTEN) Kit.codeBug();
ContextData contextData = new ContextData();
Expand All @@ -873,13 +875,15 @@ public void contextCreated(Context cx) {
}

/** Called when a Context is destroyed. */
@Override
public void contextReleased(Context cx) {
if (type != IPROXY_LISTEN) Kit.codeBug();
}

// Debugger

/** Returns a StackFrame for the given function or script. */
@Override
public DebugFrame getFrame(Context cx, DebuggableScript fnOrScript) {
if (type != IPROXY_DEBUG) Kit.codeBug();

Expand All @@ -892,6 +896,7 @@ public DebugFrame getFrame(Context cx, DebuggableScript fnOrScript) {
}

/** Called when compilation is finished. */
@Override
public void handleCompilationDone(Context cx, DebuggableScript fnOrScript, String source) {
if (type != IPROXY_DEBUG) Kit.codeBug();

Expand Down Expand Up @@ -984,6 +989,7 @@ private StackFrame(Context cx, Dim dim, FunctionSource fsource) {
}

/** 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;
Expand All @@ -994,6 +1000,7 @@ public void onEnter(Context cx, Scriptable scope, Scriptable thisObj, Object[] a
}

/** Called when the current position has changed. */
@Override
public void onLineChange(Context cx, int lineno) {
this.lineNumber = lineno;

Expand All @@ -1013,11 +1020,13 @@ public void onLineChange(Context cx, int lineno) {
}

/** 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);
Expand All @@ -1026,6 +1035,7 @@ public void onExit(Context cx, boolean byThrow, Object resultOrException) {
}

/** Called when a 'debugger' statement is executed. */
@Override
public void onDebuggerStatement(Context cx) {
dim.handleBreakpointHit(this, cx);
}
Expand Down
2 changes: 2 additions & 0 deletions toolsrc/org/mozilla/javascript/tools/debugger/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ public static ScopeProvider newScopeProvider(Scriptable scope) {
// ContextAction

/** Exit action. */
@Override
public void run() {
if (type != EXIT_ACTION) Kit.codeBug();
System.exit(0);
Expand All @@ -315,6 +316,7 @@ public void run() {
// ScopeProvider

/** Returns the scope for script evaluations. */
@Override
public Scriptable getScope() {
if (type != SCOPE_PROVIDER) Kit.codeBug();
if (scope == null) Kit.codeBug();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ public void paint(Graphics g) {
}

/** TreeCellRenderer method. Overridden to update the visible row. */
@Override
public Component getTableCellRendererComponent(
JTable table,
Object value,
Expand All @@ -218,6 +219,7 @@ public Component getTableCellRendererComponent(

/** 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;
Expand Down Expand Up @@ -351,6 +353,7 @@ protected void updateSelectedPathsFromSelectedRows() {
* the list changse.
*/
class ListSelectionHandler implements ListSelectionListener {
@Override
public void valueChanged(ListSelectionEvent e) {
updateSelectedPathsFromSelectedRows();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,12 @@ public TreeTableModelAdapter(TreeTableModel treeTableModel, JTree tree) {
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();
}
Expand All @@ -79,18 +81,22 @@ public void treeCollapsed(TreeExpansionEvent event) {
// 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();
}
Expand All @@ -99,6 +105,7 @@ public void treeStructureChanged(TreeModelEvent e) {

// Wrappers, implementing TableModel interface.

@Override
public int getColumnCount() {
return treeTableModel.getColumnCount();
}
Expand All @@ -113,6 +120,7 @@ public Class<?> getColumnClass(int column) {
return treeTableModel.getColumnClass(column);
}

@Override
public int getRowCount() {
return tree.getRowCount();
}
Expand All @@ -122,6 +130,7 @@ protected Object nodeForRow(int row) {
return treePath.getLastPathComponent();
}

@Override
public Object getValueAt(int row, int column) {
return treeTableModel.getValueAt(nodeForRow(row), column);
}
Expand All @@ -143,6 +152,7 @@ public void setValueAt(Object value, int row, int column) {
protected void delayedFireTableDataChanged() {
SwingUtilities.invokeLater(
new Runnable() {
@Override
public void run() {
fireTableDataChanged();
}
Expand Down
5 changes: 1 addition & 4 deletions toolsrc/org/mozilla/javascript/tools/jsc/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -240,11 +240,8 @@ public void processSource(String[] filenames) {
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());
Expand Down
Loading

0 comments on commit 6b09359

Please sign in to comment.