I receive an Exception When reflecting java interface - java

I want to reflect an interface in one of my SDK and call it, but I received an exception. The following is my code. Please help me solve this problem
public class ReflectMain {
Class<?> obs = null;
InterProxy.ProxyCallback callback = new InterProxy.ProxyCallback() {
#Override
public void onInvoke() {
Log.d("totolog", "invokeSuccess");
}
};
public void reflectinterMain() {
try {
Class<?> reflectClass = Class.forName(ReflectClass.class.getName());
obs = Class.forName(ReflectClass.ReflectInter.class.getName());
Method method = reflectClass.getDeclaredMethod("addInter", obs);
Object listener = Proxy.newProxyInstance(obs.getClassLoader(), new Class[]{obs}, new InterProxy(callback));
method.setAccessible(true);
method.invoke("addInter", new Object[]{listener});
obs.newInstance();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (Exception e){
e.printStackTrace();
}
}
class InterProxy implements InvocationHandler {
public ProxyCallback callback = null;
interface ProxyCallback {
void onInvoke();
}
InterProxy(ProxyCallback callback) {
if (this.callback != null) {
this.callback = callback;
}
}
#Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Log.d("totolog", method.getName());
if ("onRefletc".equals(method.getName())) {
callback.onInvoke();
}
return proxy;
}
And Reflect Class
class ReflectClass {
public ReflectInter inter = null;
void addInter(ReflectInter reflectInter) {
inter = reflectInter;
invoInter();
}
interface ReflectInter {
public void onRefletc();
}
private void invoInter() {
inter.onRefletc();
}
Then I received such an exception. Please help me analyze the cause of this exception and how to solve it.
Caused by: java.lang.IllegalArgumentException: Expected receiver of type lab.mon.actlab.java.reflectInter.ReflectClass, but got java.lang.String
at java.lang.reflect.Method.invoke(Native Method)
at lab.mon.actlab.java.reflectInter.ReflectMain.reflectinterMain(ReflectMain.java:29)
at lab.mon.actlab.java.reflect.ReflectActivity.onResume(ReflectActivity.java:87)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1354)
at android.app.Activity.performResume(Activity.java:7079)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3620)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3685) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2898) 
at android.app.ActivityThread.-wrap11(Unknown Source:0) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) 
at android.os.Handler.dispatchMessage(Handler.java:105) 
at android.os.Looper.loop(Looper.java:164) 
at android.app.ActivityThread.main(ActivityThread.java:6541) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 
How can I fix it, I want to know why?

The call to method.invoke is not correct since it expects as the first parameter an instance of the object on which you want to invoke the method. You're passing the name of the method.

Related

Interface null pointer exception in Network Callback onAvailable method

Below is my code, i tried to implement interface to intercept respond from Connectivity Manager Network Callback, but Android Studio is throwing error at this interface internetListener.onInternetStatusUpdate(true) in onAvailable method.
I also implemented this interface at my main activity. I follow a few example too but failed. Is it that onAvailable method, my interface is not reachable?
public class CheckInternetAsyncTask extends ConnectivityManager.NetworkCallback
{
private static final String TAG = "CheckInternetAsyncTask";
private Context context;
public InternetListener internetListener;
private ConnectivityManager connectivityManager;
private Network network;
private NetworkCapabilities networkCapabilities;
public interface InternetListener{
void onInternetStatusUpdate(boolean hasInternet);
}
public CheckInternetAsyncTask(Context _context) {
this.context = _context;
this.connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
connectivityManager.registerDefaultNetworkCallback(this);
}
try {
new SendInternetRequest().execute().get(5, TimeUnit.SECONDS);
} catch (ExecutionException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}
}
public void TestInternetRequest(){
try {
new SendInternetRequest().execute().get(5, TimeUnit.SECONDS);
} catch (ExecutionException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}
}
#Override
public void onAvailable(Network network) {
super.onAvailable(network);
this.network = network;
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
connectivityManager.bindProcessToNetwork(network);
this.network = connectivityManager.getActiveNetwork();
}else{
ConnectivityManager.setProcessDefaultNetwork(network);
}
this.networkCapabilities = connectivityManager.getNetworkCapabilities(network);
if(networkCapabilities != null && networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) && networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED)){
internetListener.onInternetStatusUpdate(true);
}else{
try {
new SendInternetRequest().execute().get(5, TimeUnit.SECONDS);
} catch (ExecutionException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}
}
}
class SendInternetRequest extends AsyncTask<Void,Void, Void>{
private static final String TAG = "SendInternetRequest";
CheckInternetAsyncTask.InternetListener internetListener;
#Override
protected Void doInBackground(Void... voids) {
try {
Socket sock = new Socket();
sock.connect(new InetSocketAddress("8.8.8.8", 53), 1500);
sock.close();
internetListener.onInternetStatusUpdate(true);
} catch (IOException e) {
internetListener.onInternetStatusUpdate(false);
}
return null;
}
}
Stacktrace
2019-09-25 10:08:02.566 29555-29578/com.kioskactionandnotification E/AndroidRuntime: FATAL EXCEPTION: ConnectivityThread
Process: com.kioskactionandnotification, PID: 29555
java.lang.NullPointerException: Attempt to invoke interface method 'void com.kioskactionandnotification.Model.Helper.CheckInternetAsyncTask$InternetListener.onInternetStatusUpdate(boolean)' on a null object reference
at com.kioskactionandnotification.Model.Helper.CheckInternetAsyncTask.onAvailable(CheckInternetAsyncTask.java:85)
at android.net.ConnectivityManager$NetworkCallback.onAvailable(ConnectivityManager.java:2770)
at android.net.ConnectivityManager$CallbackHandler.handleMessage(ConnectivityManager.java:2969)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.os.HandlerThread.run(HandlerThread.java:65)
MainActivity.java
I implemented the interface
#Override
public void onInternetStatusUpdate(boolean hasInternet) {
this.hasInternet = hasInternet;
Log.d(TAG, "onInternetStatusUpdate: hasInternet : "+hasInternet);
}
That's all i have.
It seems like you're trying to access an uninitialized object of type internetListener.
You need to initialize it:
internetListener = new InternetListener();
Or check if it was already initialized:
if (internetListener != null) {
internetListener.onInternetStatusUpdate(true);
}
NOTE
Any abstract class you are using should be extended by a subclass that provides implementations for any of the abstract data/methods used from the abstract class.
Abstract class in Java
In my constructor below, i added a listener. Its basically my interface.
public CheckInternetAsyncTask(Context _context, InternetListener internetListener) {
this.context = _context;
this.internetListener = internetListener;
}
Then in my main activity where i am implementing this interfaces, I had to push "this" keyword, to my listener.
I am still studying the difference between context and "this" keyword and how they work on interfaces
new CheckInternetAsyncTask(context, this);
Reference: How to create our own Listener interface in android?

