I am trying to create a class that implements the 'ModelResolver' interface
See http://maven.apache.org/ref/3.0.4/maven-model-builder/apidocs/org/apache/maven/model/resolution/ModelResolver.html
Here is the class
class MyResolver implements ModelResolver {
private final File artifactFile;
public MyResolver(File artifactFile) {
this.artifactFile = artifactFile;
}
#Override
public void addRepository(Repository arg0)
throws InvalidRepositoryException {
/** Nothing to do here as aether will take care of this. */
}
#Override
public void addRepository(Repository arg0, boolean arg1)
throws InvalidRepositoryException {
/** Nothing to do here as aether will take care of this. */
}
#Override
public ModelResolver newCopy() {
return this;
}
#Override
public ModelSource resolveModel(Parent parent)
throws UnresolvableModelException {
Artifact artifact;
try {
/** Use parent class getRemoteArtifact method */
artifact = getRemoteArtifact(parent.getGroupId(),
parent.getArtifactId(),
parent.getVersion(),
"pom");
} catch (ArtifactResolutionException e) {
throw new UnresolvableModelException(e.getMessage(),
parent.getGroupId(),
parent.getArtifactId(),
parent.getVersion());
}
return new FileModelSource(artifact.getFile());
}
#Override
public ModelSource resolveModel(String groupId,
String artifactId,
String version)
throws UnresolvableModelException {
return new FileModelSource(artifactFile);
}
}
This gives me the following error
The method addRepository must override the super class method.
I am on java 1.7 and using eclipse for my development. What am I missing?
I also verifies that the compiler compliance level is set to 1.7 in eclipse
Related
I am trying to integrate a Native UI component in react-native using the new architecture with fabric enabled
Here is my spec file
import type {HostComponent, ViewProps} from 'react-native';
import type {
DirectEventHandler,
BubblingEventHandler,
} from 'react-native/Libraries/Types/CodegenTypes';
import codegenNativeComponent from 'react-native/Libraries/Utilities/codegenNativeComponent';
type Event = Readonly<{
text?: string;
}>;
interface NativeProps extends ViewProps {
text: string;
onClickHandler?: DirectEventHandler<Event>; ////Event name should start with on
}
export default codegenNativeComponent<NativeProps>(
'MyButtonView',
) as HostComponent<NativeProps>;
Then on native side I created following files
public class MyButtonViewManager extends SimpleViewManager<MyButtonView> {
public static final String NAME = "MyButtonView";
ReactApplicationContext mCallerContext;
public MyButtonViewManager(ReactApplicationContext reactContext) {
mCallerContext = reactContext;
}
#NonNull
#Override
public String getName() {
return NAME;
}
#NonNull
#Override
protected MyButtonView createViewInstance(#NonNull ThemedReactContext reactContext) {
return new MyButtonView(reactContext);
}
#ReactProp(name = "text")
public void setQrCodeText(MyButtonView view, String text) {
view.setText(text);
}
#Nullable
#Override
public Map<String, Object> getExportedCustomDirectEventTypeConstants() {
return MapBuilder.of("topOnClickHandler",
MapBuilder.of("registrationName", "onClickHandler")
);
}
}
public class MyButtonView extends androidx.appcompat.widget.AppCompatButton {
public MyButtonView(Context context) {
super(context);
configureViews();
}
private void configureViews(){
setBackgroundColor(Color.YELLOW);
setOnClickListener(view -> {
ReactContext reactContext = (ReactContext)getContext();
EventDispatcher eventDispatcher = UIManagerHelper.getEventDispatcherForReactTag(
reactContext ,getId()
);
eventDispatcher.dispatchEvent(new MyButtonClickEvent(getId()));
});
}
}
public class MyButtonClickEvent extends Event<MyButtonClickEvent> {
public MyButtonClickEvent(int viewId) {
super(viewId);
}
#Override
public String getEventName() {
return "topOnClickHandler";
}
#Override
public void dispatch(RCTEventEmitter rctEventEmitter) {
super.dispatch(rctEventEmitter);
rctEventEmitter.receiveEvent(getViewTag(), getEventName(), Arguments.createMap());
}
#Nullable
#Override
protected WritableMap getEventData() {
WritableMap event = Arguments.createMap();
event.putString("message", "MyMessage");
return event;
}
}
What is the alternative to dispatch and RCTEventEmitter as both are deprecated? I was looking into RCTModernEventEmitter and it also extends the deprecated RCTEventEmitter
Also i have to change the event name from OnClickHandler to topOnClickHandler in Native Android side. It was throwing hermes error. Not sure why there should be top prefix, why can't it just be OnClickHandler.
From the react-native source code:
This [RCTModernEventEmitter] is a transitional replacement for RCTEventEmitter that works with Fabric and non-Fabric renderers. RCTEventEmitter works with Fabric as well, but there are negative perf implications and it should be avoided.
You can use this for now as a replacement that works with Fabric.
This [RCTModernEventEmitter] interface will also be deleted in the distant future and be replaced with a new interface that doesn't need the old receiveEvent method at all. But for the foreseeable future, this is the recommended interface to use for EventEmitters.
However, in the long run this will be removed.
There's ReactEventEmitter that might help based on your use-case.
I have to use an 3th party library to create a java driven plc. in this library you have to define the IO's etc and then you will auto generate some code to acces the IO. this will generate a lot of code which i want to split up to keep readable.
The BIhcs_Io class is where everything on the event driven plc is executed. Each time a value of a property changes (see example below of property) it will run the doExecute() method.
What i want to do is that when this method is called, instead i want to execute it on the child class.
any help would be greatly appreciated!
public class BIhcs_IO extends BComponent implements Runnable {
//Example of a created property for a PLC IO
public static final Property bool_1 = newProperty(Flags.EXECUTE_ON_CHANGE | Flags.OPERATOR | Flags.SUMMARY, ((BBoolean)((BValue)BBoolean.TYPE.getInstance())).getBoolean(), BFacets.tryMake(null));
public boolean getBool_1() { return getBoolean(bool_1); }
public void setBool_1(boolean v) { setBoolean(bool_1, v, null); }
public static final Action execute = newAction(Flags.ASYNC, null);
public void execute() { invoke(execute, null, null); }
#Override
public Type getType() { return TYPE; }
public static final Type TYPE = Sys.loadType(BIhcs_IO.class);
public BComponent getComponent() {
return this;
}
public void started() throws Exception {
}
//When the Property bool_1 changes it wil execute the doExecute() method below
public void changed(final Property prop, final Context cx) {
super.changed(prop, cx);
if (!this.isRunning()) {
return;
}
if (Flags.isExecuteOnChange((BComplex) this, (Slot) prop)) {
this.execute();
}
}
public void run() {
System.out.println("Source BProgram did not override run(). Exiting thread.");
}
//This is called when a property changes value. When this is called, instead of executing this i want to execute the code in BIhcsMain()
public void doExecute() throws Exception {
setDebug_1("called parent")
}
public void stopped() throws Exception {
}
}
Below is the child class that extends the parent class. Each time the doExecute() in the parent is called i want to override it and execute the one in the child class instead.
public class BIhcsMain extends BIhcs_IO {
#Override
public void doExecute(){
setDebug_2("called child");
}
}
I'm having an unexpected error while compiling this example code (in the fails() method). IntelliJ used to not report the error in the IDE, but it has since started to report it (some of the classes were in a library, which seemed to confuse it)
public class Main {
// The command
public interface ToMessageOperation<MODEL, MESSAGE> {
void run(MODEL object, MESSAGE message) throws Exception;
}
// A command
public class SelfLink<MODEL> implements ToMessageOperation<MODEL, LinkedMessage> {
#Override
public void run(MODEL object, LinkedMessage linkedMessage) throws Exception {
}
}
// A message type
public interface LinkedMessage {
void linkme();
}
// A message
public interface BootInfo extends LinkedMessage {
}
//The Executor
public interface GetRequest<MODEL, MESSAGE> {
GetRequest<MODEL, MESSAGE> runAll(ToMessageOperation<? super MODEL, ? super MESSAGE>... operations);
GetRequest<MODEL, MESSAGE> cleanUp();
MESSAGE now();
}
// The command factory
public SelfLink selfLink() {
return null;
}
public <MODEL, MESSAGE> GetRequest<MODEL,MESSAGE> get(Class<MESSAGE> message) {
return null;
}
public BootInfo works() {
return get(BootInfo.class).cleanUp().now();
}
public BootInfo alsoWorks() {
return get(BootInfo.class).runAll(new ToMessageOperation<Object, BootInfo>() {
#Override
public void run(Object object, BootInfo bootInfo) throws Exception {
}
}).now();
}
public BootInfo surprisedItWorks() {
return get(BootInfo.class).runAll(new ToMessageOperation<Object, LinkedMessage>() {
#Override
public void run(Object object, LinkedMessage message) throws Exception {
}
}).now();
}
public BootInfo fails() {
return get(BootInfo.class).runAll(new SelfLink()).now();
}
}
I'm a bit surprised that the error only happens when I add the runAll() method, as all methods return the same objects.
I'm really surprised that the method works with different types that are assignable from the failing case ( where the ToMessageOperation type is inherited instead of being explicit ). And even that shouldn't change the return type, right ?
Am I doing something wrong ?
I quickly fixed it by putting explicit generic parameters on SelfLink
public class SelfLink<MODEL,MESSAGE extends LinkedMessage> implements ToMessageOperation<MODEL, MESSAGE> {
#Override
public void run(MODEL object, MESSAGE linkedMessage) throws Exception {
linkedMessage.linkme();
}
}
public <MODEL,MESSAGE extends LinkedMessage> SelfLink<MODEL, MESSAGE> selfLink() {
return null;
}
Using the factory class for selflink, the code looks like this:
public BootInfo fails() {
return get(BootInfo.class).runAll(selfLink()).now();
}
I don't understand why this is necessary in java 8, but use is pretty much equivalent. If I get a better explanation I'll accept it as an answer.
I am working on application in Vaadin for my classes.
I have to draw some map on the screen so I'm using gwt-graphics lib.
I have also some servlet which is waiting for requests.
When some specific request will come view of the map should be changed.
It lead me to prepare custom event:
// class NewModulePositionHandler
package com.example.locator;
import com.google.gwt.event.shared.EventHandler;
public interface NewModulePositionHandler extends EventHandler {
void onNewModulePosition(NewModulePositionEvent event);
}
Below implementation of my custom event:
import com.google.gwt.event.shared.GwtEvent;
public class NewModulePositionEvent extends GwtEvent<NewModulePositionHandler> {
private static final Type<NewModulePositionHandler> TYPE = new Type<NewModulePositionHandler>();
private final String m_Color;
public NewModulePositionEvent(String color) {
m_Color = color;
}
public static Type<NewModulePositionHandler> getType() {
return TYPE;
}
public String getColor() {
return m_Color;
}
#Override
public com.google.gwt.event.shared.GwtEvent.Type<NewModulePositionHandler> getAssociatedType() {
// TODO Auto-generated method stub
return TYPE;
}
#Override
protected void dispatch(NewModulePositionHandler handler) {
handler.onNewModulePosition(this);
}
}
And it's time for implementation of my custom widget:
a) MyComp.java
import com.google.gwt.event.shared.GwtEvent;
public class NewModulePositionEvent extends GwtEvent<NewModulePositionHandler> {
private static final Type<NewModulePositionHandler> TYPE = new Type<NewModulePositionHandler>();
private final String m_Color;
public NewModulePositionEvent(String color) {
m_Color = color;
}
public static Type<NewModulePositionHandler> getType() {
return TYPE;
}
public String getColor() {
return m_Color;
}
#Override
public com.google.gwt.event.shared.GwtEvent.Type<NewModulePositionHandler> getAssociatedType() {
// TODO Auto-generated method stub
return TYPE;
}
#Override
protected void dispatch(NewModulePositionHandler handler) {
handler.onNewModulePosition(this);
}
}
b) MyCompClientRpc.java
import com.vaadin.shared.communication.ClientRpc;
public interface MyCompClientRpc extends ClientRpc {
// TODO example API
public void alert(String message);
public void changeColor(String color);
}
c) MyCompConnector.java
package com.example.locator.widgetset.client.mycomp;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Widget;
import com.vaadin.client.ui.AbstractComponentConnector;
import com.vaadin.shared.ui.Connect;
import com.example.locator.MyComp;
import com.example.locator.NewModulePositionEvent;
import com.example.locator.NewModulePositionHandler;
import com.example.locator.widgetset.client.mycomp.MyCompWidget;
import com.example.locator.widgetset.client.mycomp.MyCompServerRpc;
import com.vaadin.client.communication.RpcProxy;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.vaadin.shared.MouseEventDetails;
import com.vaadin.client.MouseEventDetailsBuilder;
import com.example.locator.widgetset.client.mycomp.MyCompClientRpc;
import com.example.locator.widgetset.client.mycomp.MyCompState;
import com.vaadin.client.communication.StateChangeEvent;
#Connect(MyComp.class)
public class MyCompConnector extends AbstractComponentConnector {
MyCompServerRpc rpc = RpcProxy
.create(MyCompServerRpc.class, this);
public MyCompConnector() {
registerRpc(MyCompClientRpc.class, new MyCompClientRpc() {
public void alert(String message) {
// TODO Do something useful
Window.alert(message);
}
public void changeColor(String color) {
getWidget().InitMap(color);
}
});
// TODO ServerRpc usage example, do something useful instead
getWidget().addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
final MouseEventDetails mouseDetails = MouseEventDetailsBuilder
.buildMouseEventDetails(event.getNativeEvent(),
getWidget().getElement());
rpc.clicked(mouseDetails);
}
});
getWidget().addNewModulePositionHandler(new NewModulePositionHandler() {
public void onNewModulePosition(NewModulePositionEvent event) {
// TODO Auto-generated method stub
rpc.newModulePosition(event.getColor());
}
});
}
#Override
protected Widget createWidget() {
return GWT.create(MyCompWidget.class);
}
#Override
public MyCompWidget getWidget() {
return (MyCompWidget) super.getWidget();
}
#Override
public MyCompState getState() {
return (MyCompState) super.getState();
}
#Override
public void onStateChanged(StateChangeEvent stateChangeEvent) {
super.onStateChanged(stateChangeEvent);
// TODO do something useful
final String color = getState().color;
getWidget().InitMap(color);
}
}
d) MyCompServerRpc.java
package com.example.locator.widgetset.client.mycomp;
import com.vaadin.shared.MouseEventDetails;
import com.vaadin.shared.communication.ServerRpc;
public interface MyCompServerRpc extends ServerRpc {
// TODO example API
public void clicked(MouseEventDetails mouseDetails);
public void newModulePosition(String color);
}
e) MyCompState.java
package com.example.locator.widgetset.client.mycomp;
public class MyCompState extends com.vaadin.shared.AbstractComponentState {
// TODO example state
public String color = "#000000";
}
And finally implementation of the widget:
f) MyCompWidget.java
package com.example.locator.widgetset.client.mycomp;
import org.vaadin.gwtgraphics.client.DrawingArea;
import org.vaadin.gwtgraphics.client.Line;
import org.vaadin.gwtgraphics.client.shape.Circle;
import org.vaadin.gwtgraphics.client.shape.Rectangle;
import com.example.locator.HasNewModulePositionHandlers;
import com.example.locator.NewModulePositionEvent;
import com.example.locator.NewModulePositionHandler;
import com.google.gwt.dev.util.collect.HashMap;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.touch.client.Point;
import com.example.locator.Module;
// TODO extend any GWT Widget
public class MyCompWidget extends DrawingArea implements HasNewModulePositionHandlers {
public static final String CLASSNAME = "mycomp";
public static double m_AreaWidth = 64.355;
public static double m_AreaHeight = 17.385;
public static int m_PictureWidth;
public static int m_PictureHeight;
public static double m_AreaToMapRatio;
public static double m_RouteWidth = 3.5;
public static double m_MainRouteCoordinateY = 8.0828;
public Circle circle;
//public HashMap<Integer, Module> ModuleMap = new HashMap<Integer, Module>();
public MyCompWidget(){
super(640, 320);
//ModuleMap.put(666, new Module(666, 30.0, 8.08, new Circle((int)TranslateCoordinate(30.0), (int)TranslateCoordinate(8.0), 7)));
//ModuleMap.put(15, new Module(15, 27.0, 8.08, new Circle((int)TranslateCoordinate(30.0), (int)TranslateCoordinate(8.0), 7)));
double xRatio = m_AreaWidth / 640;
double yRatio = m_AreaHeight / 320;
m_AreaToMapRatio = xRatio > yRatio ? xRatio : yRatio;
InitMap("#919491");
setStyleName(CLASSNAME);
}
public void InitMap(String color)
{
m_PictureWidth = (int)TranslateCoordinate(m_AreaWidth);
m_PictureHeight = (int)TranslateCoordinate(m_AreaHeight);
Rectangle rectangle = new Rectangle(0, 0, m_PictureWidth, m_PictureHeight);
rectangle.setFillColor(color);
add(rectangle);
Point point1Beg = new Point(0.0, 8.0828);
Point point1End = new Point(64.355, 8.0838);
Point point2Beg = new Point(20.2825, 8.0828);
Point point2End = new Point(20.2825, 17.385);
Point point3Beg = new Point(59.325, 0.0);
Point point3End = new Point(59.325, 8.0828);
point1Beg = TranslatePoint(point1Beg);
point1End = TranslatePoint(point1End);
point2Beg = TranslatePoint(point2Beg);
point2End = TranslatePoint(point2End);
point3Beg = TranslatePoint(point3Beg);
point3End = TranslatePoint(point3End);
Line line1 = new Line((int)point1Beg.getX(), (int)point1Beg.getY(), (int)point1End.getX(), (int)point1End.getY());
Line line2 = new Line((int)point2Beg.getX(), (int)point2Beg.getY(), (int)point2End.getX(), (int)point2End.getY());
Line line3 = new Line((int)point3Beg.getX(), (int)point3Beg.getY(), (int)point3End.getX(), (int)point3End.getY());
line1.setStrokeColor("#FFFFFF");
line2.setStrokeColor("#FFFFFF");
line3.setStrokeColor("#FFFFFF");
line1.setStrokeWidth((int)TranslateCoordinate(m_RouteWidth));
line2.setStrokeWidth((int)TranslateCoordinate(m_RouteWidth));
line3.setStrokeWidth((int)TranslateCoordinate(m_RouteWidth));
add(line1);
add(line2);
add(line3);
DrawWall(TranslateCoordinate(10.0));
DrawWall(TranslateCoordinate(20.0));
DrawWall(TranslateCoordinate(30.0));
DrawWall(TranslateCoordinate(40.0));
DrawWall(TranslateCoordinate(50.0));
DrawWall(TranslateCoordinate(60.0));
DrawDoor(3.0, 3.0);
DrawDoor(13.0, 3.0);
DrawDoor(23.0, 3.0);
DrawDoor(33.0, 3.0);
DrawDoor(43.0, 3.0);
DrawDoor(53.0, 3.0);
circle = new Circle((int)TranslateCoordinate(25.0), (int)TranslateCoordinate(8.0), 15);
add(circle);
}
public void DrawWall(double a_Place)
{
Line line = new Line((int)a_Place, 0, (int)a_Place, (int)TranslateCoordinate(m_AreaHeight));
line.setStrokeColor("#FFFFFF");
add(line);
}
public void DrawDoor(double a_Position, double a_Width)
{
double realDoorPositionY = m_MainRouteCoordinateY - (m_RouteWidth / 2);
int doorPositionYTop = (int)TranslateCoordinate(realDoorPositionY) - 1;
int doorPositionYBottom = (int)TranslateCoordinate(realDoorPositionY + m_RouteWidth) + 1;
Line line = new Line((int)TranslateCoordinate(a_Position), doorPositionYTop, (int)TranslateCoordinate(a_Position) + (int)TranslateCoordinate(a_Width), doorPositionYTop);
line.setStrokeColor("#000000");
line.setStrokeWidth(2);
add(line);
Line line2 = new Line((int)TranslateCoordinate(a_Position), doorPositionYBottom, (int)TranslateCoordinate(a_Position) + (int)TranslateCoordinate(a_Width), doorPositionYBottom);
line2.setStrokeColor("#000000");
line2.setStrokeWidth(2);
add(line2);
}
public Point TranslatePoint(Point a_Point)
{
Point translatedPoint = new Point(TranslateCoordinate(a_Point.getX()), TranslateCoordinate(a_Point.getY()));
return translatedPoint;
}
public double TranslateCoordinate(double a_Coordinate)
{
return (a_Coordinate) / (m_AreaToMapRatio);
}
public void Move(int id) {
//ModuleMap.get(id).GetCircle().setX(10 + circle.getX());
}
public HandlerRegistration addNewModulePositionHandler(
NewModulePositionHandler handler) {
return addHandler(handler, NewModulePositionEvent.TYPE);
}
private void someMethod() {
fireEvent(new NewModulePositionEvent("#000000"));
}
public void emulateEvent() {
someMethod();
}
}
g) HasNewModulesPositionHandlers.java
package com.example.locator;
import com.google.gwt.event.shared.HandlerRegistration;
public interface HasNewModulePositionHandlers {
// Attention! method returns HandlerRegistration, so that handler can be cancelled
public HandlerRegistration addNewModulePositionHandler(
NewModulePositionHandler handler);
}
If I compile the widgets set containing MyCompWidget and then run my application on glassfish I get the following message:
Widgetset 'com.example.locator.widgetset.LocatorWidgetset' does not contain implementation for com.example.locator.MyComp. Check its component connector's #Connect mapping, widgetsets GWT module description file and re-compile your widgetset. In case you have downloaded a vaadin add-on package, you might want to refer to add-on instructions.
If I cut
public void addNewModulePositionHandler(
NewModulePositionHandler handler) {
handlerManager.addHandler(NewModulePositionEvent.getType(), handler);
// TODO Auto-generated method stub
}
widget works properly (of course I have to comment out these lines from MyCompConnector as well):
getWidget().addNewModulePositionHandler(new NewModulePositionHandler() {
public void onNewModulePosition(NewModulePositionEvent event) {
// TODO Auto-generated method stub
rpc.newModulePosition(event.getColor());
}
});
Can anyone tell me where is the problem? It seems that compilation of the widget failes but I can't find any information about that.
Please, help me.
Thanks in advance.
Compilation problem is very clear: GWT compiler cannot fins source code for a specified java class. You add source files to GWT compiler scope in two steps:
*.java files must be accessible through classpath (eclipse gwt compiler automatically includes all source folders to classpath)
You need to tell GWT that it should consider your packages during compilation
Create .gwt.xml file in PARENT package (relative to your source files) with the following content (if your package is pkg1.pkg2.pkg3, then you should in package pkg1.pkg2 create file "Pkg3.gwt.xml", pkg = "pkg3", by convention pkg3 is typically named "client")
<module>
<source path="pkg3" />
</module>
!!!Be careful with letter cases, By convention those are the correct names.
Add to your widgetset.gwt.xml file "inherits" directive like that
<inherits name="pkg1.pkg2.Pkg3" />
!!! Pay attention to letter cases (P is capital in Pkg3, after file name Pkg3.gwt.xml), .gwt.xml is omitted here
Then, first of all, Vaadin uses GWT with a certain twist. I would recommend browsing through custom widgetset samples. You may need to understand how to propagate events to and from Vaadin-specific components. Below I explain how GWT event are supposed to be implemented and used. I'm not an expert in Vaadin, especially in advanced topics such as customizing widgetsets.
So, speaking of GWT.
Basically, you need to understand only two things to make (non-DOM, also knows as bitless) events work in GWT.
Code support
Event class. NewModulePositionEvent in your case.
Event Handler interface. NewModulePositionHandler in your case.
Feature interface (optional but advised). HasNewModulePositionHandlers in your case.
Usage pattern
Basically, the component that is supposed to fire event should create relevant Event object and pass it to the fireEvent method. All the logic to invoke necessary habdler is provided by EventBus (internally)
If you need to provide API to fire events externally (such as click() method for a Button), it should be done by providing special methods (do not expose internal stuff how exactly event is fired)
DOM events are special in details how they are created (by browser) and dispatched (they need to be explicitly enabled). Anyway, all the browser events are already implemented in GWT out of the box.
So, typical implementations for the above items: For the sake of clarity, I provide excerpts from our production code.
1) Event class
public class QuestionClickEvent extends GwtEvent<QuestionClickHandler> {
public QuestionClickEvent(SScript script, SQuestion question) {
super();
this.script = script;
this.question = question;
}
public static final Type<QuestionClickHandler> TYPE = new Type<QuestionClickHandler>();
// internal event state
private final SScript script;
private final SQuestion question;
#Override
public Type<QuestionClickHandler> getAssociatedType() {
return TYPE;
}
#Override
protected void dispatch(QuestionClickHandler handler) {
handler.onQuestionClicked(this);
}
// provide access to internal event state
public SScript getScript() {
return script;
}
public SQuestion getQuestion() {
return question;
}
}
2) Handler interface
public interface QuestionClickHandler extends EventHandler {
public void onQuestionClicked(QuestionClickEvent event);
}
3) Feature interface
public interface HasQuestionClickHandlers {
// Attention! method returns HandlerRegistration, so that handler can be cancelled
public HandlerRegistration addQuestionClickHandler(
QuestionClickHandler handler);
}
4) Your widget/component
public class SummaryPanel extends Widget implements HasQuestionClickHandlers {
// blah-blah-blah
// implement your handler registration method
#Override
public HandlerRegistration addQuestionClickHandler(
QuestionClickHandler handler) {
return addHandler(handler, QuestionClickEvent.TYPE);
}
// blah-blah-blah
private someMethod() {
// suddenly you realized that you need to to fire your event
fireEvent(new QuestionClickEvent(script, question));
}
// sample external API method
public void emulateEvent() {
someMethod();
}
}
And finally usage example:
SummaryPanel summary = new SummaryPanel();
summary.addQuestionClickHandler(new QuestionClickHandler() {
#Override
public void onQuestionClicked(QuestionClickEvent event) {
// your reaction goes here
}
});
I am trying to use Google ThreadWeaver to write a unit test for concurrent code. No matter what I do, I will get an IllegalArgumentException. I am still working with an example, but even that does not work. This is what I tried:
public class ExampleTest {
public static class ExampleMain implements MainRunnable<Example> {
private Example example;
#Override
public Class<Example> getClassUnderTest() {
return Example.class;
}
#Override
public String getMethodName() {
return null;
}
#Override
public Method getMethod() throws NoSuchMethodException {
return null;
}
#Override
public void initialize() throws Exception {
example = new Example();
}
#Override
public Example getMainObject() {
return example;
}
#Override
public void terminate() throws Exception {
}
#Override
public void run() throws Exception {
example.test("second");
}
}
public static class ExampleSecondary implements SecondaryRunnable<Example, ExampleMain> {
private ExampleMain exampleMain;
#Override
public void initialize(ExampleMain main) throws Exception {
exampleMain = main;
}
#Override
public void terminate() throws Exception {
}
#Override
public boolean canBlock() {
return false;
}
#Override
public void run() throws Exception {
exampleMain.getMainObject().test("main");
}
}
public static class Example {
private List<String> list = new ArrayList<String>();
public String test(String s) {
System.out.println("1" + s);
list.add(s);
System.out.println("2" + s);
return list.get(0);
}
}
#Test
public void testThreadWeaver() throws Exception {
ClassInstrumentation instrumentation = Instrumentation.getClassInstrumentation(Example.class);
Method tested = Example.class.getDeclaredMethod("test", String.class);
Method breakpoint = List.class.getDeclaredMethod("add", Object.class);
CodePosition codePosition = instrumentation.afterCall(tested, breakpoint);
InterleavedRunner.interleave(new ExampleMain(), new ExampleSecondary(), Arrays.asList(codePosition)).throwExceptionsIfAny();
}
}
The stack trace says:
java.lang.IllegalArgumentException: Class Example is not instrumented
at
com.google.testing.threadtester.CallLoggerFactory.getClassInstrumentation(CallLoggerFactory.java:108)
at
com.google.testing.threadtester.Instrumentation.getClassInstrumentation(Instrumentation.java:65)
at MyTest.testThreadWeaver(MyTest.java:92
I followed the instructions at the official Google code webpage, but it does not seem to work. Any ideas?
ThreadWeaver needs to instrument your classes in order to add breakpoints to your methods. Therefore, you cannot run the tests with JUnit directly but you must run your test from a specific test runner. For your case this would be ThreadedTestRunner. The actual test methods must then be annotated with #ThreadedTest instead of #Test. This should work:
#Test
public void startTest() throws Exception {
new ThreadedTestRunner().runTests(getClass(), Example.class);
}
#ThreadedTest
public void testThreadWeaver() throws Exception {
// here comes your test
}