Skip to content

Commit

Permalink
Add possibility to use library without Spring framework
Browse files Browse the repository at this point in the history
  • Loading branch information
vbauer committed Feb 26, 2015
1 parent 8a00f83 commit 259f51a
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 28 deletions.
32 changes: 32 additions & 0 deletions src/main/java/com/github/vbauer/herald/util/CollectionUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.github.vbauer.herald.util;

import java.util.Collection;

/**
* @author Vladislav Bauer
*/

public final class CollectionUtils {

private CollectionUtils() {
throw new UnsupportedOperationException();
}


public static boolean isEmpty(final Collection entries) {
return size(entries) == 0;
}

public static boolean isEmpty(final Object... entries) {
return size(entries) == 0;
}

public static int size(final Collection entries) {
return entries == null ? 0 : entries.size();
}

public static int size(final Object... entries) {
return entries == null ? 0 : entries.length;
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.github.vbauer.herald.util;

import com.github.vbauer.herald.logger.LogFactory;
import org.springframework.util.CollectionUtils;

import java.util.Collection;

Expand Down
31 changes: 11 additions & 20 deletions src/main/java/com/github/vbauer/herald/util/LoggerInjector.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@
import com.github.vbauer.herald.annotation.Log;
import com.github.vbauer.herald.exception.MissedLogFactoryException;
import com.github.vbauer.herald.logger.LogFactory;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.ReflectionUtils.FieldCallback;
import org.springframework.util.ReflectionUtils.FieldFilter;
import org.springframework.util.StringUtils;

import java.lang.reflect.Field;
import java.util.Collection;
Expand Down Expand Up @@ -34,21 +30,14 @@ public static <T> T inject(final T bean) {
final Class<?> beanClass = bean.getClass();
final Collection<LogFactory> logFactories = ServiceLoaderUtils.load(LogFactory.class);

ReflectionUtils.doWithFields(
beanClass,
new FieldCallback() {
@Override
public void doWith(final Field field) throws IllegalAccessException {
final Field[] declaredFields = beanClass.getDeclaredFields();
if (!CollectionUtils.isEmpty(declaredFields)) {
for (final Field field : declaredFields) {
if (needToInjectLogger(bean, field, logFactories)) {
injectLogger(bean, field, logFactories);
}
},
new FieldFilter() {
@Override
public boolean matches(final Field field) {
return needToInjectLogger(bean, field, logFactories);
}
}
);
}

return bean;
}
Expand Down Expand Up @@ -77,9 +66,9 @@ private static boolean needToInjectLogger(

private static void injectLogger(
final Object bean, final Field field, final Collection<LogFactory> logFactories
) throws IllegalAccessException {
) {
final boolean isAccessible = field.isAccessible();
ReflectionUtils.makeAccessible(field);
field.setAccessible(true);

try {
final Class<?> beanClass = bean.getClass();
Expand All @@ -94,6 +83,8 @@ private static void injectLogger(

final Object logger = createLogger(logFactory, loggerName, beanClass);
field.set(bean, logger);
} catch (final IllegalAccessException ex) {
ReflectionUtils.handleReflectionException(ex);
} finally {
field.setAccessible(isAccessible);
}
Expand All @@ -105,8 +96,8 @@ private static Log getAnnotation(final Class<?> beanClass, final Field field) {
}

private static Object createLogger(final LogFactory logFactory, final String loggerName, final Class<?> beanClass) {
final String name = StringUtils.trimWhitespace(loggerName);
if (!StringUtils.isEmpty(name)) {
final String name = loggerName == null ? "" : loggerName.trim();
if (!name.isEmpty()) {
return logFactory.createLogger(name);
} else {
return logFactory.createLogger(beanClass);
Expand Down
21 changes: 14 additions & 7 deletions src/main/java/com/github/vbauer/herald/util/ReflectionUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ private ReflectionUtils() {
}


public static boolean isAssignableFrom(final String clazz, final Class<?> from) {
public static boolean isAssignableFrom(final String className, final Class<?> from) {
try {
final Class<?> compatibleLoggerClass = Class.forName(clazz);
return compatibleLoggerClass.isAssignableFrom(from);
final Class<?> clazz = Class.forName(className);
return clazz.isAssignableFrom(from);
} catch (final Exception ex) {
return false;
}
Expand All @@ -26,13 +26,20 @@ public static boolean isAssignableFrom(final String clazz, final Class<?> from)
public static <I, O> O invokeStatic(final String className, final String methodName, final I argument) {
try {
final Class<?> clazz = Class.forName(className);
final Method method = clazz.getDeclaredMethod(methodName, argument.getClass());
final Class<?> argumentClass = argument.getClass();

return (O) org.springframework.util.ReflectionUtils.invokeMethod(method, null, argument);
final Method method = clazz.getDeclaredMethod(methodName, argumentClass);
return (O) method.invoke(null, argument);
} catch (final Exception ex) {
org.springframework.util.ReflectionUtils.handleReflectionException(ex);
return null;
return handleReflectionException(ex);
}
}

public static <T> T handleReflectionException(final Throwable ex) {
if (ex instanceof RuntimeException) {
throw (RuntimeException) ex;
}
throw new RuntimeException(ex);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.github.vbauer.herald.util;

import com.github.vbauer.herald.core.BasicTest;
import org.junit.Assert;
import org.junit.Test;

import java.util.Collection;
import java.util.Collections;

/**
* @author Vladislav Bauer
*/

public class CollectionUtilsTest extends BasicTest {

@Test
public void testConstructorContract() throws Exception {
checkUtilConstructorContract(CollectionUtils.class);
}

@Test
public void testSizeArray() {
Assert.assertEquals(0, CollectionUtils.size((Integer[]) null));
Assert.assertEquals(0, CollectionUtils.size(new Integer[]{}));
Assert.assertEquals(1, CollectionUtils.size(new Integer[]{1}));
}

@Test
public void testSizeCollection() {
Assert.assertEquals(0, CollectionUtils.size((Collection) null));
Assert.assertEquals(0, CollectionUtils.size(Collections.emptyList()));
Assert.assertEquals(0, CollectionUtils.size(Collections.emptySet()));
Assert.assertEquals(1, CollectionUtils.size(Collections.singleton(1)));
Assert.assertEquals(1, CollectionUtils.size(Collections.singletonList(1)));
}

@Test
public void testIsEmptyArray() {
Assert.assertTrue(CollectionUtils.isEmpty((Object[]) null));
Assert.assertTrue(CollectionUtils.isEmpty());
Assert.assertFalse(CollectionUtils.isEmpty(new Object()));
Assert.assertFalse(CollectionUtils.isEmpty(new Object(), new Object()));
}

@Test
public void testIsEmptyCollection() {
Assert.assertTrue(CollectionUtils.isEmpty((Collection) null));
Assert.assertTrue(CollectionUtils.isEmpty(Collections.emptyList()));
Assert.assertTrue(CollectionUtils.isEmpty(Collections.emptySet()));
Assert.assertFalse(CollectionUtils.isEmpty(Collections.singleton(1)));
Assert.assertFalse(CollectionUtils.isEmpty(Collections.singletonList(1)));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,14 @@ public void testInvokeStatic() {
ReflectionUtils.invokeStatic(null, null, null);
}

@Test(expected = RuntimeException.class)
public void testHandleReflectionException() {
ReflectionUtils.handleReflectionException(new Exception());
}

@Test(expected = RuntimeException.class)
public void testHandleReflectionRuntimeException() {
ReflectionUtils.handleReflectionException(new RuntimeException());
}

}

0 comments on commit 259f51a

Please sign in to comment.