How to regroup catch finally into one method in java 8?

New to java 8, I would like to optimise my code bellow:
public Response create() {
try{
...
} catch (Exception e) {
codeA;
} finally {
codeB;
}
}
public Response update() {
try{
...
} catch (Exception e) {
codeA;
} finally {
codeB;
}
}
I have a lot of methods using this same way to catch exceptions and do the same finally, is that possible to replace the bellow common code by a method in java 8? So that I could optimise all my methods who use this common code.
} catch (Exception e) {
codeA;
} finally {
codeB;
}
Depends what you do in the .... You could do something like this:
private Response method(Supplier<Response> supplier) {
try{
return supplier.get();
} catch (Exception e) {
codeA;
} finally {
codeB;
}
}
and invoke like:
public Response create() { return method(() -> { ... for create }); }
public Response update() { return method(() -> { ... for update }); }
You could wrap your payload and put it to the separate method. One thing; what do you expect to return on exception catch. This time this is null, but probably you could provide default value.
public static <T> T execute(Supplier<T> payload) {
try {
return payload.get();
} catch(Exception e) {
// code A
return null;
} finally {
// code B
}
}
Client code could look like this:
public Response create() {
return execute(() -> new CreateResponse());
}
public Response update() {
return execute(() -> new UpdateResponse());
}
This could be a generic solution.
//here describe supplier which can throw exceptions
#FunctionalInterface
public interface ThrowingSupplier<T> {
T get() throws Exception;
}
// The wrapper
private <T> T callMethod(ThrowingSupplier<T> supplier) {
try {
return supplier.get();
} catch (Exception e) {
//code A
}finally {
// code B
}
}

How to disable Caching at runtime if Couchbase connection failed?

