ItEr24S08CUAsignacionGrupoRecursosAPlanificacionItEr23S10: Moving scripts dependency extraction to ScriptDependenciesSorter
This commit is contained in:
parent
1e4d68383c
commit
3e360a5373
3 changed files with 62 additions and 71 deletions
|
|
@ -1,7 +1,10 @@
|
|||
package org.zkoss.ganttz.util.script;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
|
|
@ -49,4 +52,59 @@ public class ScriptDependenciesSorter {
|
|||
return result;
|
||||
}
|
||||
|
||||
public static List<ScriptDependency> extractFrom(Class<?> classWithScripts) {
|
||||
ScriptsRequiredDeclaration annotation = classWithScripts
|
||||
.getAnnotation(ScriptsRequiredDeclaration.class);
|
||||
if (annotation == null)
|
||||
throw new IllegalArgumentException(classWithScripts
|
||||
+ " must be annotated with "
|
||||
+ ScriptsRequiredDeclaration.class.getName());
|
||||
List<ScriptDependency> dependsOn = getDependencies(annotation);
|
||||
List<ScriptDependency> result = new ArrayList<ScriptDependency>();
|
||||
for (Field field : getStringFields(getStaticFields(classWithScripts
|
||||
.getFields()))) {
|
||||
result.add(new ScriptDependency(getValueFromStringField(field),
|
||||
dependsOn));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
static ArrayList<ScriptDependency> getDependencies(
|
||||
ScriptsRequiredDeclaration declaration) {
|
||||
Class<?>[] dependsOn = declaration.dependsOn();
|
||||
ArrayList<ScriptDependency> result = new ArrayList<ScriptDependency>();
|
||||
for (Class<?> klass : dependsOn) {
|
||||
result.addAll(extractFrom(klass));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
static String getValueFromStringField(Field stringField) {
|
||||
try {
|
||||
return (String) stringField.get(null);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private static List<Field> getStaticFields(Field[] fields) {
|
||||
List<Field> result = new ArrayList<Field>();
|
||||
for (Field field : fields) {
|
||||
if (Modifier.isStatic(field.getModifiers())) {
|
||||
result.add(field);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
static List<Field> getStringFields(Collection<Field> fields) {
|
||||
List<Field> stringFields = new ArrayList<Field>();
|
||||
for (Field field : fields) {
|
||||
if (field.getType().equals(String.class)) {
|
||||
stringFields.add(field);
|
||||
}
|
||||
}
|
||||
return stringFields;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,67 +0,0 @@
|
|||
package org.zkoss.ganttz.util.script;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
public class ScriptExtractor {
|
||||
|
||||
public static List<ScriptDependency> extractFrom(Class<?> classWithScripts) {
|
||||
ScriptsRequiredDeclaration annotation = classWithScripts
|
||||
.getAnnotation(ScriptsRequiredDeclaration.class);
|
||||
if (annotation == null)
|
||||
throw new IllegalArgumentException(classWithScripts
|
||||
+ " must be annotated with "
|
||||
+ ScriptsRequiredDeclaration.class.getName());
|
||||
List<ScriptDependency> dependsOn = getDependencies(annotation);
|
||||
List<ScriptDependency> result = new ArrayList<ScriptDependency>();
|
||||
for (Field field : getStringFields(getStaticFields(classWithScripts
|
||||
.getFields()))) {
|
||||
result.add(new ScriptDependency(getValueFromStringField(field),
|
||||
dependsOn));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private static ArrayList<ScriptDependency> getDependencies(
|
||||
ScriptsRequiredDeclaration declaration) {
|
||||
Class<?>[] dependsOn = declaration.dependsOn();
|
||||
ArrayList<ScriptDependency> result = new ArrayList<ScriptDependency>();
|
||||
for (Class<?> klass : dependsOn) {
|
||||
result.addAll(extractFrom(klass));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
private static String getValueFromStringField(Field stringField) {
|
||||
try {
|
||||
return (String) stringField.get(null);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private static List<Field> getStaticFields(Field[] fields) {
|
||||
List<Field> result = new ArrayList<Field>();
|
||||
for (Field field : fields) {
|
||||
if (Modifier.isStatic(field.getModifiers())) {
|
||||
result.add(field);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private static List<Field> getStringFields(Collection<Field> fields) {
|
||||
List<Field> stringFields = new ArrayList<Field>();
|
||||
for (Field field : fields) {
|
||||
if (field.getType().equals(String.class)) {
|
||||
stringFields.add(field);
|
||||
}
|
||||
}
|
||||
return stringFields;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -15,16 +15,16 @@ import org.hamcrest.Description;
|
|||
import org.hamcrest.Matcher;
|
||||
import org.junit.Test;
|
||||
|
||||
public class ScriptExtractorTest {
|
||||
public class ScriptExtractionTest {
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void aClassWithoutScriptRequiredAnnotationIsNotIncluded() {
|
||||
ScriptExtractor.extractFrom(String.class);
|
||||
ScriptDependenciesSorter.extractFrom(String.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onlyPublicStringFieldsAreIncluded() {
|
||||
List<ScriptDependency> scripts = ScriptExtractor
|
||||
List<ScriptDependency> scripts = ScriptDependenciesSorter
|
||||
.extractFrom(ScriptsDeclarationsExample.class);
|
||||
assertThat(scripts.size(), equalTo(2));
|
||||
assertThat(scripts,
|
||||
|
|
@ -33,7 +33,7 @@ public class ScriptExtractorTest {
|
|||
|
||||
@Test
|
||||
public void testIncludesDependencies() {
|
||||
List<ScriptDependency> scripts = ScriptExtractor
|
||||
List<ScriptDependency> scripts = ScriptDependenciesSorter
|
||||
.extractFrom(ScriptsDeclarationsExample.class);
|
||||
assertThat(scripts, each(withDependencies(ScriptIncludedExample.base,
|
||||
ScriptIncludedExample.other)));
|
||||
Loading…
Add table
Reference in a new issue