add instantiator interface set default reflection instantiators

This commit is contained in:
Albert Shift 2015-03-26 21:02:48 -07:00
parent e59f98fb3d
commit 82bbb6cde7
6 changed files with 126 additions and 18 deletions

View file

@ -18,6 +18,8 @@ package casser.config;
import java.lang.reflect.Method;
import java.util.function.Function;
import casser.core.Instantiator;
public interface CasserSettings {
Function<String, String> getMethodNameToPropertyConverter();
@ -27,6 +29,9 @@ public interface CasserSettings {
Function<Method, Boolean> getGetterMethodDetector();
Function<Method, Boolean> getSetterMethodDetector();
Instantiator getDslInstantiator();
Instantiator getPojoInstantiator();
}

View file

@ -18,6 +18,9 @@ package casser.config;
import java.lang.reflect.Method;
import java.util.function.Function;
import casser.core.Instantiator;
import casser.core.reflect.ReflectionDslInstantiator;
import casser.core.reflect.ReflectionPojoInstantiator;
import casser.mapping.convert.CamelCaseToUnderscoreConverter;
import casser.mapping.convert.MethodNameToPropertyConverter;
@ -43,4 +46,16 @@ public class DefaultCasserSettings implements CasserSettings {
return SetterMethodDetector.INSTANCE;
}
@Override
public Instantiator getDslInstantiator() {
return ReflectionDslInstantiator.INSTANCE;
}
@Override
public Instantiator getPojoInstantiator() {
return ReflectionPojoInstantiator.INSTANCE;
}
}

View file

@ -32,7 +32,7 @@ import com.datastax.driver.core.Session;
public final class Casser {
private static volatile CasserSettings casserSettings = new DefaultCasserSettings();
private static volatile CasserSettings settings = new DefaultCasserSettings();
private static final ConcurrentMap<Class<?>, Object> dslCache = new ConcurrentHashMap<Class<?>, Object>();
@ -40,12 +40,12 @@ public final class Casser {
}
public static CasserSettings settings() {
return casserSettings;
return settings;
}
public static CasserSettings configure(CasserSettings overrideSettings) {
CasserSettings old = casserSettings;
casserSettings = overrideSettings;
CasserSettings old = settings;
settings = overrideSettings;
return old;
}
@ -72,18 +72,13 @@ public final class Casser {
return dsl(iface, iface.getClassLoader());
}
@SuppressWarnings("unchecked")
public static <E> E dsl(Class<E> iface, ClassLoader classLoader) {
Object instance = dslCache.get(iface);
if (instance == null) {
DslInvocationHandler<E> handler = new DslInvocationHandler<E>(iface);
instance = Proxy.newProxyInstance(
classLoader,
new Class[] { iface },
handler);
instance = settings.getDslInstantiator().instantiate(iface, classLoader);
Object c = dslCache.putIfAbsent(iface, instance);
if (c != null) {
@ -99,14 +94,8 @@ public final class Casser {
return pojo(iface, iface.getClassLoader());
}
@SuppressWarnings("unchecked")
public static <E> E pojo(Class<E> iface, ClassLoader classLoader) {
PojoInvocationHandler<E> handler = new PojoInvocationHandler<E>(iface);
E proxy = (E) Proxy.newProxyInstance(
classLoader,
new Class[] { iface, UDTValueWritable.class },
handler);
return proxy;
return settings.getPojoInstantiator().instantiate(iface, classLoader);
}
}

View file

@ -0,0 +1,22 @@
/*
* Copyright (C) 2015 Noorq, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package casser.core;
public interface Instantiator {
<E> E instantiate(Class<E> iface, ClassLoader classLoader);
}

View file

@ -0,0 +1,39 @@
/*
* Copyright (C) 2015 Noorq, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package casser.core.reflect;
import java.lang.reflect.Proxy;
import casser.core.Instantiator;
public enum ReflectionDslInstantiator implements Instantiator {
INSTANCE;
@Override
@SuppressWarnings("unchecked")
public <E> E instantiate(Class<E> iface, ClassLoader classLoader) {
DslInvocationHandler<E> handler = new DslInvocationHandler<E>(iface);
E proxy = (E) Proxy.newProxyInstance(
classLoader,
new Class[] { iface },
handler);
return proxy;
}
}

View file

@ -0,0 +1,38 @@
/*
* Copyright (C) 2015 Noorq, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package casser.core.reflect;
import java.lang.reflect.Proxy;
import casser.core.Instantiator;
import casser.mapping.convert.UDTValueWritable;
public enum ReflectionPojoInstantiator implements Instantiator {
INSTANCE;
@Override
@SuppressWarnings("unchecked")
public <E> E instantiate(Class<E> iface, ClassLoader classLoader) {
PojoInvocationHandler<E> handler = new PojoInvocationHandler<E>(iface);
E proxy = (E) Proxy.newProxyInstance(
classLoader,
new Class[] { iface, UDTValueWritable.class },
handler);
return proxy;
}
}