Are there benefits using generics in GWT - gwt

Are there any benefits to the GWT compiler by using java generics in GWT. That is does it help in creating a smaller or more efficient javascript code or does it just have the same benefits as using them in Java.
The complication comes with using GWT, MVP with generics.. To implement, generics correctly the interfaces look as follows:
public interface ViewInterface<P extends PresenterInterface<? extends ViewInterface<P>>> {
}
public interface PresenterInterface<V extends ViewInterface<? extends PresenterInterface<V>>> {
}
Would code above improve the javascript compiler result or does it have no effect if I had just had the code as follows:
public interface ViewInterface<P extends PresenterInterface<?>> {
}
public interface PresenterInterface<V extends ViewInterface<?>> {
}
If there is no difference to the performance of the generated javascript the I would rather go with the second implementation. (Less Boilerplate)...
Hope this makes sense...

As Google I/O 2009 presentation Ray Ryan mentioned using the command pattern design when using GWT RPC. you can take a look at the presentation.there is a library called GAD a.k.a GWT Action Dispatcher where the idea is taken from the recommendation of Rayan in the presentation. GAD consists of 5 components (classes and interfaces) which uses generics. without generics it would be a lot of typecasting in the client code as well as in the server code where the Actions and Responses implementations are shared between the client and server.The 5 components i mentioned above are:
1-
public interface Action<T extends Response> extends Serializable {
}
2-
public interface ActionHandler<K extends Action, T extends Response> {
/**
* Handles the provided action and retuns response of it.
*
* #param action the action to be handled
* #return the response to be returned
*/
T handle(K action);
}
3-
public interface ActionDispatcher {
/**
* Dispatches the provided action to a proper handler that is responsible for handling of that action.
* <p/> To may dispatch the incomming action a proper handler needs to be bound
* to the {#link com.evo.gad.dispatch.ActionHandlerRepository} to may the dispatch method dispatch the
* incomming request to it.
*
* #param action the action to be handled
* #param <T> a generic response type
* #return response from the provided execution
* #throws ActionHandlerNotBoundException is thrown in cases where no handler has been bound to handle that action
*/
<T extends Response> T dispatch(Action<T> action) throws ActionHandlerNotBoundException;
}
4-
public interface ActionHandlerRepository {
ActionHandler getActionHandler(Class<? extends Action> aClass);
}
when an action is passed to the action dispatcher the action dispatcher calls the ActionHandlerRepository and asks it to get the right ActionHandler and then calls the method handle.
You can find GAD here.
In other words the benefits are quite the same. Less instanceof and typecasting.
hope this was helpful. Good Luck.

Related

GWTP REST-Dispatch - What is the new usage for rest dispach, since the removal of the RestService interface in 1.5 release

Hi everybody,
I encounter implementation issues with the rest-dispatch module of the gwtp framework.
If i follow the current documentation, the resource interface defining what a service provide should be as follow:
#Path(FOO)
#Produces(MediaType.APPLICATION_JSON)
public interface FooResource {
#GET
RestAction<FooDTO> getFoo();
}
On the client side (without delegate extension):
#Inject RestDispatch dispatcher;
#Inject FooResource fooResource;
...
dispatcher.execute(fooResource.getFoo(), new AsyncCallback<FooDTO>() {
#Override
public void onFailure(Throwable throwable) {
}
#Override
public void onSuccess(FooDTO fooDto) {
}
});
...
Question
The RestDispatch is waiting for method that return RestAction, but since the RestService interface has been remove from 1.5 release:
How can i implements the FooResource ?
Moreover
In the carstore sample project, the only resource that uses RestAction is:
https://github.com/ArcBees/GWTP-Samples/blob/master/carstore/src/main/java/com/gwtplatform/carstore/shared/api/StatisticsResource.java
But it's implementation, is in fact not an implementation in that case:
https://github.com/ArcBees/GWTP-Samples/blob/master/carstore/src/main/java/com/gwtplatform/carstore/server/api/StatisticsResourceImpl.java
Should i follow this example, and what is the purpose of an non-implemented Interface ?
I assume that my question is very specific and it is maybe principally directed to the authors of gwtp.
And i thank in advance those who will respond.
The rest-dispatch is a client-library, and the interface that describes the services are not to use on the server side.
I was attempting to do something not intended by the authors of GWTP.
Yet, the DelegateResource extension is a solution, if you want to use the interface on the server side too. It comes with a drawback: the anability to have type safe callback on the client side.
To go further, here the exchange i had with the team on github:
https://github.com/ArcBees/GWTP-Samples/issues/92

