Permalink
Browse files

Merge pull request #7 from 0xcaos/master

proposal for annotation extension system -- via seralf.
  • Loading branch information...
okram committed Dec 15, 2011
2 parents 812bc1e + 637f208 commit 996d790905cd45fa9e977ecd6ff5621804a871a8
@@ -1,59 +0,0 @@
-package com.tinkerpop.frames;
-
-import com.tinkerpop.blueprints.pgm.Edge;
-
-import java.lang.annotation.Annotation;
-import java.lang.reflect.Method;
-
-/**
- * The proxy class of a framed edge.
- *
- * @author Marko A. Rodriguez (http://markorodriguez.com)
- * @author Joshua Shinavier (http://fortytwo.net)
- */
-public class FramedEdge extends FramedElement {
-
- private Direction direction;
-
- public FramedEdge(final FramesManager manager, final Edge edge, final Direction direction) {
- super(manager, edge);
- this.direction = direction;
- }
-
- public Object invoke(final Object proxy, final Method method, final Object[] arguments) {
- final Object returnObject = super.invoke(proxy, method, arguments);
- if (NO_INVOCATION_PATH != returnObject) {
- return returnObject;
- } else {
- if (isEdgeGetter(method)) {
- return getElement();
- }
-
- final Annotation[] anns = method.getAnnotations();
- for (final Annotation ann : anns) {
- if (ann instanceof Domain & isGetMethod(method)) {
- if (this.direction.equals(Direction.STANDARD)) {
- return this.manager.frame(((Edge) element).getOutVertex(), method.getReturnType());
- } else {
- return this.manager.frame(((Edge) element).getInVertex(), method.getReturnType());
- }
- } else if (ann instanceof Range & isGetMethod(method)) {
- if (this.direction.equals(Direction.STANDARD)) {
- return this.manager.frame(((Edge) element).getInVertex(), method.getReturnType());
- } else {
- return this.manager.frame(((Edge) element).getOutVertex(), method.getReturnType());
- }
- }
- }
- throw new RuntimeException("Proxy can not invoke method: " + method);
- }
- }
-
- protected boolean isEdgeGetter(final Method method) {
- return method.getName().equals("asEdge");
- }
-
- protected Edge getEdge() {
- return (Edge) this.element;
- }
-}
@@ -1,6 +1,8 @@
package com.tinkerpop.frames;
+import com.tinkerpop.blueprints.pgm.Edge;
import com.tinkerpop.blueprints.pgm.Element;
+import com.tinkerpop.blueprints.pgm.Vertex;
import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationHandler;
@@ -12,18 +14,15 @@
*
* @author Marko A. Rodriguez (http://markorodriguez.com)
*/
-public abstract class FramedElement implements InvocationHandler {
+public class FramedElement implements InvocationHandler {
- protected final FramesManager manager;
+ private final Direction direction;
+ protected final FramesManager manager;
protected final Element element;
private static Method hashCodeMethod;
private static Method equalsMethod;
private static Method toStringMethod;
- private static final String SET = "set";
- private static final String GET = "get";
- private static final String REMOVE = "remove";
-
protected static Object NO_INVOCATION_PATH = new Object();
@@ -37,12 +36,16 @@
}
}
- public FramedElement(final FramesManager manager, final Element element) {
+ public FramedElement(final FramesManager manager, final Element element, final Direction direction) {
this.element = element;
this.manager = manager;
+ this.direction = direction;
+ }
+ public FramedElement(final FramesManager manager, final Element element) {
+ this(manager, element, null);
}
-
+
public Object invoke(final Object proxy, final Method method, final Object[] arguments) {
if (method.equals(hashCodeMethod)) {
@@ -52,57 +55,56 @@ public Object invoke(final Object proxy, final Method method, final Object[] arg
} else if (method.equals(toStringMethod)) {
return proxyToString(proxy);
}
-
+
+ if (isVertexGetter(method) && element instanceof Vertex) {
+ return (Vertex) getElement();
+ }else if(isEdgeGetter(method) && element instanceof Edge) {
+ return (Edge) getElement();
+ }
+
final Annotation[] annotations = method.getAnnotations();
for (final Annotation annotation : annotations) {
- if (annotation instanceof Property) {
- if (isGetMethod(method)) {
- return this.element.getProperty(((Property) annotation).value());
- } else if (isSetMethod(method)) {
- this.element.setProperty(((Property) annotation).value(), arguments[0]);
- return null;
- } else if (isRemoveMethod(method)) {
- this.element.removeProperty(((Property) annotation).value());
- return null;
- }
+ if(manager.hasAnnotationHandler(annotation.annotationType())){
+ if(element instanceof Vertex){
+ return manager.getAnnotationHandler(annotation.annotationType())
+ .processVertex(annotation, method, arguments, this.manager, (Vertex)this.element);
+ }else if(element instanceof Edge){
+ return manager.getAnnotationHandler(annotation.annotationType())
+ .processEdge(annotation, method, arguments, this.manager, (Edge)this.element, direction);
+ }
}
}
return NO_INVOCATION_PATH;
}
- protected boolean isGetMethod(final Method method) {
- return method.getName().startsWith(GET);
- }
-
- protected boolean isSetMethod(final Method method) {
- return method.getName().startsWith(SET);
- }
-
- protected boolean isRemoveMethod(final Method method) {
- return method.getName().startsWith(REMOVE);
- }
-
- protected Integer proxyHashCode(final Object proxy) {
+ private Integer proxyHashCode(final Object proxy) {
return System.identityHashCode(proxy) + this.element.hashCode();
}
- protected Boolean proxyEquals(final Object proxy, final Object other) {
+ private Boolean proxyEquals(final Object proxy, final Object other) {
if (proxy.getClass().equals(other.getClass())) {
return ((FramedElement) (Proxy.getInvocationHandler(proxy))).getElement().getId().equals(((FramedElement) (Proxy.getInvocationHandler(other))).getElement().getId());
} else {
return Boolean.FALSE;
}
}
- protected String proxyToString(final Object proxy) {
+ private String proxyToString(final Object proxy) {
return "Framed[" + ((FramedElement) Proxy.getInvocationHandler(proxy)).getElement().toString() + "]";
}
- protected Element getElement() {
+ public Element getElement() {
return this.element;
}
-
+
+ protected boolean isVertexGetter(final Method method) {
+ return method.getName().equals("asVertex");
+ }
+
+ protected boolean isEdgeGetter(final Method method) {
+ return method.getName().equals("asEdge");
+ }
}
@@ -1,160 +0,0 @@
-package com.tinkerpop.frames;
-
-import com.tinkerpop.blueprints.pgm.Edge;
-import com.tinkerpop.blueprints.pgm.Graph;
-import com.tinkerpop.blueprints.pgm.Vertex;
-import com.tinkerpop.frames.util.AdjacencyCollection;
-import com.tinkerpop.frames.util.RelationCollection;
-import sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl;
-
-import java.lang.annotation.Annotation;
-import java.lang.reflect.Method;
-import java.lang.reflect.Proxy;
-import java.lang.reflect.Type;
-import java.util.Collection;
-import java.util.LinkedList;
-import java.util.List;
-
-/**
- * The proxy class of a framed vertex.
- *
- * @author Marko A. Rodriguez (http://markorodriguez.com)
- * @author Joshua Shinavier (http://fortytwo.net)
- */
-public class FramedVertex extends FramedElement {
-
- private static final String ADD = "add";
-
- public FramedVertex(final FramesManager manager, final Vertex vertex) {
- super(manager, vertex);
- }
-
- private boolean returnsCollection(final Method method) {
- return Collection.class.isAssignableFrom(method.getReturnType());
- }
-
- private boolean acceptsCollection(final Method method) {
-
- return 1 == method.getParameterTypes().length
- && Collection.class.isAssignableFrom(method.getParameterTypes()[0]);
- }
-
- public Object invoke(final Object proxy, final Method method, final Object[] arguments) {
- final Object returnObject = super.invoke(proxy, method, arguments);
- if (NO_INVOCATION_PATH != returnObject) {
- return returnObject;
- } else {
- if (isVertexGetter(method)) {
- return getVertex();
- }
-
- final Annotation[] annotations = method.getAnnotations();
- for (final Annotation annotation : annotations) {
- if (annotation instanceof Relation) {
- final Relation relation = (Relation) annotation;
- if (isGetMethod(method)) {
- RelationCollection r = new RelationCollection(this.manager, (Vertex) this.element, relation.label(), relation.direction(), getGenericClass(method));
- if (returnsCollection(method)) {
- return r;
- } else {
- return r.iterator().hasNext() ? r.iterator().next() : null;
- }
- } else if (isAddMethod(method)) {
- if (relation.direction().equals(Direction.STANDARD))
- this.manager.getGraph().addEdge(null, (Vertex) this.element, ((FramedVertex) Proxy.getInvocationHandler(arguments[0])).getVertex(), relation.label());
- else
- this.manager.getGraph().addEdge(null, ((FramedVertex) Proxy.getInvocationHandler(arguments[0])).getVertex(), (Vertex) this.element, relation.label());
- return null;
- } else if (isRemoveMethod(method)) {
- this.removeEdges(relation.direction(), relation.label(), ((FramedVertex) Proxy.getInvocationHandler(arguments[0])).getVertex());
- return null;
- } else if (isSetMethod(method)) {
- this.removeEdges(relation.direction(), relation.label(), null);
- if (acceptsCollection(method)) {
- for (Object o : (Collection) arguments[0]) {
- Vertex v = ((FramedVertex) Proxy.getInvocationHandler(o)).getVertex();
- if (relation.direction().equals(Direction.STANDARD)) {
- this.manager.getGraph().addEdge(null, (Vertex) this.element, v, relation.label());
- } else {
- this.manager.getGraph().addEdge(null, v, (Vertex) this.element, relation.label());
- }
- }
- return null;
- } else {
- if (null != arguments[0]) {
- if (relation.direction().equals(Direction.STANDARD)) {
- this.manager.getGraph().addEdge(null, (Vertex) this.element, ((FramedVertex) Proxy.getInvocationHandler(arguments[0])).getVertex(), relation.label());
- } else {
- this.manager.getGraph().addEdge(null, ((FramedVertex) Proxy.getInvocationHandler(arguments[0])).getVertex(), (Vertex) this.element, relation.label());
- }
- }
- return null;
- }
- }
- } else if (annotation instanceof Adjacency) {
- final Adjacency adjacency = (Adjacency) annotation;
- if (isGetMethod(method)) {
- return new AdjacencyCollection(this.manager, (Vertex) this.element, adjacency.label(), adjacency.direction(), getGenericClass(method));
- } else if (isAddMethod(method)) {
- if (adjacency.direction().equals(Direction.STANDARD))
- return this.manager.frame(this.manager.getGraph().addEdge(null, (Vertex) this.element, ((FramedVertex) Proxy.getInvocationHandler(arguments[0])).getVertex(), adjacency.label()), Direction.STANDARD, method.getReturnType());
- else
- return this.manager.frame(this.manager.getGraph().addEdge(null, ((FramedVertex) Proxy.getInvocationHandler(arguments[0])).getVertex(), (Vertex) this.element, adjacency.label()), Direction.INVERSE, method.getReturnType());
- } else if (isRemoveMethod(method)) {
- this.manager.getGraph().removeEdge(((FramedEdge) Proxy.getInvocationHandler(arguments[0])).getEdge());
- return null;
- }
- } /*else if (annotation instanceof GremlinInference) {
- final GremlinInference gremlinInference = (GremlinInference) annotation;
- if (isGetMethod(method)) {
- Pipe<Vertex, Vertex> pipe = Gremlin.compile(gremlinInference.script());
- pipe.setStarts(new SingleIterator<Vertex>(this.getVertex()));
- return new IterableCollection(new FramingVertexIterable(this.manager, pipe, getGenericClass(method)));
- }
- }*/
- }
- throw new RuntimeException("Proxy can not invoke method: " + method);
- }
- }
-
- protected boolean isVertexGetter(final Method method) {
- return method.getName().equals("asVertex");
- }
-
- private Class getGenericClass(final Method method) {
- final Type returnType = method.getGenericReturnType();
- if (returnType instanceof ParameterizedTypeImpl)
- return (Class) ((ParameterizedTypeImpl) returnType).getActualTypeArguments()[0];
- else
- return method.getReturnType();
- }
-
- private void removeEdges(final Direction direction, final String label, final Vertex otherVertex) {
- final Graph graph = this.manager.getGraph();
- List<Edge> toRemove = new LinkedList<Edge>();
- if (direction.equals(Direction.STANDARD)) {
- for (final Edge edge : this.getVertex().getOutEdges(label)) {
- if (null == otherVertex || edge.getInVertex().equals(otherVertex)) {
- toRemove.add(edge);
- }
- }
- } else {
- for (final Edge edge : this.getVertex().getInEdges(label)) {
- if (null == otherVertex || edge.getOutVertex().equals(otherVertex)) {
- toRemove.add(edge);
- }
- }
- }
- for (final Edge edge : toRemove) {
- graph.removeEdge(edge);
- }
- }
-
- protected boolean isAddMethod(final Method method) {
- return method.getName().startsWith(ADD);
- }
-
- protected Vertex getVertex() {
- return (Vertex) this.element;
- }
-}
Oops, something went wrong.

0 comments on commit 996d790

Please sign in to comment.