There is source code which is not allowed to be edited and I need to add an aspect class that modifies it slightly.
Here is the class that I made:
package ext;
import java.awt.event.ActionEvent;
import javax.swing.*;
import battleship.*;
public privileged aspect AddStrategy {
JButton playButton1 = new JButton("Play");
after(BattleshipDialog dialog): this(dialog) && execution(JPanel BattleshipDialog.makeControlPane()){
dialog.playButton.setText("Practice");
JPanel buttons = (JPanel) dialog.playButton.getParent();
buttons.add(playButton1);
playButton1.setFocusPainted(false);
playButton1.addActionListener(this::playButton1Clicked);
}
BattleshipDialog bd;
public void playButton1Clicked(ActionEvent event) {
bd.startNewGame();
}
}
I was able to rename the original button "Play" to "Practice" successfully. Whenever I click the "Practice" button, it has the original functionality of restarting the game and works fine.
However, when I try to mimic that functionality with the newly created button "Play" the console shows a bunch of errors all saying blahblahblah(UnknownSource).
I think I'm getting this issue because I need to be augmenting the same "dialog" and I'm trying to use "dialog" and "bd." However, I tried various ways to make them use the same dialog and none of them worked.
tl;dr Trying to get 2 buttons to do the same thing, but only 1 button does it while the other button throws a bunch of "Unknown Source" errors to the console.
I'm not sure if it will help, but here's the relevant source code from the BattleshipDialog.java class:
public void playButtonClicked(ActionEvent event) {
if (isGameOver()) {
startNewGame();
} else {
if (JOptionPane.showConfirmDialog(BattleshipDialog.this,
"Play a new game?", "Battleship", JOptionPane.YES_NO_OPTION)
== JOptionPane.YES_OPTION) {
startNewGame();
}
}
}
/** Start a new game. This will terminate the current play and
* start a new play. */
private void startNewGame() {
msgBar.setText("Shots: 0");
board.reset();
placeShips();
repaint();
}
EDIT: Here are the error messages as per requested:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at ext.AddStrategy.playButton1Clicked(AddStrategy.aj:30)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)