Author: dklco
Date: Fri Aug 30 04:22:04 2013
New Revision: 1518891
URL: http://svn.apache.org/r1518891
Log:
Fixing the implementation of the Transfer Objects's to be cleaner and more
succinct and fixed an issue where the method name constraints were excessively
restrictive
Added:
sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/to/PathBasedInvokedTO.java
- copied, changed from r1518665,
sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/to/BaseInvokedTO.java
Removed:
sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/to/BaseInvokedTO.java
sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/to/InvokedTOFactory.java
Modified:
sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/SlingInvocationHandler.java
sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/to/InvokedChildrenTO.java
sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/to/InvokedPropertyTO.java
sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/to/InvokedTO.java
sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/to/SlingServiceInvocationTO.java
Modified:
sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/SlingInvocationHandler.java
URL:
http://svn.apache.org/viewvc/sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/SlingInvocationHandler.java?rev=1518891&r1=1518890&r2=1518891&view=diff
==============================================================================
---
sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/SlingInvocationHandler.java
(original)
+++
sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/SlingInvocationHandler.java
Fri Aug 30 04:22:04 2013
@@ -36,6 +36,7 @@ import org.apache.sling.api.resource.Res
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.commons.proxy.SlingProxyService;
import org.apache.sling.commons.proxy.annotations.SlingChildren;
+import org.apache.sling.commons.proxy.annotations.SlingProperty;
import org.apache.sling.commons.proxy.annotations.SlingReference;
import org.apache.sling.commons.proxy.annotations.SlingServiceInvocation;
import org.apache.sling.commons.proxy.impl.lang.JDPEqualsImpl;
@@ -43,11 +44,9 @@ import org.apache.sling.commons.proxy.im
import org.apache.sling.commons.proxy.impl.lang.JDPToStringImpl;
import org.apache.sling.commons.proxy.impl.lang.MethodType;
import org.apache.sling.commons.proxy.impl.reflection.Annotations;
-import org.apache.sling.commons.proxy.impl.to.BaseInvokedTO;
import org.apache.sling.commons.proxy.impl.to.InvokedChildrenTO;
import org.apache.sling.commons.proxy.impl.to.InvokedPropertyTO;
-import org.apache.sling.commons.proxy.impl.to.InvokedTO;
-import org.apache.sling.commons.proxy.impl.to.InvokedTOFactory;
+import org.apache.sling.commons.proxy.impl.to.PathBasedInvokedTO;
import org.apache.sling.commons.proxy.impl.to.SlingServiceInvocationTO;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
@@ -229,7 +228,7 @@ public class SlingInvocationHandler impl
* the method invocation transfer object
* @return the resulting object
*/
- private Object handleGetReference(final BaseInvokedTO to) {
+ private Object handleGetReference(final PathBasedInvokedTO to) {
log.trace("handleGetReference");
Object value = null;
@@ -370,42 +369,35 @@ public class SlingInvocationHandler impl
public Object invoke(final Object proxy, final Method method,
final Object[] args) throws Throwable {
- // TODO: this really needs to be refactored, we shouldn't
constraining
- // the method invocations by Java-Bean naming
- final InvokedTO to = InvokedTOFactory.newInstance(method, args);
- if (to.isGetter()) {
- if (Annotations.methodHasAnnotation(method,
SlingReference.class)) {
- return (this.handleGetReference((BaseInvokedTO)
to));
- } else if (Annotations.methodHasAnnotation(method,
- SlingChildren.class)) {
- return
(this.handleGetChildren((InvokedChildrenTO) to));
- } else if (Annotations.methodHasAnnotation(method,
- SlingServiceInvocation.class)) {
- return (this
-
.handleServiceInvocation((SlingServiceInvocationTO) to));
- } else {
- return
(this.handleGetProperty((InvokedPropertyTO) to));
- }
- } else if (to.isType(MethodType.JavaBeanSet)) {
- throw new UnsupportedOperationException(
- "Setter methods not yet implemented.");
- } else if (to.isType(MethodType.ToString)) {
+ MethodType mt = MethodType.getMethodType(method);
+ if (mt == MethodType.ToString) {
return new JDPToStringImpl().toString(proxy);
- } else if (to.isType(MethodType.HashCode)) {
+ } else if (mt == MethodType.HashCode) {
return new JDPHashCodeImpl().hashCode(proxy);
- } else if (to.isType(MethodType.Equals)) {
+ } else if (mt == MethodType.Equals) {
if ((args == null) || (args.length != 1)) {
final String msg = "Method 'equals' requires
exactly 1 argument.";
throw new IllegalArgumentException(msg);
}
return new JDPEqualsImpl().equals(proxy, args[0]);
- } else if (to.isType(MethodType.BackingResource)) {
+ } else if (mt == MethodType.BackingResource) {
return this.r;
+ } else if (Annotations.methodHasAnnotation(method,
SlingProperty.class)) {
+ return
handleGetProperty(InvokedPropertyTO.newInstance(method));
+ } else if (Annotations
+ .methodHasAnnotation(method,
SlingReference.class)) {
+ SlingReference sr =
method.getAnnotation(SlingReference.class);
+ PathBasedInvokedTO to = new PathBasedInvokedTO(method,
sr.path());
+ return handleGetReference(to);
+ } else if (Annotations.methodHasAnnotation(method,
SlingChildren.class)) {
+ return
handleGetChildren(InvokedChildrenTO.newInstance(method));
+ } else if (Annotations.methodHasAnnotation(method,
+ SlingServiceInvocation.class)) {
+ return handleServiceInvocation(SlingServiceInvocationTO
+ .newInstance(method, args));
}
- // TODO: this probably should look for the annotation first,
then if
- // one's not found and it's not one of the white-listed
methods, return
- // an unsupported operation exception
- throw new NoSuchMethodException("Method " + method.getName() +
" DNE");
+ throw new UnsupportedOperationException("Method " +
method.getName()
+ + " does not have valid annotation");
}
}
Modified:
sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/to/InvokedChildrenTO.java
URL:
http://svn.apache.org/viewvc/sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/to/InvokedChildrenTO.java?rev=1518891&r1=1518890&r2=1518891&view=diff
==============================================================================
---
sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/to/InvokedChildrenTO.java
(original)
+++
sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/to/InvokedChildrenTO.java
Fri Aug 30 04:22:04 2013
@@ -20,12 +20,26 @@ package org.apache.sling.commons.proxy.i
import java.lang.reflect.Method;
-import org.apache.sling.commons.proxy.impl.lang.MethodType;
+import org.apache.sling.commons.proxy.annotations.SlingChildren;
/**
* Transfer object for SlingChildren method invocations.
*/
-public final class InvokedChildrenTO extends BaseInvokedTO {
+public final class InvokedChildrenTO extends PathBasedInvokedTO {
+
+ /**
+ * Returns a new instance of the InvokedChildrenTO
+ *
+ * @param method
+ * the method being invoked
+ * @return the InvokedChildrenTO
+ */
+ public static InvokedChildrenTO newInstance(Method method) {
+ SlingChildren sc = method.getAnnotation(SlingChildren.class);
+ String path = sc.path();
+ InvokedChildrenTO to = new InvokedChildrenTO(method, sc, path);
+ return to;
+ }
/**
* The generic type to return when constructing the Iterator of
children.
@@ -46,10 +60,10 @@ public final class InvokedChildrenTO ext
* @param mt
* the method type
*/
- protected InvokedChildrenTO(final Method method, final Object[] args,
- final String path, final Class<?> returnType, final
MethodType mt) {
- super(method, path, mt);
- this.returnType = returnType;
+ protected InvokedChildrenTO(final Method method, final SlingChildren sc,
+ final String path) {
+ super(method, path);
+ this.returnType = sc.returnType();
}
/**
Modified:
sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/to/InvokedPropertyTO.java
URL:
http://svn.apache.org/viewvc/sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/to/InvokedPropertyTO.java?rev=1518891&r1=1518890&r2=1518891&view=diff
==============================================================================
---
sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/to/InvokedPropertyTO.java
(original)
+++
sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/to/InvokedPropertyTO.java
Fri Aug 30 04:22:04 2013
@@ -21,14 +21,26 @@ package org.apache.sling.commons.proxy.i
import java.lang.reflect.Method;
import org.apache.sling.commons.proxy.annotations.SlingProperty;
-import org.apache.sling.commons.proxy.impl.lang.MethodType;
/**
* InvokedPropertyTO - Transfer Object Meant as a convenient way to maintain
and
* pass a method invocation around The members are not private because the
class
* is package protected and they are final.
*/
-public final class InvokedPropertyTO extends BaseInvokedTO {
+public final class InvokedPropertyTO extends PathBasedInvokedTO {
+
+ /**
+ * Returns a new instance of the InvokedPropertyTO
+ *
+ * @param method
+ * the method being invoked
+ * @return the InvokedPropertyTO
+ */
+ public static InvokedPropertyTO newInstance(Method method) {
+ SlingProperty sp = method.getAnnotation(SlingProperty.class);
+ InvokedPropertyTO to = new InvokedPropertyTO(method, sp,
sp.path());
+ return to;
+ }
/** The default bytes. */
private final byte[] defaultBytes;
@@ -59,10 +71,11 @@ public final class InvokedPropertyTO ext
* annotation parameters.
*/
private final String propertyName;
-
+
/** The use default flag. */
private final boolean useDefault;
+ /** The default boolean value */
private final boolean defaultBoolean;
/**
@@ -70,22 +83,16 @@ public final class InvokedPropertyTO ext
*
* @param method
* the invoked method
- * @param args
- * the method arguments
+ * @param sp
+ * the sling property annotation
* @param path
* the path annotation value
- * @param name
- * the name annotation value
- * @param mt
- * the type of method invoked
- */
- protected InvokedPropertyTO(final Method method, final Object[] args,
- final String path, final String name, final MethodType
mt) {
- super(method, path, mt);
- this.name = name;
+ */
+ protected InvokedPropertyTO(final Method method, SlingProperty sp,
+ String path) {
+ super(method, path);
+ this.name = sp.name();
this.propertyName = (path != null ? path + "/" : "") + name;
-
- final SlingProperty sp =
method.getAnnotation(SlingProperty.class);
this.defaultBoolean = sp.defaultBoolean();
this.defaultBytes = sp.defaultBytes();
this.defaultDate = sp.defaultDate();
@@ -98,6 +105,11 @@ public final class InvokedPropertyTO ext
}
+ /**
+ * Gets the default boolean value.
+ *
+ * @return the default boolean
+ */
public boolean getDefaultBoolean() {
return this.defaultBoolean;
}
Modified:
sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/to/InvokedTO.java
URL:
http://svn.apache.org/viewvc/sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/to/InvokedTO.java?rev=1518891&r1=1518890&r2=1518891&view=diff
==============================================================================
---
sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/to/InvokedTO.java
(original)
+++
sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/to/InvokedTO.java
Fri Aug 30 04:22:04 2013
@@ -18,56 +18,84 @@
*/
package org.apache.sling.commons.proxy.impl.to;
+import java.lang.reflect.Method;
+
import org.apache.sling.commons.proxy.impl.lang.MethodType;
/**
* Represents a method invocation on the InvocationHandler.
*/
-public interface InvokedTO {
+public class InvokedTO {
+
+ /**
+ * The method which was invokedF
+ */
+ private Method method;
+
+ /**
+ * The type of the method being invoked.
+ */
+ private MethodType mt;
- public static final InvokedTO UNKNOWN = new InvokedPropertyTO(null,
null,
- null, null, MethodType.Unknown);
+ /**
+ * Constructs a new InvokedTO from the method.
+ *
+ * @param method
+ * the method being invoked
+ */
+ protected InvokedTO(Method method) {
+ this.method = method;
+ this.mt = MethodType.getMethodType(method);
+ }
/**
- * Determines if the item represented by this method Invocation is an
- * absolute path reference, starts with a /.
+ * Gets the method which was invoked.
*
- * @return TRUE if the path starts with /, FALSE otherwise
+ * @return the invoked method
*/
- public boolean isAbsolute();
+ public Method getMethod() {
+ return method;
+ }
/**
- * Determines if the current Invocation is a 'Getter' method - that is a
- * method that starts with 'get' or 'is', has no arguments, and returns
a
- * value.
+ * Gets the type of method which was invoked.
*
- * @return TRUE if it is a 'get' or 'is' method, FALSE otherwise
+ * @return the type of method invoked
*/
- public boolean isGetter();
+ public MethodType getMethodType() {
+ return mt;
+ }
/**
- * Determines if the current Invocation is named in the JavaBeans
style, '
- * that is 'get', 'is' or 'set'.
+ * Checks whether or not the method invoked was a getter.
*
- * @return TRUE if the method is JavaBean compliant, FALSE otherwise
+ * @return true if the method is a JavaBean getter
*/
- public boolean isJavaBean();
+ public boolean isGetter() {
+ return MethodType.contains(new MethodType[] {
MethodType.JavaBeanIs,
+ MethodType.JavaBeanGet }, this.mt);
+ }
/**
- * Determines if the item represented by this method Invocation is a
- * relative path reference, that is a descendant of the backing Resource
+ * Checks whether or not the method invoked is compliant with JavaBean
+ * naming conventions.
*
- * @return TRUE if the path contains / but does not start with /, FALSE
- * otherwise
+ * @return true if the method is a valid JavaBean method
*/
- public boolean isRelative();
+ public boolean isJavaBean() {
+ return MethodType.contains(new MethodType[] {
MethodType.JavaBeanIs,
+ MethodType.JavaBeanGet, MethodType.JavaBeanSet
}, this.mt);
+ }
/**
- * Determines if the current Invocation is of type
<code>methodType</code>
+ * Checks if the method is of the specified type.
*
- * @param methodType
- * MethodType
- * @return TRUE if it is, FALSE otherwise
+ * @param _mt
+ * the type to check
+ * @return if the method invoked is the same type as the specified
method
+ * type
*/
- public boolean isType(MethodType methodType);
+ public boolean isType(final MethodType _mt) {
+ return this.mt == _mt;
+ }
}
Copied:
sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/to/PathBasedInvokedTO.java
(from r1518665,
sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/to/BaseInvokedTO.java)
URL:
http://svn.apache.org/viewvc/sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/to/PathBasedInvokedTO.java?p2=sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/to/PathBasedInvokedTO.java&p1=sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/to/BaseInvokedTO.java&r1=1518665&r2=1518891&rev=1518891&view=diff
==============================================================================
---
sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/to/BaseInvokedTO.java
(original)
+++
sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/to/PathBasedInvokedTO.java
Fri Aug 30 04:22:04 2013
@@ -20,19 +20,12 @@ package org.apache.sling.commons.proxy.i
import java.lang.reflect.Method;
-import org.apache.sling.commons.proxy.impl.lang.MethodType;
-
/**
- * BaseInvokedTO - Transfer Object Meant as a convenient way to maintain and
+ * PathBasedInvokedTO - Transfer Object Meant as a convenient way to maintain
and
* pass a method invocation around The members are not private because the
class
* is package protected and they are final.
*/
-public class BaseInvokedTO implements InvokedTO {
-
- /**
- * The method being invoked
- */
- private final Method method;
+public class PathBasedInvokedTO extends InvokedTO {
/**
* The path from the annotations
@@ -40,11 +33,6 @@ public class BaseInvokedTO implements In
private final String path;
/**
- * The type of method being invoked
- */
- private final MethodType mt;
-
- /**
* Constructs a new Base Invoked Transfer Object.
*
* @param method
@@ -54,29 +42,9 @@ public class BaseInvokedTO implements In
* @param mt
* the type of method being invoked
*/
- protected BaseInvokedTO(final Method method, final String path,
- final MethodType mt) {
- this.method = method;
+ public PathBasedInvokedTO(final Method method, final String path) {
+ super(method);
this.path = path;
- this.mt = mt;
- }
-
- /**
- * Gets the invoked method.
- *
- * @return the method
- */
- public final Method getMethod() {
- return this.method;
- }
-
- /**
- * Gets the method type, derived from the method name.
- *
- * @return the method type
- */
- public final MethodType getMt() {
- return this.mt;
}
/**
@@ -88,53 +56,23 @@ public class BaseInvokedTO implements In
return this.path;
}
- /*
- * (non-Javadoc)
+ /**
+ * Checks whether or not the path is absolute (starts with '/').
*
- * @see org.apache.sling.commons.proxy.impl.InvokedTO#isAbsolute()
+ * @return true if the path is relative
*/
public boolean isAbsolute() {
return (this.path != null) && this.path.startsWith("/");
}
- /*
- * (non-Javadoc)
- *
- * @see org.apache.sling.commons.proxy.impl.InvokedTO#isGetter()
- */
- public boolean isGetter() {
- return MethodType.contains(new MethodType[] {
MethodType.JavaBeanIs,
- MethodType.JavaBeanGet }, this.mt);
- }
-
- /*
- * (non-Javadoc)
- *
- * @see org.apache.sling.commons.proxy.impl.InvokedTO#isJavaBean()
- */
- public boolean isJavaBean() {
- return MethodType.contains(new MethodType[] {
MethodType.JavaBeanIs,
- MethodType.JavaBeanGet, MethodType.JavaBeanSet
}, this.mt);
- }
-
- /*
- * (non-Javadoc)
+ /**
+ * Checks whether or not the path is relative (does not start with '/').
*
- * @see org.apache.sling.commons.proxy.impl.InvokedTO#isRelative()
+ * @return true if the path is relative
*/
public boolean isRelative() {
return (this.path != null) && !this.isAbsolute()
&& this.path.contains("/");
}
- /*
- * (non-Javadoc)
- *
- * @see
- * org.apache.sling.commons.proxy.impl.InvokedTO#isType(org.apache.sling
- * .commons.proxy.impl.lang.MethodType)
- */
- public boolean isType(final MethodType _mt) {
- return this.mt == _mt;
- }
}
Modified:
sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/to/SlingServiceInvocationTO.java
URL:
http://svn.apache.org/viewvc/sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/to/SlingServiceInvocationTO.java?rev=1518891&r1=1518890&r2=1518891&view=diff
==============================================================================
---
sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/to/SlingServiceInvocationTO.java
(original)
+++
sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/to/SlingServiceInvocationTO.java
Fri Aug 30 04:22:04 2013
@@ -20,12 +20,12 @@ package org.apache.sling.commons.proxy.i
import java.lang.reflect.Method;
-import org.apache.sling.commons.proxy.impl.lang.MethodType;
+import org.apache.sling.commons.proxy.annotations.SlingServiceInvocation;
/**
* Transfer object for SlingServiceInvocation method invocations.
*/
-public final class SlingServiceInvocationTO extends BaseInvokedTO {
+public final class SlingServiceInvocationTO extends InvokedTO {
/**
* The service to invoke
@@ -38,6 +38,21 @@ public final class SlingServiceInvocatio
private Object[] args;
/**
+ * Gets a new instance of the SlingServiceInvocationTO.
+ *
+ * @param method
+ * the method being invoked
+ * @param args
+ * the method arguments
+ * @return a new SlingServiceInvocationTO
+ */
+ public static SlingServiceInvocationTO newInstance(Method method,
+ Object[] args) {
+ SlingServiceInvocationTO to = new
SlingServiceInvocationTO(method, args);
+ return to;
+ }
+
+ /**
* Constructs a new Invoked Children Transfer Object.
*
* @param method
@@ -49,10 +64,11 @@ public final class SlingServiceInvocatio
* @param mt
* the method type
*/
- protected SlingServiceInvocationTO(final Method method,
- final Object[] args, final Class<?> service, final
MethodType mt) {
- super(method, null, mt);
- this.service = service;
+ protected SlingServiceInvocationTO(final Method method, Object[] args) {
+ super(method);
+ SlingServiceInvocation ssi = method
+ .getAnnotation(SlingServiceInvocation.class);
+ this.service = ssi.service();
this.args = args;
}