Skip to content

Commit

Permalink
Added code comments, clean up some invalid code.
Browse files Browse the repository at this point in the history
  • Loading branch information
finesoft committed Jul 19, 2022
1 parent 55db267 commit 8a69069
Show file tree
Hide file tree
Showing 18 changed files with 209 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@

/**
* corant-context
* <p>
* A CDI component management class for hosting the creation and destruction of various bean
* instances using CDI-related scopes. These instances can be obtained by specifying a unique name
* key. These instances are also destroyed through the scope of CDI, and manual destruction is also
* supported.
*
* @author bingo 下午5:29:56
*
Expand Down Expand Up @@ -86,21 +91,53 @@ private void writeObject(ObjectOutputStream stream) throws IOException {
}
}

/**
* corant-context
* <p>
* Request scoped component manager
*
* @author bingo 上午10:50:08
*
*/
@RequestScoped
abstract class RsComponentManager<N, C> extends AbstractComponentManager<N, C> {
private static final long serialVersionUID = -2588026760995417834L;
}

/**
* corant-context
* <p>
* Session scoped component manager
*
* @author bingo 上午10:50:29
*
*/
@SessionScoped
abstract class SsComponentManager<N, C> extends AbstractComponentManager<N, C> {
private static final long serialVersionUID = 7462742316873226368L;
}