I have a similar problem as asked here - How to disable Redis Caching at run time if redis connection failed. My application is using #Cacheable at the service layer for most of the database/static resources call.
Cache is backed by Couchbase and whenever application fails to connect Couchbase node application goes down. Which is what we are not expecting, we expect data should be served from the source system whenever connection failed.
We tried implementing CacheErrorHandler but it does not work as expected because we want to execute the actual method which is making a service call and return the response rather than logging the Cache fail, basically bypassing the cache and as soon as the Couchbase node is up or connection established get the data from cache.
Any idea how we can achieve it?
Thanks #Daniel Bickler for the suggestion, below is the implementation I written referring #John Blum answer.
CouchbaseCustomCacheManager:
import java.util.Map;
import org.springframework.cache.Cache;
import com.couchbase.client.spring.cache.CacheBuilder;
import com.couchbase.client.spring.cache.CouchbaseCacheManager;
public class CouchbaseCustomCacheManager extends CouchbaseCacheManager {
public CouchbaseCustomCacheManager(
final Map<String, CacheBuilder> initialCaches) {
super(initialCaches);
}
#Override
public Cache getCache(String name) {
return new CouchbaseCacheWrapper(super.getCache(name));
}
protected static class CouchbaseCacheWrapper implements Cache {
private final Cache delegate;
public CouchbaseCacheWrapper(Cache couchbaseCache) {
this.delegate = couchbaseCache;
}
#Override
public String getName() {
try {
return delegate.getName();
} catch (Exception e) {
return null;
}
}
#Override
public Object getNativeCache() {
try {
return delegate.getNativeCache();
} catch (Exception e) {
return null;
}
}
#Override
public ValueWrapper get(Object key) {
try {
return delegate.get(key);
} catch (Exception e) {
return null;
}
}
#Override
public <T> T get(Object key, Class<T> type) {
try {
return delegate.get(key, type);
} catch (Exception e) {
return null;
}
}
#Override
public void put(Object key, Object value) {
try {
delegate.put(key, value);
} catch (Exception e) {
try {
handleErrors(e);
} catch (Exception e1) {
}
}
}
#Override
public ValueWrapper putIfAbsent(Object key, Object value) {
try {
return delegate.putIfAbsent(key, value);
} catch (Exception e) {
return null;
}
}
#Override
public void evict(Object key) {
try {
delegate.evict(key);
} catch (Exception e) {
try {
handleErrors(e);
} catch (Exception e1) {
}
}
}
#Override
public void clear() {
try {
delegate.clear();
} catch (Exception e) {
try {
handleErrors(e);
} catch (Exception e1) {
}
}
}
protected <T> T handleErrors(Exception e) throws Exception {
if (e instanceof Exception) {
return null;
} else {
throw e;
}
}
}
}
And used it as:
#Bean
public CacheManager cacheManager() {
final Map<String, CacheBuilder> cache = new HashMap<>();
for (final String appCache : "127.0.0.1,127.0.0.2,127.0.0.3".split(",")) {
cache.put(appCache, CacheBuilder.newInstance(CouchbaseCluster.create().openBucket(
"default", "")));
}
return new CouchbaseCustomCacheManager(cache);
}

Common Argument Pass in Method

I have a method called makePersistent in my DAO class.
Currntly we have this method in all dao classes and what i need to do is convert this method to common format. So is there any way to do it?
Method in UserDao Class
public void makePersistent(User model) throws InfrastructureException {
try {
getSession().saveOrUpdate(model);
getSession().flush();
getSession().clear();
} catch (org.hibernate.StaleObjectStateException ex) {
throw new InfrastructureException(Labels.getString("com.tran.msg.objectDeletedOrUpdated"));
} catch (HibernateException ex) {
throw new InfrastructureException(ex);
}
}
Method in HolidayDao Class
public void makePersistent(Holiday model) throws InfrastructureException {
try {
getSession().saveOrUpdate(model);
getSession().flush();
getSession().clear();
} catch (org.hibernate.StaleObjectStateException ex) {
throw new InfrastructureException(Labels.getString("com.tran.msg.objectDeletedOrUpdated"));
} catch (HibernateException ex) {
throw new InfrastructureException(ex);
}
}
Please help me to get rid of this redundant coding.
Thank you.
Just use Object the hibernate will persist it.
public void makePersistent(Object model) throws InfrastructureException {
try {
getSession().saveOrUpdate(model);
getSession().flush();
getSession().clear();
} catch (org.hibernate.StaleObjectStateException ex) {
throw new InfrastructureException(Labels.getString("com.tran.msg.objectDeletedOrUpdaed"));
} catch (HibernateException ex) {
throw new InfrastructureException(ex);
}
}
Create a superclass for your DAOs with a type parameter and make your DAO classes extend that superclass with the appropriate type argument. For example:
public class BaseDao<T> {
public void makePersistent(T model) throws InfrastructureException {
try {
getSession().saveOrUpdate(model);
getSession().flush();
getSession().clear();
} catch (org.hibernate.StaleObjectStateException ex) {
throw new InfrastructureException(Labels.getString("com.tran.msg.objectDeletedOrUpdated"));
} catch (HibernateException ex) {
throw new InfrastructureException(ex);
}
}
}
public class UserDao extends BaseDao<User> {
// ...
}
public class HolidayDao extends BaseDao<Holiday> {
// ...
}
UserDao and HolidayDao inherit the makePersistent method from BaseDao, so you don't have to implement it again in every DAO class.

How to pass an Object with annotation?

I'm trying to use annotated class as parameters like below:
#Target(ElementType.TYPE)
#Retention(RetentionPolicy.RUNTIME)
public #interface ContextHolder {
}
#ContextHolder
class Foo extends Context {
}
// In some other place
protected Context getContext(ActionHandler handler) {
if (handler.getClass().isAssignableFrom(Context.class)) {
return (Context) handler;
}
for (Method m : handler.getClass().getDeclaredMethods()) {
if (m.getReturnType().isAssignableFrom(Context.class)) {
try {
return (Context) m.invoke(handler);
} catch (IllegalAccessException e) {
ALog.w("", e);
} catch (IllegalArgumentException e) {
ALog.w("", e);
} catch (InvocationTargetException e) {
ALog.w("", e);
}
break;
}
}
ALog.e("Can't find Context in passed ActionHandler");
return null;
}
Foo foo = ...;
getContext(foo?)
The problem is I don't know how to call getContext(). Simply passing foo results compile error.
Any hint will be appreciated. Thanks!

Categories

Resources