Created
December 18, 2017 15:36
-
-
Save Nikola-Andreev/16e58822bbac0525d1c02b82267d145e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.junit.Assert; | |
import org.junit.Test; | |
import java.lang.annotation.Annotation; | |
import java.lang.reflect.Constructor; | |
import java.lang.reflect.Field; | |
import java.lang.reflect.Method; | |
import java.lang.reflect.Modifier; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.Map; | |
public class WastesOne { | |
@Test | |
public void test() throws NoSuchMethodException { | |
Map<String, Class> allClasses = Classes.allClasses; | |
Class<?> waste = allClasses.get("Waste"); | |
Class<Annotation> disposable = allClasses.get("Disposable"); | |
boolean isCorrectInterface = isCorrectInterface(waste); | |
Assert.assertTrue(isCorrectInterface); | |
List<Class> wastes = new ArrayList<>(); | |
for (String name : allClasses.keySet()) { | |
Class<?> current = allClasses.get(name); | |
if (waste.isAssignableFrom(current) && !current.isInterface()) { | |
boolean isEncapsulated = isEncapsulated(current); | |
boolean isDisposable = isDisposable(current, disposable); | |
boolean isDependencyInjected = isDependencyInjected(current); | |
if (isEncapsulated && isDisposable && isDependencyInjected) { | |
wastes.add(current); | |
} | |
} | |
} | |
Assert.assertTrue("One or more wastes not meeting requirements", wastes.size() > 0); | |
} | |
private boolean isCorrectInterface(Class<?> waste) throws NoSuchMethodException { | |
if (waste.getDeclaredMethods().length != 3) { | |
return false; | |
} | |
Method getName = waste.getMethod("getName"); | |
if (! getName.getReturnType().equals(String.class)) { | |
return false; | |
} | |
Method getVolumePerKg = waste.getMethod("getVolumePerKg"); | |
if (! getVolumePerKg.getReturnType().equals(double.class)) { | |
return false; | |
} | |
Method getWeight = waste.getMethod("getWeight"); | |
if (! getWeight.getReturnType().equals(double.class)) { | |
return false; | |
} | |
return true; | |
} | |
private boolean isDisposable(Class<?> current, Class<? extends Annotation> disposable) { | |
Annotation[] annotations = current.getAnnotations(); | |
for (Annotation annotation : annotations) { | |
if (annotation.annotationType().isAnnotationPresent(disposable)) { | |
return true; | |
} | |
} | |
return false; | |
} | |
private boolean isDependencyInjected(Class<?> current) { | |
Constructor<?>[] constructors = current.getDeclaredConstructors(); | |
for (Constructor<?> constructor : constructors) { | |
Class<?>[] parameterTypes = constructor.getParameterTypes(); | |
for (Class<?> parameterType : parameterTypes) { | |
if (! parameterType.getName().startsWith("java.") && ! parameterType.isPrimitive()) { | |
if (! parameterType.isInterface()) { | |
return false; | |
} | |
} | |
} | |
} | |
return true; | |
} | |
private boolean isEncapsulated(Class<?> current) { | |
Field[] fields = current.getDeclaredFields(); | |
for (Field field : fields) { | |
if (! Modifier.isPrivate(field.getModifiers())) { | |
return false; | |
} | |
} | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment