Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/main/java/net/vidageek/mirror/DefaultAccessorsController.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import net.vidageek.mirror.invoke.DefaultInvocationHandler;
import net.vidageek.mirror.invoke.dsl.InvocationHandler;
import net.vidageek.mirror.provider.ReflectionProvider;
import net.vidageek.mirror.reflect.DefaultTypeHandler;
import net.vidageek.mirror.set.DefaultSetterHandler;
import net.vidageek.mirror.set.dsl.SetterHandler;

Expand All @@ -19,9 +20,9 @@
*/
public final class DefaultAccessorsController implements AccessorsController {

private final Object target;
private final Object target;

private final ReflectionProvider provider;
private final ReflectionProvider provider;

public DefaultAccessorsController(final ReflectionProvider provider, final Object target) {
if (target == null) {
Expand All @@ -42,4 +43,12 @@ public SetterHandler set() {
public GetterHandler get() {
return new DefaultGetterHandler(provider, target);
}
}

public boolean isCollection() {
return new DefaultTypeHandler(target.getClass()).isCollection();
}

public boolean isPrimitive() {
return new DefaultTypeHandler(target.getClass()).isPrimitive();
}
}
14 changes: 11 additions & 3 deletions src/main/java/net/vidageek/mirror/DefaultClassController.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import net.vidageek.mirror.provider.ReflectionProvider;
import net.vidageek.mirror.reflect.DefaultAllReflectionHandler;
import net.vidageek.mirror.reflect.DefaultReflectionHandler;
import net.vidageek.mirror.reflect.DefaultTypeHandler;
import net.vidageek.mirror.reflect.dsl.AllReflectionHandler;
import net.vidageek.mirror.reflect.dsl.ReflectionHandler;
import net.vidageek.mirror.set.DefaultSetterHandler;
Expand All @@ -23,9 +24,9 @@
*/
public final class DefaultClassController<T> implements ClassController<T> {

private final Class<T> clazz;
private final Class<T> clazz;

private final ReflectionProvider provider;
private final ReflectionProvider provider;

public DefaultClassController(final ReflectionProvider provider, final Class<T> clazz) {
this.provider = provider;
Expand Down Expand Up @@ -55,4 +56,11 @@ public AllReflectionHandler<T> reflectAll() {
return new DefaultAllReflectionHandler<T>(provider, clazz);
}

}
public boolean isCollection() {
return new DefaultTypeHandler(clazz).isCollection();
}

public boolean isPrimitive() {
return new DefaultTypeHandler(clazz).isPrimitive();
}
}
16 changes: 15 additions & 1 deletion src/main/java/net/vidageek/mirror/dsl/AccessorsController.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,18 @@ public interface AccessorsController {
*/
public GetterHandler get();

}
/**
* This part of the DSL verifies if the class is a collection.
*
* @return true if reflected class is a collection.
*/
public boolean isCollection();

/**
* This part of the DSL verifies if the class is a primitive.
*
* @return true if reflected class is a primitive.
*/
public boolean isPrimitive();

}
15 changes: 14 additions & 1 deletion src/main/java/net/vidageek/mirror/dsl/ClassController.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,17 @@ public interface ClassController<T> {
*/
public AllReflectionHandler<T> reflectAll();

}
/**
* This part of the DSL verifies if the class is a collection.
*
* @return true if reflected class is a collection.
*/
public boolean isCollection();

/**
* This part of the DSL verifies if the class is a primitive.
*
* @return true if reflected class is a primitive.
*/
public boolean isPrimitive();
}
41 changes: 41 additions & 0 deletions src/main/java/net/vidageek/mirror/reflect/DefaultTypeHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package net.vidageek.mirror.reflect;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.Map;

import net.vidageek.mirror.reflect.dsl.TypeHandler;

public class DefaultTypeHandler implements TypeHandler {

private Class<?> clazz;

public DefaultTypeHandler(Class<?> clazz) {
this.clazz = clazz;
}

public boolean isCollection() {
Type type = clazz;
if (type instanceof ParameterizedType) {
ParameterizedType ptype = (ParameterizedType) type;
return Collection.class.isAssignableFrom((Class<?>) ptype.getRawType())
|| Map.class.isAssignableFrom((Class<?>) ptype.getRawType());
}

return Collection.class.isAssignableFrom((Class<?>) type);
}

public boolean isPrimitive() {
return clazz.isPrimitive()
|| clazz.isEnum()
|| Number.class.isAssignableFrom(clazz)
|| clazz.equals(String.class)
|| Date.class.isAssignableFrom(clazz)
|| Calendar.class.isAssignableFrom(clazz)
|| Boolean.class.equals(clazz)
|| Character.class.equals(clazz);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package net.vidageek.mirror.reflect.dsl;

public interface TypeHandler {
public boolean isCollection();

public boolean isPrimitive();
}
4 changes: 2 additions & 2 deletions src/test/java/net/vidageek/mirror/dsl/MirrorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public Object intercepts(final Object target, final Method method, final Object.
@Test
public void testThatProxifyMoreThanOneInterfaceIsCorrect() {
Object proxy = new Mirror().proxify(OneClassFixture.class, OneInterfaceFixture.class,
OtherInterfaceFixture.class).interceptingWith(new MethodInterceptor() {
OtherInterfaceFixture.class).interceptingWith(new MethodInterceptor() {

public boolean accepts(final Method method) {
return true;
Expand All @@ -108,4 +108,4 @@ public Object intercepts(final Object target, final Method method, final Object.
assertEquals("foo", ((OtherInterfaceFixture) proxy).otherInterfaceMethod());
assertEquals("foo", ((OneClassFixture) proxy).classMethod());
}
}
}
23 changes: 23 additions & 0 deletions src/test/java/net/vidageek/mirror/reflect/TypeHandlerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package net.vidageek.mirror.reflect;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.util.ArrayList;

import org.junit.Test;

public class TypeHandlerTest {

@Test
public void testThatReflectClassIsACollection() {
assertTrue(new DefaultTypeHandler(ArrayList.class).isCollection());
assertFalse(new DefaultTypeHandler(Integer.class).isCollection());
}

@Test
public void testThatReflectClassIsAPrimitive() {
assertTrue(new DefaultTypeHandler(Integer.class).isPrimitive());
assertFalse(new DefaultTypeHandler(ArrayList.class).isPrimitive());
}
}