/**
* corant-context
* <p>
* Thread scoped component manager
*
* @author bingo 上午10:50:50
*
*/
@ThreadScoped
abstract class thComponentManager<N, C> extends AbstractComponentManager<N, C> {
abstract class ThComponentManager<N, C> extends AbstractComponentManager<N, C> {
private static final long serialVersionUID = -5758319290954516372L;
}

/**
* corant-context
* <p>
* Transaction scoped component manager
*
* @author bingo 上午10:51:21
*
*/
@TransactionScoped
abstract class TsComponentManager<N, C> extends AbstractComponentManager<N, C> {
private static final long serialVersionUID = -2804585149568989342L;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
/**
* corant-context
*
* <p>
* A simple CDI base invocation handler implementation, supports method interceptor.
*
* @author bingo 下午2:12:51
*
Expand Down Expand Up @@ -54,13 +56,13 @@ public int hashCode() {
}

@Override
public Object invoke(Object o, Method method, Object[] args) throws Throwable {
public Object invoke(Object target, Method method, Object[] args) throws Throwable {
List<InterceptorInvocation> interceptorInvocations = interceptorChains.get(method);
if (isNotEmpty(interceptorInvocations)) {
return new InvocationContextImpl(clazz, o, method, invokers.get(method), args,
return new InvocationContextImpl(clazz, target, method, invokers.get(method), args,
interceptorInvocations).proceed();
} else {
return new InvocationContextImpl(clazz, o, method, invokers.get(method), args,
return new InvocationContextImpl(clazz, target, method, invokers.get(method), args,
Collections.emptyList()).proceed();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
/**
* corant-context
*
* <p>
* A simple contextual method handler, use for constructs a contextual CDI invocation from a method
* object.
*
* @author bingo 下午2:26:15
*
*/
Expand All @@ -47,34 +51,34 @@ public class ContextualMethodHandler implements Serializable {
protected transient Method method; // ?? use java.lang.invoke.MethodHandle
protected MethodSignature methodSignature;

public ContextualMethodHandler(Class<?> beanClass, Method beanMethod, Annotation... qualifiers) {
public ContextualMethodHandler(Method method, Annotation... qualifiers) {
this(method.getDeclaringClass(), method, qualifiers);
}

protected ContextualMethodHandler(Class<?> beanClass, Method beanMethod,
Annotation... qualifiers) {
method = shouldNotNull(beanMethod);
methodSignature = MethodSignature.of(method);
clazz = defaultObject(beanClass, beanMethod::getDeclaringClass);
this.qualifiers = qualifiers;
}

public ContextualMethodHandler(Method method) {
this(null, method);
}

public static Set<ContextualMethodHandler> from(Class<?> clazz, Predicate<Method> methodPredicate,
Annotation... qualifiers) {
Set<ContextualMethodHandler> annotatedMethods = new LinkedHashSet<>();
Set<ContextualMethodHandler> handlers = new LinkedHashSet<>();
if (!clazz.isInterface() && !Modifier.isAbstract(clazz.getModifiers())) {
for (Method m : clazz.getMethods()) {
if (methodPredicate.test(m)) {
annotatedMethods.add(new ContextualMethodHandler(clazz, m, qualifiers));
handlers.add(new ContextualMethodHandler(clazz, m, qualifiers));
}
}
}
return annotatedMethods;
return handlers;
}

public static Set<ContextualMethodHandler> fromDeclared(Class<?> clazz,
Predicate<Method> methodPredicate, Annotation... qualifiers) {
Set<ContextualMethodHandler> annotatedMethods = new LinkedHashSet<>();
// FIXME the class qualifiers
Set<ContextualMethodHandler> handlers = new LinkedHashSet<>();
if (!clazz.isInterface() && !Modifier.isAbstract(clazz.getModifiers())) {
for (Method m : clazz.getDeclaredMethods()) {
if (methodPredicate.test(m)) {
Expand All @@ -85,11 +89,11 @@ public static Set<ContextualMethodHandler> fromDeclared(Class<?> clazz,
return null;
});
}
annotatedMethods.add(new ContextualMethodHandler(clazz, m, qualifiers));
handlers.add(new ContextualMethodHandler(clazz, m, qualifiers));
}
}
}
return annotatedMethods;
return handlers;
}

@Override
Expand Down Expand Up @@ -119,15 +123,13 @@ public boolean equals(Object obj) {
}

/**
*
* @return the clazz
*/
public Class<?> getClazz() {
return clazz;
}

/**
*
* @return the method
*/
public Method getMethod() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@
/**
* corant-context
*
* <p>
* An object representing an interceptor meta object and an interceptor instance, while providing an
* interceptor to call the entry method.
*
* <p>
* Note: Only supports {@code InterceptionType.AROUND_INVOKE}
*
* @author bingo 上午10:41:54
*
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
/**
* corant-context
*
* <p>
* A simple interceptor call context implementation that works with {@link InterceptorInvocation}.
*
* @author bingo 上午11:10:02
*
*/
Expand All @@ -47,12 +50,14 @@ public class InvocationContextImpl implements InvocationContext {
private final MethodInvoker methodInvoker;

/**
* @param targetClass
* @param target
* @param method
* @param methodInvoker
* @param args
* @param chain
* Build a simple invocation context
*
* @param targetClass the invocation target class
* @param target the invocation target object
* @param method the invocation method
* @param methodInvoker the method implementation
* @param args the method parameters
* @param chain the interceptor chain
*/
public InvocationContextImpl(final Class<?> targetClass, final Object target, final Method method,
final MethodInvoker methodInvoker, final Object[] args,
Expand All @@ -61,18 +66,17 @@ public InvocationContextImpl(final Class<?> targetClass, final Object target, fi
}

/**
* @param targetClass
* @param target
* @param method
* @param args
* @param chain
* Build a simple invocation context with interceptor chain position
*
* @param targetClass the invocation target class
* @param target the invocation target object
* @param method the invocation method
* @param methodInvoker the method implementation
* @param args the method parameters
* @param chain the interceptor chain
* @param position the current interceptor chain position
*/
public InvocationContextImpl(final Class<?> targetClass, final Object target, final Method method,
final Object[] args, final List<InterceptorInvocation> chain) {
this(targetClass, target, method, null, args, chain, 0);
}

private InvocationContextImpl(final Class<?> targetClass, final Object target,
protected InvocationContextImpl(final Class<?> targetClass, final Object target,
final Method method, final MethodInvoker methodInvoker, final Object[] args,
final List<InterceptorInvocation> chain, final int position) {
this.targetClass = targetClass;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
/**
* corant-context
*
* <p>
* A simple implementation interface, use for proxy invoking.
*
* @author bingo 上午10:38:27
*
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,15 @@ public class ProxyBuilder {
/**
* Build normal Interface-based dynamic proxy instance.
*
* @see MethodInvoker
*
* @param <T>
* @param interfaceType
* @param invokerHandler
* @return build
* @param <T> interface type
* @param interfaceType interface for the proxy class to implement
* @param invokerHandler the invocation handler creator, used to create concrete implementation
* from a method.
* @return a dynamic proxy instance for the specified interface that dispatches method invocations
* to the specified invocation handler.
*
* @see MethodInvoker
*/
@SuppressWarnings("unchecked")
public static <T> T build(final Class<?> interfaceType,
Expand All @@ -49,11 +52,15 @@ public static <T> T build(final Class<?> interfaceType,
/**
* Build contextual Interface-based dynamic proxy instance.
*
* @param <T>
* @param beanManager
* @param interfaceType
* @param invokerHandler
* @return buildContextual
* @param <T> interface type
* @param beanManager bean manager for contextual bean handling
* @param interfaceType interface for the proxy class to implement
* @param invokerHandler the invocation handler creator, used to create concrete implementation
* from a method.
* @return a dynamic proxy contextual instance for the specified interface that dispatches method
* invocations to the specified invocation handler.
*
* @see MethodInvoker
*/
@SuppressWarnings("unchecked")
public static <T> T buildContextual(final BeanManager beanManager, final Class<?> interfaceType,
Expand All @@ -63,25 +70,30 @@ public static <T> T buildContextual(final BeanManager beanManager, final Class<?
}

/**
* Build contextual bean method handler instance.
* Build contextual bean method handler instance from the declared methods of the given bean
* class.
*
* @param clazz bean class
* @param methodPredicate the method predicate use for method selecting
* @param appendQualifiers the append qualifiers for bean instance selecting
* @return a contextual method handlers
*
* @param clazz
* @param methodPredicate
* @param appendQualifiers
* @return buildDeclaredMethods
* @see Class#getDeclaredMethods()
*/
public static Set<ContextualMethodHandler> buildDeclaredMethods(final Class<?> clazz,
Predicate<Method> methodPredicate, Annotation... appendQualifiers) {
return ContextualMethodHandler.fromDeclared(clazz, methodPredicate, appendQualifiers);
}

/**
* Build contextual bean method handler instance.
* Build contextual bean method handler instance from the methods of the given bean class.
*
* @param clazz bean class
* @param methodPredicate the method predicate use for method selecting
* @param appendQualifiers the append qualifiers for bean instance selecting
* @return a contextual method handlers
*
* @param clazz
* @param methodPredicate
* @param appendQualifiers
* @return buildMethods
* @see Class#getMethods()
*/
public static Set<ContextualMethodHandler> buildMethods(final Class<?> clazz,
Predicate<Method> methodPredicate, Annotation... appendQualifiers) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

/**
* corant-context
* <p>
* A simple invocation handler implementation.
*
* @author bingo 下午3:20:16
*
Expand Down Expand Up @@ -79,18 +81,18 @@ public int hashCode() {
}

@Override
public Object invoke(Object o, Method method, Object[] args) throws Throwable {
public Object invoke(Object target, Method method, Object[] args) throws Throwable {
MethodInvoker methodInvoker = invokers.get(method);
if (methodInvoker == null) {
// The default method and java.lang.Object methods use for hq in Collection
if (method.isDefault()) {
return ProxyUtils.invokeDefaultMethod(o, method, args);
return ProxyUtils.invokeDefaultMethod(target, method, args);
} else if ("equals".equals(method.getName()) && method.getParameterTypes()[0] == Object.class
&& args != null && args.length == 1) {
if (args[0] == null) {
return false;
}
if (o == args[0]) {
if (target == args[0]) {
return true;
}
return ProxyUtils.isProxyOfSameInterfaces(args[0], clazz)
Expand All @@ -103,7 +105,7 @@ public Object invoke(Object o, Method method, Object[] args) throws Throwable {
throw new CorantRuntimeException("Can not find method %s.", method);
}
}
return methodInvoker.invoke(o, args);
return methodInvoker.invoke(target, args);

}

Expand Down
Loading

0 comments on commit 8a69069

Please sign in to comment.