com.syncleus.ferma.AbstractVertexFrame Java Examples

The following examples show how to use com.syncleus.ferma.AbstractVertexFrame. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PolymorphicTypeResolver.java    From Ferma with Apache License 2.0 6 votes vote down vote up
@Override
public <T> Class<? extends T> resolve(final Element element, final Class<T> kind) {
    final Property<String> nodeClazzProperty = element.<String>property(this.typeResolutionKey);
    final String nodeClazz;
    if( nodeClazzProperty.isPresent() )
        nodeClazz = nodeClazzProperty.value();
    else
        return kind;

    final Class<T> nodeKind = (Class<T>) this.reflectionCache.forName(nodeClazz);

    if (kind.isAssignableFrom(nodeKind) || kind.equals(VertexFrame.class) || kind.equals(EdgeFrame.class) || kind.equals(AbstractVertexFrame.class) || kind.equals(AbstractEdgeFrame.class) || kind.
          equals(Object.class))
        return nodeKind;
    else
        return kind;
}
 
Example #2
Source File: UntypedTypeResolver.java    From Ferma with Apache License 2.0 5 votes vote down vote up
@Override
public <T> Class<? extends T> resolve(final Element element, final Class<T> kind) {
    if (VertexFrame.class.equals(kind) || AbstractVertexFrame.class.equals(kind))
        return (Class<? extends T>) TVertex.class;
    else if (EdgeFrame.class.equals(kind) || AbstractEdgeFrame.class.equals(kind))
        return (Class<? extends T>) TEdge.class;
    return kind;
}
 
Example #3
Source File: UntypedTypeResolverTest.java    From Ferma with Apache License 2.0 5 votes vote down vote up
@Test
public void testResolveWithDefault() {
    Element mockElement = Mockito.mock(Element.class);
    Assert.assertEquals(resolver.resolve(mockElement, VertexFrame.class), TVertex.class);
    Mockito.verifyZeroInteractions(mockElement);
    Assert.assertEquals(resolver.resolve(mockElement, AbstractVertexFrame.class), TVertex.class);
    Mockito.verifyZeroInteractions(mockElement);
    Assert.assertEquals(resolver.resolve(mockElement, EdgeFrame.class), TEdge.class);
    Mockito.verifyZeroInteractions(mockElement);
    Assert.assertEquals(resolver.resolve(mockElement, AbstractEdgeFrame.class), TEdge.class);
    Mockito.verifyZeroInteractions(mockElement);
    Assert.assertEquals(resolver.resolve(mockElement, God.class), God.class);
    Mockito.verifyZeroInteractions(mockElement);
}
 
Example #4
Source File: AbstractAnnotationFrameFactory.java    From windup with Eclipse Public License 1.0 4 votes vote down vote up
private <E> Class<? extends E> constructClass(final Element element, final Class<E> clazz)
{
    Class constructedClass = constructedClassCache.get(clazz);
    if (constructedClass != null)
        return constructedClass;

    DynamicType.Builder<? extends E> classBuilder;
    if (clazz.isInterface())
    {
        if (element instanceof Vertex)
            classBuilder = (DynamicType.Builder<? extends E>) new ByteBuddy().subclass(AbstractVertexFrame.class);
        else if (element instanceof Edge)
            classBuilder = (DynamicType.Builder<? extends E>) new ByteBuddy().subclass(AbstractEdgeFrame.class);
        else
            throw new IllegalStateException("class is neither an Edge or a vertex!");

        if (clazz.getCanonicalName().contains("ByteBuddy"))
        {
            // if the input class is itself a bytebuddy class, only take its interfaces
            classBuilder = classBuilder.implement(clazz.getInterfaces());
        }
        else
        {
            classBuilder = classBuilder.implement(clazz);
        }
    }
    else
    {
        if (!(element instanceof Vertex || element instanceof Edge))
            throw new IllegalStateException("element is neither an edge nor a vertex");
        else if (element instanceof Vertex && !VertexFrame.class.isAssignableFrom(clazz))
            throw new IllegalStateException(clazz.getName() + " Class is not a type of VertexFrame");
        else if (element instanceof Edge && !EdgeFrame.class.isAssignableFrom(clazz))
            throw new IllegalStateException(clazz.getName() + " Class is not a type of EdgeFrame");
        classBuilder = new ByteBuddy().subclass(clazz);
    }

    classBuilder = classBuilder.defineField("reflectionCache", ReflectionCache.class, Visibility.PRIVATE, FieldManifestation.PLAIN)
                .implement(CachesReflection.class).intercept(FieldAccessor.ofBeanProperty());

    /*
     * Just a hack so that our generified frame types can work.
     *
     * This information will not really be used by the generated class.
     */
    classBuilder = classBuilder.typeVariable("T");

    // try and construct any abstract methods that are left
    for (final Method method : clazz.getMethods())
        if (isAbstract(method))
            annotation_loop: for (final Annotation annotation : method.getAnnotations())
            {
                final MethodHandler handler = methodHandlers.get(annotation.annotationType());
                if (handler != null)
                {
                    classBuilder = handler.processMethod(classBuilder, method, annotation);
                    break;
                }
            }

    DynamicType.Unloaded unloadedClass = classBuilder.make();
    constructedClass = unloadedClass.load(this.classLoader, ClassLoadingStrategy.Default.WRAPPER).getLoaded();
    this.constructedClassCache.put(clazz, constructedClass);
    return constructedClass;
}