Skip to content

Commit

Permalink
reverted deletion of base package
Browse files Browse the repository at this point in the history
  • Loading branch information
g3force committed Oct 28, 2015
1 parent 2598e3f commit 22ed31f
Showing 1 changed file with 37 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,22 +96,32 @@ public HierarchicalConfiguration getDefaultConfig(final Set<Class<?>> classes, f
classesAndSubClasses.addAll(getAllSubClasses(clazz));
}

String basePackage = classesAndSubClasses.iterator().next().getPackage().getName();
Map<Class<?>, List<ConfigurableFieldData>> classesAndSubClassesMap = new LinkedHashMap<Class<?>, List<ConfigurableFieldData>>();
for (Class<?> clazz : classesAndSubClasses)
{
List<ConfigurableFieldData> dataRead = read(clazz);
if (!dataRead.isEmpty())
{
classesAndSubClassesMap.put(clazz, dataRead);

String packageName = clazz.getPackage().getName();
basePackage = greatestCommonPrefix(basePackage, packageName);
}
}

config.getRoot().setName(name);
config.getRoot().addAttribute(new HierarchicalConfiguration.Node("base", basePackage));
for (Map.Entry<Class<?>, List<ConfigurableFieldData>> entry : classesAndSubClassesMap.entrySet())
{
Class<?> clazz = entry.getKey();
String clazzName = clazz.getName();
String clazzKey = clazzName;
if (!clazzName.startsWith(basePackage))
{
log.error("Invalid class: " + clazzName + ". Must start with: " + basePackage);
continue;
}
String clazzKey = clazzName.substring(basePackage.length() + 1);

List<ConfigurableFieldData> dataRead = entry.getValue();
for (ConfigurableFieldData fieldData : dataRead)
Expand All @@ -131,6 +141,28 @@ public HierarchicalConfiguration getDefaultConfig(final Set<Class<?>> classes, f
}


private String greatestCommonPrefix(final String a, final String b)
{
String[] pkgsA = a.split("\\.");
String[] pkgsB = b.split("\\.");
int minLength = Math.min(pkgsA.length, pkgsB.length);
StringBuilder prefix = new StringBuilder();
for (int i = 0; i < minLength; i++)
{
if (!pkgsA[i].equals(pkgsB[i]))
{
return prefix.toString();
}
if (i != 0)
{
prefix.append('.');
}
prefix.append(pkgsA[i]);
}
return prefix.toString();
}


private Map<String, ConfigurationNode> getClassNodesFromConfigRec(final String basePackage,
final List<ConfigurationNode> nodes)
{
Expand Down Expand Up @@ -163,11 +195,12 @@ public void loadConfiguration(final HierarchicalConfiguration config)
data.clear();

List<ConfigurationNode> attrs = config.getRoot().getAttributes("base");
String base = "";
if (attrs.size() == 1)
if (attrs.size() != 1)
{
base = attrs.get(0).getValue().toString();
log.error("No unique base package path found: " + attrs);
return;
}
String base = attrs.get(0).getValue().toString();

List<ConfigurationNode> classNodes = config.getRoot().getChildren();

Expand Down

0 comments on commit 22ed31f

Please sign in to comment.