How can I add autocompletion for Flight PHP microframework in PHPStorm

I've started using Flight microframework, but all methods are hidden under the hood (not declared in the Flight class).
How can I configure PHPStorm or should I write new set of rules?
Update: use framework instance doesn't work
I've tried to use framework instance, but has no success — I have internal methods in the suggestion list:
Update: autocomplete implemented in the Flight framework
First of all: I'd suggest to submit new issue on their Issue Tracker asking to provide some sort of helper file (like below).. or implement it in any other way (e.g. via PHPDoc' #method for Flight class -- no helper needed and no changes in the actual code -- just PHPDoc) so that IDE (e.g. PhpStorm or Netbeans) would not complain for non-existing methods and you will have some code completion help from IDE.
Magic is good .. but not when whole interface is based on such magic.
On the actual question, which you can resolve yourself.
You will have to spend some time (half an hour or even less) and create some fake Flight class and put it anywhere in your IDE -- it will be used for code completion only. Yes, IDE may warn you about duplicate classes.. but that inspection can be turned off.
The idea is to create a class and declare all required methods as they should have been done if it would be an ordinary class. To start with (will resolve issues for first code example on their readme):
<?php
class Flight
{
/**
* Routes a URL to a callback function.
*
* #param string $pattern URL pattern to match
* #param callback $callback Callback function
* #param boolean $pass_route Pass the matching route object to the callback
*/
public static function route($pattern, $callback, $pass_route = false) {}
/**
* Starts the framework.
*/
public static function start() {}
}
Here is how it looks now:
As you can see Flight is underwaved -- IDE says that there is more than one class with such name in this project. Just tell PhpStorm to not to report such cases:
For adding methods to the original class via #method PHPDoc tags:
/**
* Bla-bla -- class description
*
* #method static void route(string $pattern, callback $callback, bool $pass_route = false) Routes a URL to a callback function
* #method static void start() Starts the framework
*/
class Flight
{
...
}

How to write tests for tracking event flows in eventbus?

Long description:
In our gwt with mvp4g app we have pretty complicated flow of events in eventbus. One event like LOGIN produces multiple others as a reaction from presenters/handlers. Currently we have great difficulties with understanding how events interrelated i.e. which events must follow this particular one.
We have tests for presenters and views, but we are lacking tests which would clearly show/model event flows, preferably without usage of real views and services.
Short description:
New tests on eventBus(?) should be developed which should clearly describe and test event flows.
I have few rud ideas but they all sounds not satisfactory:
Write custom implementation(could be ugly) of mvp4g eventbus and:
use real presenters
use mock(?) views
mock services
verify all produced service calls
Why not cool: (a) In this case test would not verify produced events directly but only that ones which have services. (b)
EventBus implementation would look rather scarry - it must create each presenter with mocked services and views
Find a way to use some magical mvp4g mechanism to create eventBus in test and mock vies, services.
Why not cool : same as prev - only indirect verification through services is possible, and I cannot find how to create eventBus manually and solve all problems with GIN, inter GWT module dependencies and so. I guess there is no simple way to do it.
Is there any general solution for problem of tracking event tree in tests? Guess I'm not the first person to stare at complicated eventbus event flows.
Do you want to test the eventBus? Or do you want to track all event which are fired?
If you want to track your events, maybe some kind of EventMonitor could help you? A class that implements all necessary EventHandler and log every event that occurs.
Something like that? Just instance that class before your tests starts.
import java.util.logging.Logger;
import com.google.gwt.event.shared.GwtEvent;
import com.google.web.bindery.event.shared.EventBus;
public class EventMonitor implements AEventHandler, BEventHandler /* , ... */{
private static int event_count = 1;
private final Logger logger = Logger.getLogger(this.getClass().getName());
public EventMonitor(EventBus eventBus) {
eventBus.addHandler(AEvent.getType(), this);
eventBus.addHandler(BEvent.getType(), this);
// [...]
}
private void logEvent(GwtEvent<?> event) {
logger.info(event_count + " useful information");
event_count++;
}
#Override
public void onAEvent(AEvent event) {
logEvent(event);
}
#Override
public void onBEvent(BEvent event) {
logEvent(event);
}
}

