Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix issue 110 #111

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public interface EzySingletonFactory {

Object getSingleton(String name, Class type);

Object getSingleton(EzyBeanKey key);

Object getSingleton(Map properties);

Object getAnnotatedSingleton(Class annotationClass);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ public int hashCode() {

@Override
public String toString() {
return "(" + name + "," + type.getSimpleName() + ")";
return "(" + name + "," + type.getName() + ")";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@ public EzyByFieldSingletonLoader(
Object configurator,
Map<Class<?>, EzyMethod> methodsByType
) {
this(beanName, field, configurator, methodsByType, new ArrayList<>());
this(
beanName,
field,
configurator,
methodsByType,
new ArrayList<>()
);
}

public EzyByFieldSingletonLoader(
Expand All @@ -33,7 +39,8 @@ public EzyByFieldSingletonLoader(
Map<Class<?>, EzyMethod> methodsByType,
List<Class<?>> stackCallClasses
) {
super(beanName,
super(
beanName,
new EzyClass(field.getType()),
configurator,
methodsByType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,13 @@ public EzyByMethodSingletonLoader(
Object configurator,
Map<Class<?>, EzyMethod> methodsByType
) {
this(beanName, method, configurator, methodsByType, new ArrayList<>());
this(
beanName,
method,
configurator,
methodsByType,
new ArrayList<>()
);
}

public EzyByMethodSingletonLoader(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.tvd12.ezyfox.bean.impl;

import com.tvd12.ezyfox.bean.EzyBeanContext;
import com.tvd12.ezyfox.reflect.EzyField;
import com.tvd12.ezyfox.reflect.EzyMethod;

import java.util.Map;

public class EzyConfigurationFieldSingletonLoader
extends EzyByFieldSingletonLoader {

public EzyConfigurationFieldSingletonLoader(
String beanName,
EzyField field,
Object configurator,
Map<Class<?>, EzyMethod> methodsByType
) {
super(
beanName,
field,
configurator,
methodsByType
);
}

@SuppressWarnings("rawtypes")
@Override
protected Object getOrCreateSingleton(
EzyBeanContext context,
String name,
Class[] parameterTypes
) {
Object singleton = newSingletonByConstructor(
context,
parameterTypes
);
logger.debug(
"add singleton with name {} of {}, object = {}",
name,
singleton.getClass(),
singleton
);
return singleton;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.tvd12.ezyfox.bean.impl;

import com.tvd12.ezyfox.bean.EzyBeanContext;
import com.tvd12.ezyfox.reflect.EzyMethod;

import java.util.Map;

public class EzyConfigurationMethodSingletonLoader
extends EzyByMethodSingletonLoader {

public EzyConfigurationMethodSingletonLoader(
String beanName,
EzyMethod method,
Object configurator,
Map<Class<?>, EzyMethod> methodsByType
) {
super(
beanName,
method,
configurator,
methodsByType
);
}

@SuppressWarnings("rawtypes")
@Override
protected Object getOrCreateSingleton(
EzyBeanContext context,
String name,
Class[] parameterTypes
) {
Object singleton = newSingletonByConstructor(
context,
parameterTypes
);
logger.debug(
"add singleton with name {} of {}, object = {}",
name,
singleton.getClass(),
singleton
);
return singleton;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import static com.tvd12.ezyfox.bean.impl.EzyBeanNameParser.getPrototypeName;
import static com.tvd12.ezyfox.bean.impl.EzyBeanNameParser.getSingletonName;
import static com.tvd12.ezyfox.io.EzyMaps.newHashMapNewKeys;

public class EzySimpleConfigurationLoader
extends EzyLoggable
Expand All @@ -27,11 +28,16 @@ public class EzySimpleConfigurationLoader
protected EzySingletonFactory singletonFactory;
protected EzyBeanNameTranslator beanNameTranslator;
protected Map<Class<?>, EzyMethod> singletonMethods;
protected Map<EzyBeanKey, EzyMethod> singletonMethodByKey;

@Override
public EzyConfigurationLoader clazz(Class<?> configClass) {
this.clazz = new EzyClass(configClass);
this.singletonMethods = mapSingletonTypeMethods();
this.singletonMethodByKey = mapSingletonKeyMethods();
this.singletonMethods = newHashMapNewKeys(
singletonMethodByKey,
EzyBeanKey::getType
);
return this;
}

Expand Down Expand Up @@ -88,29 +94,38 @@ private void addSingletonByFields(Object configurator) {

private void addSingletonByField(EzyField field, Object configurator) {
String beanName = getSingletonName(field);
Object current = singletonFactory.getSingleton(beanName, field.getType());
Object current = singletonFactory.getSingleton(
EzyBeanKey.of(beanName, field.getType())
);
if (current == null) {
EzySingletonLoader loader = new EzyByFieldSingletonLoader(beanName, field, configurator, singletonMethods);
EzySingletonLoader loader = new EzyConfigurationFieldSingletonLoader(
beanName,
field,
configurator,
singletonMethods
);
loader.load(context);
}
}

private void addSingletonByMethods(Object configurator) {
Set<Class<?>> types = new HashSet<>(singletonMethods.keySet());
for (Class<?> type : types) {
EzyMethod method = singletonMethods.remove(type);
Set<EzyBeanKey> keys = new HashSet<>(singletonMethodByKey.keySet());
for (EzyBeanKey key : keys) {
EzyMethod method = singletonMethodByKey.remove(key);
if (method != null) {
logger.debug("add singleton of {} with method {}", type, method);
logger.debug("add singleton of {} with method {}", key, method);
addSingletonByMethod(method, configurator);
}
}
}

private void addSingletonByMethod(EzyMethod method, Object configurator) {
String beanName = getSingletonName(method);
Object current = singletonFactory.getSingleton(beanName, method.getReturnType());
Object current = singletonFactory.getSingleton(
EzyBeanKey.of(beanName, method.getReturnType())
);
if (current == null) {
EzySingletonLoader loader = new EzyByMethodSingletonLoader(
EzySingletonLoader loader = new EzyConfigurationMethodSingletonLoader(
beanName,
method,
configurator,
Expand All @@ -135,10 +150,12 @@ private void addPrototypeByField(EzyField field, Object configurator) {
}

private void addPrototypeByMethods(Object configurator) {
Map<Class<?>, EzyMethod> methods = mapPrototypeTypeMethods();
Set<Class<?>> types = new HashSet<>(methods.keySet());
for (Class<?> type : types) {
addPrototypeByMethod(methods.remove(type), configurator);
Map<EzyBeanKey, EzyMethod> methods = mapPrototypeKeyMethods();
Set<EzyBeanKey> keys = new HashSet<>(methods.keySet());
for (EzyBeanKey key : keys) {
EzyMethod method = methods.remove(key);
logger.debug("add prototype of {} with method {}", key, method);
addPrototypeByMethod(method, configurator);
}
}

Expand All @@ -160,27 +177,32 @@ private List<EzyField> getSingletonFields() {
return getBeanFields(EzySingleton.class);
}

private Map<Class<?>, EzyMethod> mapSingletonTypeMethods() {
return mapBeanTypeMethods(EzySingleton.class);
private Map<EzyBeanKey, EzyMethod> mapSingletonKeyMethods() {
return mapBeanKeyMethods(EzySingleton.class);
}

private List<EzyField> getPrototypeFields() {
return getBeanFields(EzyPrototype.class);
}

private Map<Class<?>, EzyMethod> mapPrototypeTypeMethods() {
return mapBeanTypeMethods(EzyPrototype.class);
private Map<EzyBeanKey, EzyMethod> mapPrototypeKeyMethods() {
return mapBeanKeyMethods(EzyPrototype.class);
}

private List<EzyField> getBeanFields(Class<? extends Annotation> annClass) {
return clazz.getPublicFields(f -> f.isAnnotated(annClass));
}

@SuppressWarnings("unchecked")
private Map<Class<?>, EzyMethod> mapBeanTypeMethods(Class<? extends Annotation> annClass) {
private Map<EzyBeanKey, EzyMethod> mapBeanKeyMethods(Class<? extends Annotation> annClass) {
List<EzyMethod> methods = clazz.getPublicMethods(m ->
m.isAnnotated(annClass) && m.getReturnType() != void.class
);
return EzyMaps.newHashMap(methods, EzyMethod::getReturnType);
return EzyMaps.newHashMap(
methods,
it -> EzyBeanKey.of(
getSingletonName(it),
it.getReturnType()
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,11 @@ public Object getSingleton(String name, Class type) {
return singleton;
}

@Override
public Object getSingleton(EzyBeanKey key) {
return singletonByKey.get(key);
}

@Override
public Object getSingleton(Map properties) {
for (Entry<Object, Map> entry : propertiesBySingleton.entrySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ private Object process(EzyBeanContext context) throws Exception {
return answer;
}

private Object getOrCreateSingleton(
protected Object getOrCreateSingleton(
EzyBeanContext context,
String name,
Class[] parameterTypes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,25 @@
import com.tvd12.ezyfox.bean.impl.EzyBeanKey;
import com.tvd12.test.assertion.Asserts;
import com.tvd12.test.base.BaseTest;
import com.tvd12.test.performance.Performance;
import org.testng.annotations.Test;

public class EzyBeanKeyTest extends BaseTest {

public static void main(String[] args) {
EzyBeanKey hello = new EzyBeanKey("hello", String.class);
EzyBeanKey world = new EzyBeanKey("world", String.class);
long equalsTime = Performance.create()
.test(() -> hello.equals(world))
.getTime();
System.out.println("equalsTime: " + equalsTime);

long hashCodeTime = Performance.create()
.test(hello::hashCode)
.getTime();
System.out.println("hashCodeTime: " + hashCodeTime);
}

@Test
public void test() {
// given
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ public void test() throws Exception {
EzyMethod methodB = new EzyMethod(getClass().getDeclaredMethod("newB"));
EzyByMethodSingletonLoader loader = new EzyByMethodSingletonLoader(
"a",
methodA, this, EzyMaps.newHashMap(B.class, methodB));
methodA,
this,
EzyMaps.newHashMap(B.class, methodB));

Method getConstructorParameterTypes = EzyByMethodSingletonLoader.class
.getDeclaredMethod("getConstructorParameterTypes", Class.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.tvd12.ezyfox.bean.v129.testing;

import com.tvd12.ezyfox.bean.EzyBeanContext;
import com.tvd12.test.assertion.Asserts;
import org.testng.annotations.Test;

public class V129DeclareTwoBeansSameTypeTest {

@Test
public void test() {
// given
EzyBeanContext beanContext = EzyBeanContext.builder()
.scan("com.tvd12.ezyfox.bean.v129.testing.config")
.build();

// when
// then
Asserts.assertEquals(
beanContext.getBean("hello", String.class),
"hello"
);
Asserts.assertEquals(
beanContext.getBean("world", String.class),
"world"
);
Asserts.assertEquals(
beanContext.getBean("foo", String.class),
"foo"
);
Asserts.assertEquals(
beanContext.getBean("bar", String.class),
"bar"
);
}
}
Loading
Loading