Define object of a class derived from a string variable - class

Following is the use case:
Invoker class (with main method)
public class Invoker {
public static void main(String[] args) {
String class_file="Batch_Status";
}
}
Class to be invoked (with the same method name as that of class name, e.g. in this case it is Batch_Status)
import java.util.*;
public class Batch_Status {
public static void Batch_Status(String args) {
......
......Code Goes Here
......
}
}
Now the problem is that i am not able to define any object such as test in Invoker class by using the value of string class_file such as class_file test = new class_file();
Above is just a snippet, in my production code the values in the String variable will vary and for each value, a different class file (the name of the class file will be same as that of value of the String variable).
Please suggest.
Regards

This code demonstrates being able to retrieve a class instance given a string:
String classString = "com.rbs.testing.Test";
try {
Class c = Class.forName(classString);
Test test = (Test) c.newInstance();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
If you don't know what class to cast to yet, you can dump
c.newInstance();
into an Object class, then use if else clauses until you find out the class type contained in the object.
Object o = c.newInstance();
if (o instanceof Test) {
} else if(o instanceof Test2) {
I hope this helps. Sorry if I misunderstood your need.

Thanks Michael,
In fact, while doing some brainstorming, i also did the same thing and it worked as desired. Now i am able to call the method as well which is also derived from the same string variable. Following is the code which i tried:
import java.lang.reflect.*;
import java.util.logging.*;
public class Invoker {
public static void main(String[] args){
try {
String str ="Batch_Status";
Class t = Class.forName(str);
t.getMethods()[0].invoke(t,str);
} catch (ClassNotFoundException | IllegalAccessException | InvocationTargetException ex) {
Logger.getLogger(Invoker.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
And when checked your reply, it seems to be quite similar. Thanks, i really appreciate that.
Regards

Related

Springboot Generic JPA AttributeConverter

I have below sample code, i am trying to write generic JPA converter which could convert,
Collection of user defined objects to Json
vice versa
Below is sample code I was trying to achieve the result but looks like it's not correct.
Please take a look.
To be more clear i need like below
List To string
Json String to List
Please suggest
#Converter(autoApply = true)
public class SetJsonConverter<E extends Collections> implements AttributeConverter<E, Object> {
#Override
public Object convertToDatabaseColumn(E e) {
return null;
}
#Override
public E convertToEntityAttribute(Object o) {
ObjectMapper objectMapper=new ObjectMapper();
return null;
}
}
JPA will not automatically handle generic converters. Each collection type and element type will require subclassing. You will need to define the base converter the following way:
public class AbstractJsonConverter<T, C extends Collection<T>> implements AttributeConverter<C, String> {
private final ObjectMapper objectMapper;
private final TypeReference<C> collectionType;
public AbstractJsonConverter(ObjectMapper objectMapper, Class<T> elementType, TypeReference<C> collectionType) {
this.objectMapper = objectMapper;
this.collectionType = collectionType;
}
#Override
public String convertToDatabaseColumn(C collection) {
try {
return objectMapper.writeValueAsString(collection);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
#Override
public C convertToEntityAttribute(String jsonString) {
try {
return objectMapper.readValue(jsonString, collectionType);
} catch (IOException e) {
throw new RuntimeException();
}
}
}
You then define specific converters as:
#Converter(autoApply = true)
public class UserSetConverter extends AbstractJsonConverter<User, Set<User>> {
public UserSetConverter(ObjectMapper objectMapper) {
super(objectMapper, User.class, new TypeReference<Set<User>>() {});
}
}

How to catch ConstraintViolationExceptions in a REST Method

Before marking this as a duplicate: I read here and there that an ExceptionMapper will solve my problem, but for some reason it does not catch the ConstraintViolationException.
Update
The problem is solved: Using a separate, more specific ExceptionMapper works (one that implements ExceptionMapper< ConstraintViolationException >). But I don't fully understand why a more general exception mapper (one that implements ExceptionMapper< Exception >) does NOT catch my ConstraintViolationException.
Original question:
I am introducing bean validation to my REST Methods:
#PUT
public Response updateUser(#NotNull #Valid UserUpdateDTO userUpdateDTO) {
return ResponseUtil.ok(userService.updateUser(userUpdateDTO));
}
When a validation fails, I get a 400 response:
[PARAMETER]
[updateUser.arg0.initials]
[Initials must be between 3 and 5]
[AD]
I would like to catch the ConstraintViolationException before the response is sent because I have my own ResponseFactory.
Here is my ExceptionMapper (that works with my other exceptions!)
#Provider
public class ApiExceptionMapper implements ExceptionMapper<Exception> {
#Override
public Response toResponse(Exception e) {
Throwable cause = (e instanceof EJBException) && e.getCause() != null ? e.getCause() : e;
if (cause instanceof BadRequestException) {
logger.error("BadRequest", cause);
return ResponseUtil.badRequest(cause.getMessage());
} else if (cause instanceof ForbiddenException) {
logger.error("Forbidden", cause);
return ResponseUtil.forbidden(cause.getMessage());
} else if (cause instanceof ServerException) {
logger.error("ServerException", cause);
return ResponseUtil.serverError(cause.getMessage());
} else if (cause instanceof ConstraintViolationException) {
return ResponseUtil.badRequest("Validation failed");
}
// Default
logger.error("unexpected exception while processing request", cause);
return ResponseUtil.serverError(cause);
}
}
The ExceptionMapper is not even called when a validation problem occurs, and I get the default 400 error right away.
What am I doing wrong ? I suspect that it has something to do with the fact that the exception is not thrown within the method's body, but rather in its signature.
I am using Wildfly 11 RC and its default validation
Given a Rest Service such as:
#Stateless
#Path("/people")
public class PersonService {
#PersistenceContext(name = "people")
private EntityManager em;
#POST
#Path("/")
#Consumes(APPLICATION_JSON)
public Response create(#Valid Person person) throws DuplicateKeyException {
em.persist(person);
return Response.created(UriBuilder.fromResource(PersonService.class)
.path(PersonService.class, "getPerson")
.resolveTemplate("id", person.getId()).build())
.build();
}
}
then the following ExceptionMapper works just fine by itself:
#Provider
public class ConstraintViolationExceptionMapper implements ExceptionMapper<ConstraintViolationException>{
#Inject
private Logger logger;
private static class ConstraintViolationBean {
private final String propertyName;
private final String message;
private final String invalidValue;
private ConstraintViolationBean(ConstraintViolation constraintViolation) {
final StringBuilder propertyPath = new StringBuilder();
for (Path.Node node: constraintViolation.getPropertyPath()) {
if (propertyPath.length() > 0) {
propertyPath.append('.');
}
propertyPath.append(node.getName());
}
this.propertyName = propertyPath.toString();
this.message = constraintViolation.getMessage();
this.invalidValue = constraintViolation.getInvalidValue().toString();
}
public String getPropertyName() {
return propertyName;
}
public String getMessage() {
return message;
}
public String getInvalidValue() {
return invalidValue;
}
}
#Override
public Response toResponse(ConstraintViolationException exception) {
logger.log(Level.WARNING, "Constraint violation: {}", exception.getMessage());
List<ConstraintViolationBean> messages = new ArrayList<>();
for (ConstraintViolation cv : exception.getConstraintViolations()) {
messages.add(new ConstraintViolationBean(cv));
}
return Response.status(Response.Status.BAD_REQUEST)
.entity(messages)
.build();
}
}
This is real working (not production) code that I have been messing with for fun. There is also an ExceptionMapper for the DuplicateKeyException.
You can find the source on github at jaxrs-people, which is essentially an experiment.
One thing I have noticed is that EJBExceptions seem to be unwrapped before the ExceptionMapper is selected and invoked.
Update:
Now, if I add the following implementation of ExceptionMapper<Exception> to the deployment, then this one is invoked and the remaining exception mappers are ignored.
#Provider
public class GenericExceptionMapper implements ExceptionMapper<Exception> {
#Override
public Response toResponse(Exception exception) {
return Response.status(Response.Status.NOT_ACCEPTABLE)
.build();
}
}
Therefore it seems that because your ApiExceptionMapper is actually catching everything and your other ExceptionMappers will be ignored.
It looks like you need to either implement a separate ExceptionMapper for each of BadRequestException, ForbiddenException and ServerException, or some common ancestor class that is not Exception or RuntimeException.
I think that separate implementations would be better because code without if statements is easier to unit test.
What the Spec says:
§4.4 of "JAX-RS: Java™ API for RESTful Web Services (v2.0)" contains the statement:
When choosing an exception mapping provider to map an exception, an implementation MUST use the provider whose generic type is the nearest superclass of the exception.
This behaviour corresponds with what we have experienced here.

jpa-derby Boolean merge

am working with JPA(EclipseLink) and Derby. In my object there is a boolean field. Before a merge operation, the field is set to true. but after the merge, the field still holds the false value.
#Entity
#Access(AccessType.PROPERTY)
public class SleepMeasure extends AbstractEntity {
private static final long serialVersionUID = 1361849156336265486L;
...
private boolean WeatherDone;
public boolean isWeatherDone() { // I have already tried with the "getWeatherDone()"
return WeatherDone;
}
public void setWeatherDone(boolean weatherDone) {
WeatherDone = weatherDone;
}
...
}
It doesn't seem to matter whether, I use "getWeatherDone()" or "isWeatherDone()".
using code:
public class WeatherDataCollectorImpl{
...
private void saveMeasures(WeatherResponse mResponse, SleepMeasure sleep) throws Exception {
AppUser owner = sleep.getOwner();
...
sleep.setWeatherDone(Boolean.TRUE);
reposService.updateEntity(sleep,SleepMeasure.class);
}
...
}
And here is my repository class
public class RepositoryImpl{
...
public <T extends AbstractEntity> T updateEntity(T entity, Class<T> type) throws RepositoryException {
openEM();
EntityTransaction tr = em.getTransaction();
try {
tr.begin();
{
// entity.weatherdone has value true
entity = em.merge(entity);
// entity.weatherdone has value false
}
tr.commit();
} catch (Exception e) {
tr.rollback();
}
return entity;
}
...
}
JPA console Info: There is no error, nor warning and not even any info that the boolean column shall be updated.
--Merge clone with references com.sleepmonitor.persistence.entities.sleep.SleepMeasure#b9025d
...
--Register the existing object // other objects
...
--Register the existing object com.sleepmonitor.persistence.entities.sleep.SleepMeasure#1ba90cc
So how do I solve this small problem.
Note:
Derby defined this field as "SMALLINT".
thanks.
Oh God! I found my problem. Actually I realised, it was not only the boolean field, but the whole object could not be updated.
While trying to complete a bideirection referencing, I stupidly did this in a setter property instead of an addMethod() .
public void setSleepProperties(SleepProperties sleepProperties) {
this.sleepProperties = sleepProperties;
if (!(sleepProperties == null)) {
this.sleepProperties.setSleepMeasure(this);
}
}
Instead of:
public void addSleepProperties(SleepProperties sleepProperties) {
this.sleepProperties = sleepProperties;
if (!(sleepProperties == null)) {
this.sleepProperties.setSleepMeasure(this);
}
}
So I ended up with the referenced entity (sleepProperties.sleepMeasure) over-writing the updates on the owning entity just before a merge. That was very defficult to find, and I think have learned a big lesson from it. Thanks to all who tried to help me out.
The "addMethod()" solved my problem.

GWT.setUncaughtExceptionHandler()

Has anyone successfully used the above statement to catch the exception before it goes to the browser as an alert?.
I registered a custom Exception Handler in the first line of my application entry point. But it does not catch the exception as expected.
public void onModuleLoad(){
GWT.setUncaughtExceptionHandler(new MyExceptionHandler());
...
....
}
EDIT
Here are my two classes:
I expect my system.out will print the details of the exception
and exception will be swallowed and should not be sent to browser.
Or Am I wrong?
package mypackage;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
public class MyEntryPoint implements EntryPoint {
public void onModuleLoad() {
GWT.setUncaughtExceptionHandler(new ClientExceptionHandler());
startApplication();
}
private void startApplication() {
Integer.parseInt("I_AM_NOT_NUMBER");
}
}
package mypackage;
import com.google.gwt.core.client.GWT;
public class ClientExceptionHandler implements GWT.UncaughtExceptionHandler {
#Override
public void onUncaughtException(Throwable cause) {
System.out.println(cause.getMessage());
}
}
I believe what's happening here is that the current JS event cycle is using the DefaultUncaughtExceptionHandler because that was the handler set at the start of the cycle. You'll need to defer further initialization to the next event cycle, like this:
public void onModuleLoad() {
GWT.setUncaughtExceptionHandler(new ClientExceptionHandler());
Scheduler.get().scheduleDeferred(new ScheduledCommand() {
#Override
public void execute() {
startApplication();
Window.alert("You won't see this");
}
});
}
private void startApplication() {
Integer.parseInt("I_AM_NOT_A_NUMBER");
// or any exception that results from server call
}
Update: And here's the issue that describes why this works, and why it isn't planned to be fixed.
Setting up a default handler can be a tricky proposition some times. I can tell you exactly what is going on. If you get an exception in the onModuleLoad(), the handler will not be called. It is only after the load method is completed that it will ACTUALLY get put into place.
You should try the following:
public void onModuleLoad(){
GWT.setUncaughtExceptionHandler(new UncaughtExceptionHandler() {
onUncaughtException(Throwable t) {
// Do stuff here
}
});
}
and see if that helps.
Silly solution, but it works fine!
before anything add your EntryPoint in your app.gwt.xml
<entry-point class='myPackage.client.MyEntryPoint' />
then;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.inject.Inject;
import com.google.web.bindery.event.shared.EventBus;
public class MyEntryPoint implements EntryPoint {
private EventBus eventBus;
#Inject
public MyEntryPoint(final EventBus eventBus){
this.eventBus = eventBus;
}
#Override
public void onModuleLoad() {
CustomUncaughtExceptionHandler customUncaughtExceptionHandler = new CustomUncaughtExceptionHandler();
GWT.setUncaughtExceptionHandler(customUncaughtExceptionHandler);
try {
onModuleLoad2();
} catch (RuntimeException ex) {
eventBus.fireEvent(new BusyEvent(false));
customUncaughtExceptionHandler.onUncaughtException(ex);
}
}
private void onModuleLoad2() {
throw new RuntimeException("test");
}
}
and your CustomUncaughtExceptionHandler would be something like:
import com.google.gwt.core.client.GWT.UncaughtExceptionHandler;
import com.google.gwt.event.shared.UmbrellaException;
import com.google.gwt.user.client.Window;
public class CustomUncaughtExceptionHandler implements UncaughtExceptionHandler {
#Override
public void onUncaughtException(Throwable e) {
Throwable exceptionToDisplay = getExceptionToDisplay( e );
Window.alert( exceptionToDisplay.getCause() .getMessage()+" "+ exceptionToDisplay.getStackTrace());
}
private static Throwable getExceptionToDisplay( Throwable throwable ) {
Throwable result = throwable;
if (throwable instanceof UmbrellaException && ((UmbrellaException) throwable).getCauses().size() >= 1) {
result = ((UmbrellaException) throwable).getCauses().iterator().next();
}
return result;
}
}

GWT Void remote services fail for seemingly no reason

I'm working on a GWT project and have several void remote services that seem to execute just fine, but on the client side, end up firing the onFailure() method. No exceptions are thrown anywhere, and the expected behavior is observed on the backend. I have no idea what could be going wrong. Here is the relevant code:
Interfaces and implementation...
#RemoteServiceRelativePath("DeleteSearchService")
public interface DeleteSearchService extends RemoteService {
/**
* Utility class for simplifying access to the instance of async service.
*/
public static class Util {
private static DeleteSearchServiceAsync instance;
public static DeleteSearchServiceAsync getInstance(){
if (instance == null) {
instance = GWT.create(DeleteSearchService.class);
}
return instance;
}
}
public void delete(SearchBean search);
}
public interface DeleteSearchServiceAsync {
public void delete(SearchBean bean, AsyncCallback<Void> callback);
}
public class DeleteSearchServiceImpl extends RemoteServiceServlet implements DeleteSearchService {
private static final long serialVersionUID = 1L;
#Override
public void delete(SearchBean search) {
try {
Connection conn = SQLAccess.getConnection();
String sql = "DELETE FROM `searches` WHERE `id`=?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1, search.getSearchId());
ps.execute();
sql = "DELETE FROM `searchsourcemap` WHERE `search-id` = ?";
ps = conn.prepareStatement(sql);
ps.setInt(1, search.getSearchId());
ps.execute();
return;
} catch (Exception e) {
// TODO Log error
e.printStackTrace();
}
}
}
Calling code...
private class DeleteListener implements ClickListener {
public void onClick(Widget sender) {
DeleteSearchServiceAsync dss = DeleteSearchService.Util.getInstance();
SearchBean bean = buildBeanFromGUI();
dss.delete(bean, new AsyncCallback<Void>(){
//#Override
public void onFailure(Throwable caught) {
// TODO log
SearchNotDeleted snd = new SearchNotDeleted();
snd.show();
}
//#Override
public void onSuccess(Void result) {
SearchDeleted sd = new SearchDeleted();
sd.show();
searchDef.getParent().removeFromParent();
}
});
}
}
I know I'm a jerk for posting like 500 lines of code but I've been staring at this since yesterday and can't figure out where I'm going wrong. Maybe a 2nd set of eyes would help...
Thanks,
brian
LGTM I'm afraid.
Are you using the hosted mode or a full-fledged browser? You can try switching and see if it helps.
Also, it might help listening to that //TODO and perform a GWT.log when onFailure is invoked.