From 99cd23fbcbdc8010f63074538348c39c3dbfe902 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Francesco=20Chicchiricc=C3=B2?= <--global> Date: Wed, 14 May 2014 13:43:06 +0200 Subject: [PATCH] [OLINGO-261] Simplifying enum handling --- .../olingo/ext/proxy/utils/CoreUtils.java | 38 ++++++------------- .../ext/pojogen/AbstractPOJOGenMojo.java | 16 ++------ fit/src/test/resources/META-INF/enumTypes | 3 -- 3 files changed, 15 insertions(+), 42 deletions(-) delete mode 100644 fit/src/test/resources/META-INF/enumTypes diff --git a/ext/client-proxy/src/main/java/org/apache/olingo/ext/proxy/utils/CoreUtils.java b/ext/client-proxy/src/main/java/org/apache/olingo/ext/proxy/utils/CoreUtils.java index 2601db3f9..e8fa91b3c 100644 --- a/ext/client-proxy/src/main/java/org/apache/olingo/ext/proxy/utils/CoreUtils.java +++ b/ext/client-proxy/src/main/java/org/apache/olingo/ext/proxy/utils/CoreUtils.java @@ -34,8 +34,6 @@ import java.util.Collection; import java.util.Iterator; import java.util.List; import java.util.Map; -import org.apache.commons.io.IOUtils; -import org.apache.commons.lang3.StringUtils; import org.apache.olingo.client.api.CommonEdmEnabledODataClient; import org.apache.olingo.client.api.v3.UnsupportedInV3Exception; import org.apache.olingo.commons.api.domain.CommonODataEntity; @@ -240,6 +238,17 @@ public final class CoreUtils { } } + @SuppressWarnings({"unchecked", "rawtypes"}) + private static Enum enumValueToObject(final ODataEnumValue value, final Class reference) { + final Namespace namespace = reference.getAnnotation(Namespace.class); + final EnumType enumType = reference.getAnnotation(EnumType.class); + if (value.getTypeName().equals(namespace.value() + "." + enumType.name())) { + return Enum.valueOf((Class) reference, value.getValue()); + } + + return null; + } + private static Object primitiveValueToObject(final ODataPrimitiveValue value, final Class reference) { Object obj; @@ -396,29 +405,6 @@ public final class CoreUtils { } } - @SuppressWarnings({"unchecked", "rawtypes"}) - private static Enum buildEnumInstance(final ODataEnumValue value) { - try { - for (String enumTypeName - : StringUtils.split(IOUtils.toString(CoreUtils.class.getResourceAsStream("/META-INF/enumTypes")), '\n')) { - - final Class enumClass = - (Class) Thread.currentThread().getContextClassLoader().loadClass(enumTypeName); - if (enumClass != null) { - final Namespace namespace = enumClass.getAnnotation(Namespace.class); - final EnumType enumType = enumClass.getAnnotation(EnumType.class); - if (value.getTypeName().equals(namespace.value() + "." + enumType.name())) { - return Enum.valueOf(enumClass, value.getValue()); - } - } - } - } catch (Exception e) { - LOG.error("While trying to load enum for {}", value, e); - } - - return null; - } - @SuppressWarnings("unchecked") public static Object getValueFromProperty( final CommonEdmEnabledODataClient client, @@ -469,7 +455,7 @@ public final class CoreUtils { res = collection; } else if (property instanceof ODataProperty && ((ODataProperty) property).hasEnumValue()) { - res = buildEnumInstance(((ODataProperty) property).getEnumValue()); + res = enumValueToObject(((ODataProperty) property).getEnumValue(), internalRef); } else { res = primitiveValueToObject(property.getPrimitiveValue(), internalRef); } diff --git a/ext/pojogen-maven-plugin/src/main/java/org/apache/olingo/ext/pojogen/AbstractPOJOGenMojo.java b/ext/pojogen-maven-plugin/src/main/java/org/apache/olingo/ext/pojogen/AbstractPOJOGenMojo.java index ce566ee7a..250cf5367 100644 --- a/ext/pojogen-maven-plugin/src/main/java/org/apache/olingo/ext/pojogen/AbstractPOJOGenMojo.java +++ b/ext/pojogen-maven-plugin/src/main/java/org/apache/olingo/ext/pojogen/AbstractPOJOGenMojo.java @@ -84,8 +84,9 @@ public abstract class AbstractPOJOGenMojo extends AbstractMojo { protected abstract String getVersion(); - protected File mkdir(final String path) { - final File dir = new File(outputDirectory + File.separator + TOOL_DIR + File.separator + path); + protected File mkPkgDir(final String path) { + final File dir = new File(outputDirectory + File.separator + TOOL_DIR + File.separator + + basePackage.replace('.', File.separatorChar) + File.separator + path); if (dir.exists()) { if (!dir.isDirectory()) { @@ -98,10 +99,6 @@ public abstract class AbstractPOJOGenMojo extends AbstractMojo { return dir; } - protected File mkPkgDir(final String path) { - return mkdir(basePackage.replace('.', File.separatorChar) + File.separator + path); - } - protected void writeFile(final String name, final File path, final VelocityContext ctx, final Template template, final boolean append) throws MojoExecutionException { @@ -227,8 +224,6 @@ public abstract class AbstractPOJOGenMojo extends AbstractMojo { namespaces.add(schema.getNamespace().toLowerCase()); } - final StringBuilder enumTypeNames = new StringBuilder(); - for (EdmSchema schema : edm.getSchemas()) { createUtility(edm, schema, basePackage); @@ -248,7 +243,6 @@ public abstract class AbstractPOJOGenMojo extends AbstractMojo { // write types into types package for (EdmEnumType enumType : schema.getEnumTypes()) { final String className = utility.capitalize(enumType.getName()); - enumTypeNames.append(typesPkg).append('.').append(className).append('\n'); objs.clear(); objs.put("enumType", enumType); parseObj(typesBaseDir, typesPkg, "enumType", className + ".java", objs); @@ -324,9 +318,6 @@ public abstract class AbstractPOJOGenMojo extends AbstractMojo { } } } - - final File metaInf = mkdir("META-INF"); - FileUtils.fileWrite(metaInf.getPath() + File.separator + "enumTypes", enumTypeNames.toString()); } catch (Exception t) { getLog().error(t); @@ -335,5 +326,4 @@ public abstract class AbstractPOJOGenMojo extends AbstractMojo { : new MojoExecutionException("While executin mojo", t); } } - } diff --git a/fit/src/test/resources/META-INF/enumTypes b/fit/src/test/resources/META-INF/enumTypes deleted file mode 100644 index c1e536523..000000000 --- a/fit/src/test/resources/META-INF/enumTypes +++ /dev/null @@ -1,3 +0,0 @@ -org.apache.olingo.fit.proxy.v4.staticservice.microsoft.test.odata.services.odatawcfservice.types.AccessLevel -org.apache.olingo.fit.proxy.v4.staticservice.microsoft.test.odata.services.odatawcfservice.types.Color -org.apache.olingo.fit.proxy.v4.staticservice.microsoft.test.odata.services.odatawcfservice.types.CompanyCategory