GWT Requestfactory coarse grained wrapper object

Currently in my application we are using GWT RequestFactory. We have multiple EntityProxy. Couple of finder method returns List from service layer. Since we are using pagination in our application we are returning pre-configured number of EntityProxy in List. We requires total number of EntityProxy also for showing in pagination UI for which we are making separate request. We want to create some wrapper object which encapsulates List and totalRecord count in single class. So in single request we can get both List and record count. What is best to do this using requestfactory ? Note : I am beginner in GWT RequestFactory.
The answer of Umit is quite correct. I would only add a layer that abstracts the pagination handling. This comes useful when you have your BasicTables and BasicLists to address all data through the same interface PageProxy (eg. for pagination)
public interface PaginationInfo extends ValueProxy {
public int getTotalRecords();
//either have the manual page info
public int getPageNumber();
//or use the count API on GAE (returned by your db request as a web safe String)
public String getCount();
}
public interface PageProxy extends ValueProxy {
public PaginationInfo getPageInfo();
}
public interface MyEntityProxy extends EntityProxy {}
public interface MyEntityPageProxy extends PageProxy {
public List<MyEntityProxy> getEntities();
}
Well you can use something along this lines:
public interface MyEntityProxy extends EntityProxy {}
public interface MyEntityPageProxy extends ValueProxy {
public List<MyEntityProxy> getEntities();
public int getTotalRecords();
}
It would be better to use a generic PageProxy interface (i.e. MyEntityPageProxy<T extends EntityProxy>) however because of this bug it's not possible or at least only through a workaround.
So for each EntityProxy you want to have Paginationsupport you have to create a separate PageProxy interface.

GWT RequestFactory and multiple types

My GWT app has ten different kinds of entities. Right now I use plain old DTOs and transport them over GWT-RPC. This works well for cases like startup - I can pack them all into a single request.
I'm looking at switching to RequestFactory because there are many times throughout the lifetime of the app (30 minutes, on average) when I just have to update one type of entity, and the unifying/bandwidth-saving features of RequestFactory are appealing. BUT: I don't see a way to download all of my initialization data in a single request when the app loads. I don't want to have to make ten requests to fetch all of the init data for my ten entity types.
Is there a way to make a GeneralRequestContext, or something? I'd even be happy with a solution like:
public interface InitDataProxy extends EntityProxy
{
public UserProxy getInitUsers();
public OrganizationProxy getInitOrganizations();
...
}
public interface GeneralRequestContext extends RequestContext
{
Request<InitDataProxy> getInitData();
}
But this won't work because I don't want to have to actually back InitDataProxy with anything, I just want to use it to combine a bunch of different types of Proxies in a single request.
So: Is there a way to receive multiple, unrelated types of EntityProxy in a single request?
I would also be happy enough making a normal gwt-rpc request to go outside of RequestFactory for this data, but I don't want to have to implement duplicate DTOs to run next to RequestFactory's proxies, and write custom code to copy the DTOs into them!
The InitDataProxy could extend ValueProxy instead, which doesn't require that the object on the server have any kind of id or version semantics. The domain-side InitData type could be an interface, possibly implemented with an anonymous type.
interface InitData {
User getUser();
Organization getOrgatization();
}
class InitService {
static InitData makeInitData() {
return new InitData() { ..... };
}
}
#ProxyFor(InitData.class)
interface InitDataProxy extends ValueProxy {
UserProxy getUser();
OrganizationProxy getOrganization();
}
#Service(InitService.class)
interface Init extends RequestContext {
Request<InitDataProxy> makeInitData();
}