MentaContainer

Compare Revisions

Ignore whitespace Rev 143 → Rev 144

/trunk/src/main/java/org/mentacontainer/impl/GenericFactory.java
9,21 → 9,21
private final Object factory;
private final Method method;
private final Method getMethod;
private final Class<E> type;
public GenericFactory(Object factory, String methodName) {
public GenericFactory(Object factory, String getMethodName) {
this.factory = factory;
try {
this.method = FindMethod.getMethod(factory.getClass(), methodName, new Class[] { });
this.getMethod = FindMethod.getMethod(factory.getClass(), getMethodName, new Class[] { });
this.method.setAccessible(true);
this.getMethod.setAccessible(true);
this.type = (Class<E>) method.getReturnType();
this.type = (Class<E>) getMethod.getReturnType();
} catch(Exception e) {
36,11 → 36,11
try {
return (E) method.invoke(factory, (Object[]) null);
return (E) getMethod.invoke(factory, (Object[]) null);
} catch(Exception e) {
throw new RuntimeException("Cannot invoke method: " + method, e);
throw new RuntimeException("Cannot invoke method: " + getMethod, e);
}
}
/trunk/src/main/java/org/mentacontainer/impl/GenericPool.java
New file
0,0 → 1,43
package org.mentacontainer.impl;
 
import java.lang.reflect.Method;
 
import org.mentacontainer.Pool;
import org.mentacontainer.util.FindMethod;
 
public class GenericPool<E> extends GenericFactory<E> implements Pool<E> {
private final Object factory;
private final Method releaseMethod;
 
public GenericPool(Object factory, String getMethodName, String releaseMethodName) {
super(factory, getMethodName);
this.factory = factory;
try {
this.releaseMethod = FindMethod.getMethod(factory.getClass(), releaseMethodName, new Class[] { getType() });
this.releaseMethod.setAccessible(true);
} catch(Exception e) {
 
throw new RuntimeException(e);
}
}
 
@Override
public void releaseInstance(E releasedObj) {
try {
releaseMethod.invoke(factory, new Object[] { releasedObj });
} catch(Exception e) {
throw new RuntimeException("Cannot invoke method: " + releaseMethod, e);
}
}
}