You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@reef.apache.org by yu...@apache.org on 2015/06/22 17:54:08 UTC

[01/17] incubator-reef git commit: Define schema and configure avro string to use String instead of CharSequence

Repository: incubator-reef
Updated Branches:
  refs/heads/REEF-395 [created] a5bfae411


Define schema and configure avro string to use String instead of CharSequence


Project: http://git-wip-us.apache.org/repos/asf/incubator-reef/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-reef/commit/7d4baf0f
Tree: http://git-wip-us.apache.org/repos/asf/incubator-reef/tree/7d4baf0f
Diff: http://git-wip-us.apache.org/repos/asf/incubator-reef/diff/7d4baf0f

Branch: refs/heads/REEF-395
Commit: 7d4baf0f4298ec5de18ac374cbe4a9163efd52a9
Parents: 9964d5d
Author: Yunseong Lee <yu...@apache.org>
Authored: Fri Jun 19 17:22:11 2015 +0900
Committer: Yunseong Lee <yu...@apache.org>
Committed: Fri Jun 19 17:22:11 2015 +0900

----------------------------------------------------------------------
 .../tang/src/main/avro/class_hierarchy.avsc     | 120 +++++++++++++++++++
 pom.xml                                         |   3 +
 2 files changed, 123 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/7d4baf0f/lang/java/reef-tang/tang/src/main/avro/class_hierarchy.avsc
----------------------------------------------------------------------
diff --git a/lang/java/reef-tang/tang/src/main/avro/class_hierarchy.avsc b/lang/java/reef-tang/tang/src/main/avro/class_hierarchy.avsc
new file mode 100644
index 0000000..198c70d
--- /dev/null
+++ b/lang/java/reef-tang/tang/src/main/avro/class_hierarchy.avsc
@@ -0,0 +1,120 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+[
+{
+    "namespace":"org.apache.reef.tang.formats.avro",
+    "type":"record",
+    "name":"AvroConstructorArg",
+    "fields":[
+        {"name":"fullArgClassName", "type":"string"},
+        {"name":"namedParameterName", "type":"string"},
+        {"name":"isInjectionFuture", "type":"boolean"}
+    ]
+},
+{
+    "namespace":"org.apache.reef.tang.formats.avro",
+    "type":"record",
+    "name":"AvroConstructorDef",
+    "fields":[
+        {"name":"fullArgClassName", "type":"string"},
+        {"name":"constructorArg", "type":{"type":"array", "items":"AvroConstructorArg"}}
+    ]
+},
+{
+    "namespace":"org.apache.reef.tang.formats.avro",
+    "type":"record",
+    "name":"AvroNamedParameterNode",
+    "fields":[
+        {"name":"simpleArgClassName", "type":"string"},
+        {"name":"fullArgClassName", "type":"string"},
+        {"name":"isSet", "type":"boolean"},
+        {"name":"isList", "type":"boolean"},
+        {"name":"documentation", "type":["null", "string"]},
+        {"name":"shortName", "type":["null", "string"]},
+        {"name":"instanceDefault", "type":{"type":"array","items":"string"}}
+    ]
+},
+{
+    "namespace":"org.apache.reef.tang.formats.avro",
+    "type":"record",
+    "name":"AvroClassNode",
+    "fields":[
+        {"name":"isInjectionCandidate", "type":"boolean"},
+        {"name":"isExternalConstructor", "type":"boolean"},
+        {"name":"isUnit", "type":"boolean"},
+        {"name":"injectableConstructors", "type":{"type":"array", "items":"AvroConstructorDef"}},
+        {"name":"otherConstructors", "type":{"type":"array", "items":"AvroConstructorDef"}},
+        {"name":"implFullNames", "type":{"type":"array", "items":"string"}},
+        {"name":"defaultImplementation", "type":["null", "string"]}
+    ]
+},
+/*
+    Intentionally left blank. Packages don't have any interesting
+    attributes except their names and children.
+*/
+{
+    "namespace":"org.apache.reef.tang.formats.avro",
+    "type":"record",
+    "name":"AvroPackageNode",
+    "fields":[]
+},
+/*
+  Node is the abstract base type for all the data encoded in a class hierarchy.
+  It is represented as an (un)tagged union, so only one of the three optional
+  fields can be non-null.
+ */
+{
+    "namespace":"org.apache.reef.tang.formats.avro",
+    "type":"record",
+    "name":"AvroNode",
+    "fields":[
+        /*
+          The short name of this node (e.g., a class name without the enclosing
+          package, or a package name without the name of the enclosing package.
+         */
+        {"name":"name", "type":"string"},
+        /*
+          For some languages it can be difficult to map from a list of nodes to
+          the correct short name, so for now, the full name is (redundantly)
+          encoded here.  In Java, the full name is a bunch of short names that
+          have been concatenated with "." and/or "$".
+         */
+        {"name":"fullName", "type":"string"},
+
+        /*
+         * Exactly one of the next three fields must be defined. See below for
+         * their documentation.
+         */
+        {"name":"classNode", "type":["null", "AvroClassNode"]},
+        {"name":"namedParameterNode", "type":["null", "AvroNamedParameterNode"]},
+        {"name":"packageNode", "type":["null", "AvroPackageNode"]},
+
+        /*
+           Class hierarchy nodes are arranged in a tree that mirrors the language
+           namespaces / packages that contain class names (e.g., Java packages,
+           C++ namespaces).
+
+           A NamedParameterNode cannot have any children.  A ClassNode can not have
+           any package nodes as children.  PackageNodes cannot have PackageNodes as
+           children.
+         */
+        {"name":"children", "type":{"type":"array","items":"AvroNode"}}
+    ]
+}
+]

http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/7d4baf0f/pom.xml
----------------------------------------------------------------------
diff --git a/pom.xml b/pom.xml
index 60f50e8..1cbdde3 100644
--- a/pom.xml
+++ b/pom.xml
@@ -211,6 +211,9 @@ under the License.
                     <groupId>org.apache.avro</groupId>
                     <artifactId>avro-maven-plugin</artifactId>
                     <version>${avro.version}</version>
+                    <configuration>
+                      <stringType>String</stringType>
+                    </configuration>
                     <executions>
                         <execution>
                             <phase>generate-sources</phase>


[14/17] incubator-reef git commit: Replace ClassHierarchy serializer and leave comments for the future changes

Posted by yu...@apache.org.
Replace ClassHierarchy serializer and leave comments for the future changes


Project: http://git-wip-us.apache.org/repos/asf/incubator-reef/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-reef/commit/14418120
Tree: http://git-wip-us.apache.org/repos/asf/incubator-reef/tree/14418120
Diff: http://git-wip-us.apache.org/repos/asf/incubator-reef/diff/14418120

Branch: refs/heads/REEF-395
Commit: 144181204eee118d7be0fe1e6af360596c4950ba
Parents: 2328a9d
Author: Yunseong Lee <yu...@apache.org>
Authored: Mon Jun 22 21:20:14 2015 +0900
Committer: Yunseong Lee <yu...@apache.org>
Committed: Mon Jun 22 22:06:44 2015 +0900

----------------------------------------------------------------------
 .../org/apache/reef/javabridge/Utilities.java   |  2 ++
 .../reef/examples/helloCLR/HelloDriver.java     |  2 ++
 .../reef/tang/util/ValidateConfiguration.java   |  2 ++
 .../tang/ClassHierarchyDeserializationTest.java | 24 ++++++++++++++------
 .../apache/reef/tang/test/RoundTripTest.java    |  6 +++--
 5 files changed, 27 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/14418120/lang/java/reef-bridge-java/src/main/java/org/apache/reef/javabridge/Utilities.java
----------------------------------------------------------------------
diff --git a/lang/java/reef-bridge-java/src/main/java/org/apache/reef/javabridge/Utilities.java b/lang/java/reef-bridge-java/src/main/java/org/apache/reef/javabridge/Utilities.java
index 93b6903..88307cf 100644
--- a/lang/java/reef-bridge-java/src/main/java/org/apache/reef/javabridge/Utilities.java
+++ b/lang/java/reef-bridge-java/src/main/java/org/apache/reef/javabridge/Utilities.java
@@ -33,6 +33,7 @@ import java.nio.file.Paths;
 
 public class Utilities {
   public static ClassHierarchy loadClassHierarchy(String classHierarchyFile) {
+    // TODO The file should be created via AvroClassHierarchySerializer
     Path p = Paths.get(classHierarchyFile);
     if (!Files.exists(p)) {
       p = Paths.get(System.getProperty("user.dir") + "/reef/global/" + classHierarchyFile);
@@ -40,6 +41,7 @@ public class Utilities {
     if (!Files.exists(p)) {
       throw new RuntimeException("cannot find file " + p.toAbsolutePath());
     }
+    // TODO Use the AvroClassHierarchy in place of protobuf
     try (final InputStream chin = new FileInputStream(p.toAbsolutePath().toString())) {
       final ClassHierarchyProto.Node root = ClassHierarchyProto.Node.parseFrom(chin);
       final ClassHierarchy ch = new ProtocolBufferClassHierarchy(root);

http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/14418120/lang/java/reef-examples-clr/src/main/java/org/apache/reef/examples/helloCLR/HelloDriver.java
----------------------------------------------------------------------
diff --git a/lang/java/reef-examples-clr/src/main/java/org/apache/reef/examples/helloCLR/HelloDriver.java b/lang/java/reef-examples-clr/src/main/java/org/apache/reef/examples/helloCLR/HelloDriver.java
index 33f60a3..6c64cdc 100644
--- a/lang/java/reef-examples-clr/src/main/java/org/apache/reef/examples/helloCLR/HelloDriver.java
+++ b/lang/java/reef-examples-clr/src/main/java/org/apache/reef/examples/helloCLR/HelloDriver.java
@@ -92,7 +92,9 @@ public final class HelloDriver {
    * @return
    */
   private static ClassHierarchy loadClassHierarchy() {
+    // TODO The file should be created by AvroClassHierarchySerializer
     try (final InputStream chin = new FileInputStream(HelloCLR.CLASS_HIERARCHY_FILENAME)) {
+      // TODO Use AvroClassHierarchySerializer instead
       final ClassHierarchyProto.Node root = ClassHierarchyProto.Node.parseFrom(chin); // A
       final ClassHierarchy ch = new ProtocolBufferClassHierarchy(root);
       return ch;

http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/14418120/lang/java/reef-tang/tang/src/main/java/org/apache/reef/tang/util/ValidateConfiguration.java
----------------------------------------------------------------------
diff --git a/lang/java/reef-tang/tang/src/main/java/org/apache/reef/tang/util/ValidateConfiguration.java b/lang/java/reef-tang/tang/src/main/java/org/apache/reef/tang/util/ValidateConfiguration.java
index 82c929c..31f4b96 100644
--- a/lang/java/reef-tang/tang/src/main/java/org/apache/reef/tang/util/ValidateConfiguration.java
+++ b/lang/java/reef-tang/tang/src/main/java/org/apache/reef/tang/util/ValidateConfiguration.java
@@ -36,6 +36,7 @@ import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 
+// TODO Clean up the code which are deprecated and commented out.
 public class ValidateConfiguration {
   private final String target;
   private final File ch;
@@ -85,6 +86,7 @@ public class ValidateConfiguration {
 
     final Tang t = Tang.Factory.getTang();
 
+    // TODO Use the AvroClassHierarchySerializer
     final ClassHierarchyProto.Node root;
     try (final InputStream chin = new FileInputStream(this.ch)) {
       root = ClassHierarchyProto.Node.parseFrom(chin);

http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/14418120/lang/java/reef-tang/tang/src/test/java/org/apache/reef/tang/ClassHierarchyDeserializationTest.java
----------------------------------------------------------------------
diff --git a/lang/java/reef-tang/tang/src/test/java/org/apache/reef/tang/ClassHierarchyDeserializationTest.java b/lang/java/reef-tang/tang/src/test/java/org/apache/reef/tang/ClassHierarchyDeserializationTest.java
index a7e1469..f0aebaa 100644
--- a/lang/java/reef-tang/tang/src/test/java/org/apache/reef/tang/ClassHierarchyDeserializationTest.java
+++ b/lang/java/reef-tang/tang/src/test/java/org/apache/reef/tang/ClassHierarchyDeserializationTest.java
@@ -18,9 +18,10 @@
  */
 package org.apache.reef.tang;
 
-import org.apache.reef.tang.exceptions.BindException;
 import org.apache.reef.tang.exceptions.NameResolutionException;
+import org.apache.reef.tang.formats.AvroClassHierarchySerializer;
 import org.apache.reef.tang.formats.AvroConfigurationSerializer;
+import org.apache.reef.tang.formats.ClassHierarchySerializer;
 import org.apache.reef.tang.formats.ConfigurationSerializer;
 import org.apache.reef.tang.implementation.protobuf.ProtocolBufferClassHierarchy;
 import org.apache.reef.tang.proto.ClassHierarchyProto;
@@ -35,16 +36,21 @@ import java.util.Set;
 
 /**
  * Test case for class hierarchy deserialization.
+ * TODO The files should be created and deserialized by the AvroClassHierarchySerializer
  */
 public class ClassHierarchyDeserializationTest {
+  private final ConfigurationSerializer configurationSerializer = new AvroConfigurationSerializer();
+  private final ClassHierarchySerializer classHierarchySerializer = new AvroClassHierarchySerializer();
 
   /**
    * generate task.bin from running .Net ClassHierarchyBuilder.exe
    */
   @Test
   public void testDeserializationForTasks() {
+    // TODO The file should be written by Avro
     try (final InputStream chin = Thread.currentThread().getContextClassLoader()
         .getResourceAsStream("Task.bin")) {
+      // TODO Use AvroClassHierarchySerializer instead
       final ClassHierarchyProto.Node root = ClassHierarchyProto.Node.parseFrom(chin);
       final ClassHierarchy ch = new ProtocolBufferClassHierarchy(root);
       Node n1 = ch.getNode("Org.Apache.REEF.Examples.Tasks.StreamingTasks.StreamTask1, Org.Apache.REEF.Examples.Tasks, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null");
@@ -75,8 +81,10 @@ public class ClassHierarchyDeserializationTest {
     final ConfigurationBuilder taskConfigurationBuilder;
     final ConfigurationBuilder eventConfigurationBuilder;
 
+    // TODO The file should be written by Avro
     try (final InputStream chin = Thread.currentThread().getContextClassLoader()
         .getResourceAsStream("Task.bin")) {
+      // TODO Use AvroClassHierarchySerializer instead
       final ClassHierarchyProto.Node root = ClassHierarchyProto.Node.parseFrom(chin);
       final ClassHierarchy ch = new ProtocolBufferClassHierarchy(root);
       taskConfigurationBuilder = Tang.Factory.getTang().newConfigurationBuilder(ch);
@@ -85,8 +93,10 @@ public class ClassHierarchyDeserializationTest {
       throw new RuntimeException(message, e);
     }
 
+    // TODO The file should be written by Avro
     try (final InputStream chin = Thread.currentThread().getContextClassLoader()
         .getResourceAsStream("Event.bin")) {
+      // TODO Use AvroClassHierarchySerializer instead
       final ClassHierarchyProto.Node root = ClassHierarchyProto.Node.parseFrom(chin);
       final ClassHierarchy ch = new ProtocolBufferClassHierarchy(root);
       eventConfigurationBuilder = Tang.Factory.getTang().newConfigurationBuilder(ch);
@@ -103,8 +113,10 @@ public class ClassHierarchyDeserializationTest {
    */
   @Test
   public void testDeserializationForEvent() {
+    // TODO The file should be written by Avro
     try (final InputStream chin = Thread.currentThread().getContextClassLoader()
         .getResourceAsStream("Event.bin")) {
+      // TODO Use AvroClassHierarchySerializer instead
       final ClassHierarchyProto.Node root = ClassHierarchyProto.Node.parseFrom(chin);
       final ClassHierarchy ch = new ProtocolBufferClassHierarchy(root);
       final ConfigurationBuilder taskConfigurationBuilder = Tang.Factory.getTang()
@@ -120,15 +132,14 @@ public class ClassHierarchyDeserializationTest {
   public void testBindSetEntryWithSetOfT() throws IOException {
     final ClassHierarchy ns1 = Tang.Factory.getTang().getDefaultClassHierarchy();
     ns1.getNode(SetOfClasses.class.getName());
-    final ClassHierarchy ns2 = new ProtocolBufferClassHierarchy(ProtocolBufferClassHierarchy.serialize(ns1));
+    final ClassHierarchy ns2 = classHierarchySerializer.fromString(classHierarchySerializer.toString(ns1));
     final ConfigurationBuilder cb = Tang.Factory.getTang().newConfigurationBuilder(ns2);
 
     final NamedParameterNode<Set<Number>> n2 = (NamedParameterNode<Set<Number>>) ns1.getNode(SetOfClasses.class.getName());
     final Node fn = ns1.getNode(Float.class.getName());
     cb.bindSetEntry(n2, fn);
 
-    final ConfigurationSerializer serializer = new AvroConfigurationSerializer();
-    final Configuration c = serializer.fromString(serializer.toString(cb.build()), ns2);
+    final Configuration c = configurationSerializer.fromString(configurationSerializer.toString(cb.build()), ns2);
   }
 
   @Test
@@ -136,7 +147,7 @@ public class ClassHierarchyDeserializationTest {
   public void testBindSetEntryWithSetOfString() throws IOException {
     final ClassHierarchy ns1 = Tang.Factory.getTang().getDefaultClassHierarchy();
     ns1.getNode(SetOfStrings.class.getName());
-    final ClassHierarchy ns2 = new ProtocolBufferClassHierarchy(ProtocolBufferClassHierarchy.serialize(ns1));
+    final ClassHierarchy ns2 = classHierarchySerializer.fromString(classHierarchySerializer.toString(ns1));
     final ConfigurationBuilder cb = Tang.Factory.getTang().newConfigurationBuilder(ns2);
     cb.bindSetEntry(SetOfStrings.class.getName(), "four");
     cb.bindSetEntry(SetOfStrings.class.getName(), "five");
@@ -144,7 +155,6 @@ public class ClassHierarchyDeserializationTest {
     final NamedParameterNode<Set<String>> n2 = (NamedParameterNode<Set<String>>) ns1.getNode(SetOfStrings.class.getName());
     cb.bindSetEntry(n2, "six");
 
-    final ConfigurationSerializer serializer = new AvroConfigurationSerializer();
-    final Configuration c = serializer.fromString(serializer.toString(cb.build()), ns2);
+    final Configuration c = configurationSerializer.fromString(configurationSerializer.toString(cb.build()), ns2);
   }
 }

http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/14418120/lang/java/reef-tang/tang/src/test/java/org/apache/reef/tang/test/RoundTripTest.java
----------------------------------------------------------------------
diff --git a/lang/java/reef-tang/tang/src/test/java/org/apache/reef/tang/test/RoundTripTest.java b/lang/java/reef-tang/tang/src/test/java/org/apache/reef/tang/test/RoundTripTest.java
index 2dd34a6..7000568 100644
--- a/lang/java/reef-tang/tang/src/test/java/org/apache/reef/tang/test/RoundTripTest.java
+++ b/lang/java/reef-tang/tang/src/test/java/org/apache/reef/tang/test/RoundTripTest.java
@@ -21,7 +21,8 @@ package org.apache.reef.tang.test;
 import org.apache.reef.tang.ClassHierarchy;
 import org.apache.reef.tang.Configuration;
 import org.apache.reef.tang.Tang;
-import org.apache.reef.tang.implementation.protobuf.ProtocolBufferClassHierarchy;
+import org.apache.reef.tang.formats.AvroClassHierarchySerializer;
+import org.apache.reef.tang.formats.ClassHierarchySerializer;
 import org.junit.Assert;
 import org.junit.Test;
 
@@ -48,7 +49,8 @@ public abstract class RoundTripTest {
   public void testRoundTripWithClassHierarchy() throws Exception {
     // TODO: use 'getConfiguration' instead of 'getConfigurationWithoutList' after #192 is fixed
     final Configuration confBefore = ObjectTreeTest.getConfigurationWithoutList();
-    final ClassHierarchy c = new ProtocolBufferClassHierarchy(ProtocolBufferClassHierarchy.serialize(confBefore.getClassHierarchy()));
+    final ClassHierarchySerializer chSerializer = new AvroClassHierarchySerializer();
+    final ClassHierarchy c = chSerializer.fromString(chSerializer.toString(confBefore.getClassHierarchy()));
     final Configuration confAfter = roundTrip(confBefore, c);
     Assert.assertEquals(confBefore.getNamedParameters().size(), confAfter.getNamedParameters().size());
     //For now, we cannot use ProtocolBufferClassHierarchy to do injection


[13/17] incubator-reef git commit: Modify the round trip test to cover more type of serialization

Posted by yu...@apache.org.
Modify the round trip test to cover more type of serialization


Project: http://git-wip-us.apache.org/repos/asf/incubator-reef/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-reef/commit/2328a9dd
Tree: http://git-wip-us.apache.org/repos/asf/incubator-reef/tree/2328a9dd
Diff: http://git-wip-us.apache.org/repos/asf/incubator-reef/diff/2328a9dd

Branch: refs/heads/REEF-395
Commit: 2328a9ddb3dbc765375306a141ebcf6d2358e2ea
Parents: 47865fd
Author: Yunseong Lee <yu...@apache.org>
Authored: Mon Jun 22 21:09:52 2015 +0900
Committer: Yunseong Lee <yu...@apache.org>
Committed: Mon Jun 22 21:09:52 2015 +0900

----------------------------------------------------------------------
 .../formats/TestClassHierarchyRoundTrip.java    | 250 ++++++++++++++++++-
 1 file changed, 240 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/2328a9dd/lang/java/reef-tang/tang/src/test/java/org/apache/reef/tang/formats/TestClassHierarchyRoundTrip.java
----------------------------------------------------------------------
diff --git a/lang/java/reef-tang/tang/src/test/java/org/apache/reef/tang/formats/TestClassHierarchyRoundTrip.java b/lang/java/reef-tang/tang/src/test/java/org/apache/reef/tang/formats/TestClassHierarchyRoundTrip.java
index 558ba0f..9a06531 100644
--- a/lang/java/reef-tang/tang/src/test/java/org/apache/reef/tang/formats/TestClassHierarchyRoundTrip.java
+++ b/lang/java/reef-tang/tang/src/test/java/org/apache/reef/tang/formats/TestClassHierarchyRoundTrip.java
@@ -30,378 +30,608 @@ import java.io.File;
 import java.io.IOException;
 
 public class TestClassHierarchyRoundTrip extends TestClassHierarchy {
+  final AvroClassHierarchySerializer serializer = new AvroClassHierarchySerializer();
 
-  private void setup1() {
+  // The original ClassHierarchy
+  private void setup0() {
     TangImpl.reset();
     ns = Tang.Factory.getTang().getDefaultClassHierarchy();
   }
 
+  // Serializes ClassHierarchy to AvroClassHierarchy
+  private void setup1() {
+    TangImpl.reset();
+    ns = serializer.fromAvro(serializer.toAvro(ns));
+  }
+
+  // Serializes ClassHierarchy to file
   private void setup2() {
     TangImpl.reset();
-    final AvroClassHierarchySerializer serializer = new AvroClassHierarchySerializer();
     try {
-      ns = serializer.fromString(serializer.toString(ns));
+      final File file = java.io.File.createTempFile("TangTest", "avroch");
+      serializer.toFile(ns, file);
+      ns = serializer.fromFile(file);
+      file.delete();
     } catch (IOException e) {
-      Assert.fail(String.format("IOException when serialize/deserialize the string created by Avro", e));
+      Assert.fail(String.format("IOException when serialize/deserialize the ClassHierarchy", e));
     }
   }
 
+  // Serializes ClassHierarchy to TextFile
   private void setup3() {
     TangImpl.reset();
     try {
-      final File file = java.io.File.createTempFile("testAvro", "tmp");
-      final ClassHierarchySerializer serializer = new AvroClassHierarchySerializer();
-      serializer.toFile(ns, file);
-      ns = serializer.fromFile(file);
-      file.delete();
+      final File textFile = java.io.File.createTempFile("TangTest", "avroch");
+      serializer.toTextFile(ns, textFile);
+      ns = serializer.fromTextFile(textFile);
+      textFile.delete();
+    } catch (IOException e) {
+      Assert.fail(String.format("IOException when serialize/deserialize the ClassHierarchy", e));
+    }
+  }
+
+  // Serializes ClassHierarchy to byte[]
+  private void setup4() {
+    TangImpl.reset();
+    try {
+      ns = serializer.fromByteArray(serializer.toByteArray(ns));
+    } catch (IOException e) {
+      Assert.fail(String.format("IOException when serialize/deserialize the ClassHierarchy", e));
+    }
+  }
+
+  // Serializes ClassHierarchy to String
+  private void setup5() {
+    TangImpl.reset();
+    try {
+      ns = serializer.fromString(serializer.toString(ns));
     } catch (IOException e) {
-      Assert.fail(String.format("IOException when serialize/deserialize the file created by Avro", e));
+      Assert.fail(String.format("IOException when serialize/deserialize the ClassHierarchy", e));
     }
   }
 
   @Test
   @Override
   public void testJavaString() throws NameResolutionException {
+    setup0();
+    super.testJavaString();
     setup1();
     super.testJavaString();
     setup2();
     super.testJavaString();
     setup3();
     super.testJavaString();
+    setup4();
+    super.testJavaString();
+    setup5();
+    super.testJavaString();
   }
 
   @Test
   @Override
   public void testSimpleConstructors() throws NameResolutionException {
+    setup0();
+    super.testSimpleConstructors();
     setup1();
     super.testSimpleConstructors();
     setup2();
     super.testSimpleConstructors();
     setup3();
     super.testSimpleConstructors();
+    setup4();
+    super.testSimpleConstructors();
+    setup5();
+    super.testSimpleConstructors();
   }
 
   @Test
   @Override
   public void testNamedParameterConstructors() throws NameResolutionException {
+    setup0();
+    super.testNamedParameterConstructors();
     setup1();
     super.testNamedParameterConstructors();
     setup2();
     super.testNamedParameterConstructors();
     setup3();
     super.testNamedParameterConstructors();
+    setup4();
+    super.testNamedParameterConstructors();
+    setup5();
+    super.testNamedParameterConstructors();
   }
 
   @Test
   @Override
   public void testArray() throws NameResolutionException {
+    setup0();
+    super.testArray();
     setup1();
     super.testArray();
     setup2();
     super.testArray();
     setup3();
     super.testArray();
+    setup4();
+    super.testArray();
+    setup5();
+    super.testArray();
   }
 
   @Test
   @Override
   public void testRepeatConstructorArg() throws NameResolutionException {
+    setup0();
+    super.testRepeatConstructorArg();
     setup1();
     super.testRepeatConstructorArg();
     setup2();
     super.testRepeatConstructorArg();
     setup3();
     super.testRepeatConstructorArg();
+    setup4();
+    super.testRepeatConstructorArg();
+    setup5();
+    super.testRepeatConstructorArg();
   }
 
   @Test
   @Override
   public void testRepeatConstructorArgClasses() throws NameResolutionException {
+    setup0();
+    super.testRepeatConstructorArgClasses();
     setup1();
     super.testRepeatConstructorArgClasses();
     setup2();
     super.testRepeatConstructorArgClasses();
     setup3();
     super.testRepeatConstructorArgClasses();
+    setup4();
+    super.testRepeatConstructorArgClasses();
+    setup5();
+    super.testRepeatConstructorArgClasses();
   }
 
   @Test
   @Override
   public void testLeafRepeatedConstructorArgClasses() throws NameResolutionException {
+    setup0();
+    super.testLeafRepeatedConstructorArgClasses();
     setup1();
     super.testLeafRepeatedConstructorArgClasses();
     setup2();
     super.testLeafRepeatedConstructorArgClasses();
     setup3();
     super.testLeafRepeatedConstructorArgClasses();
+    setup4();
+    super.testLeafRepeatedConstructorArgClasses();
+    setup5();
+    super.testLeafRepeatedConstructorArgClasses();
   }
 
   @Test
   @Override
   public void testNamedRepeatConstructorArgClasses() throws NameResolutionException {
+    setup0();
+    super.testNamedRepeatConstructorArgClasses();
     setup1();
     super.testNamedRepeatConstructorArgClasses();
     setup2();
     super.testNamedRepeatConstructorArgClasses();
     setup3();
     super.testNamedRepeatConstructorArgClasses();
+    setup4();
+    super.testNamedRepeatConstructorArgClasses();
+    setup5();
+    super.testNamedRepeatConstructorArgClasses();
   }
 
   @Test
   @Override
   public void testResolveDependencies() throws NameResolutionException {
+    setup0();
+    super.testResolveDependencies();
     setup1();
     super.testResolveDependencies();
     setup2();
     super.testResolveDependencies();
     setup3();
     super.testResolveDependencies();
+    setup4();
+    super.testResolveDependencies();
+    setup5();
+    super.testResolveDependencies();
   }
 
   @Test
   @Override
   public void testDocumentedLocalNamedParameter() throws NameResolutionException {
+    setup0();
+    super.testDocumentedLocalNamedParameter();
     setup1();
     super.testDocumentedLocalNamedParameter();
     setup2();
     super.testDocumentedLocalNamedParameter();
     setup3();
     super.testDocumentedLocalNamedParameter();
+    setup4();
+    super.testDocumentedLocalNamedParameter();
+    setup5();
+    super.testDocumentedLocalNamedParameter();
   }
 
   @Test
   @Override
   public void testNamedParameterTypeMismatch() throws NameResolutionException {
+    setup0();
+    super.testNamedParameterTypeMismatch();
     setup1();
     super.testNamedParameterTypeMismatch();
     setup2();
     super.testNamedParameterTypeMismatch();
     setup3();
     super.testNamedParameterTypeMismatch();
+    setup4();
+    super.testNamedParameterTypeMismatch();
+    setup5();
+    super.testNamedParameterTypeMismatch();
   }
 
   @Test
   @Override
   public void testUnannotatedName() throws NameResolutionException {
+    setup0();
+    super.testUnannotatedName();
     setup1();
     super.testUnannotatedName();
     setup2();
     super.testUnannotatedName();
     setup3();
     super.testUnannotatedName();
+    setup4();
+    super.testUnannotatedName();
+    setup5();
+    super.testUnannotatedName();
   }
 
   @Test
   @Override
   public void testAnnotatedNotName() throws NameResolutionException {
+    setup0();
+    super.testAnnotatedNotName();
     setup1();
     super.testAnnotatedNotName();
     setup2();
     super.testAnnotatedNotName();
     setup3();
     super.testAnnotatedNotName();
+    setup4();
+    super.testAnnotatedNotName();
+    setup5();
+    super.testAnnotatedNotName();
   }
 
   @Test
   @Override
   public void testGenericTorture1() throws NameResolutionException {
+    setup0();
+    super.testGenericTorture1();
     setup1();
     super.testGenericTorture1();
     setup2();
     super.testGenericTorture1();
     setup3();
     super.testGenericTorture1();
+    setup4();
+    super.testGenericTorture1();
+    setup5();
+    super.testGenericTorture1();
   }
 
   @Test
   @Override
   public void testGenericTorture2() throws NameResolutionException {
+    setup0();
+    super.testGenericTorture2();
     setup1();
     super.testGenericTorture2();
     setup2();
     super.testGenericTorture2();
     setup3();
     super.testGenericTorture2();
+    setup4();
+    super.testGenericTorture2();
+    setup5();
+    super.testGenericTorture2();
   }
 
   @Test
   @Override
   public void testGenericTorture3() throws NameResolutionException {
+    setup0();
+    super.testGenericTorture3();
     setup1();
     super.testGenericTorture3();
     setup2();
     super.testGenericTorture3();
     setup3();
     super.testGenericTorture3();
+    setup4();
+    super.testGenericTorture3();
+    setup5();
+    super.testGenericTorture3();
   }
 
   @Test
   @Override
   public void testGenericTorture4() throws NameResolutionException {
+    setup0();
+    super.testGenericTorture4();
     setup1();
     super.testGenericTorture4();
     setup2();
     super.testGenericTorture4();
     setup3();
     super.testGenericTorture4();
+    setup4();
+    super.testGenericTorture4();
+    setup5();
+    super.testGenericTorture4();
   }
 
   @Test
   @Override
   public void testGenericTorture5() throws NameResolutionException {
+    setup0();
+    super.testGenericTorture5();
     setup1();
     super.testGenericTorture5();
     setup2();
     super.testGenericTorture5();
     setup3();
     super.testGenericTorture5();
+    setup4();
+    super.testGenericTorture5();
+    setup5();
+    super.testGenericTorture5();
   }
 
   @Test
   @Override
   public void testGenericTorture6() throws NameResolutionException {
+    setup0();
+    super.testGenericTorture6();
     setup1();
     super.testGenericTorture6();
     setup2();
     super.testGenericTorture6();
     setup3();
     super.testGenericTorture6();
+    setup4();
+    super.testGenericTorture6();
+    setup5();
+    super.testGenericTorture6();
   }
 
   @Test
   @Override
   public void testGenericTorture7() throws NameResolutionException {
+    setup0();
+    super.testGenericTorture7();
     setup1();
     super.testGenericTorture7();
     setup2();
     super.testGenericTorture7();
     setup3();
     super.testGenericTorture7();
+    setup4();
+    super.testGenericTorture7();
+    setup5();
+    super.testGenericTorture7();
   }
 
   @Test
   @Override
   public void testGenericTorture8() throws NameResolutionException {
+    setup0();
+    super.testGenericTorture8();
     setup1();
     super.testGenericTorture8();
     setup2();
     super.testGenericTorture8();
     setup3();
     super.testGenericTorture8();
+    setup4();
+    super.testGenericTorture8();
+    setup5();
+    super.testGenericTorture8();
   }
 
   @Test
   @Override
   public void testGenericTorture9() throws NameResolutionException {
+    setup0();
+    super.testGenericTorture9();
     setup1();
     super.testGenericTorture9();
     setup2();
     super.testGenericTorture9();
     setup3();
     super.testGenericTorture9();
+    setup4();
+    super.testGenericTorture9();
+    setup5();
+    super.testGenericTorture9();
   }
 
   @Test
   @Override
   public void testGenericTorture10() throws NameResolutionException {
+    setup0();
+    super.testGenericTorture10();
     setup1();
     super.testGenericTorture10();
     setup2();
     super.testGenericTorture10();
+    setup3();
+    super.testGenericTorture10();
+    setup4();
+    super.testGenericTorture10();
+    setup5();
+    super.testGenericTorture10();
   }
 
   @Test
   @Override
   public void testGenericTorture11() throws NameResolutionException {
+    setup0();
+    super.testGenericTorture11();
     setup1();
     super.testGenericTorture11();
     setup2();
     super.testGenericTorture11();
+    setup3();
+    super.testGenericTorture11();
+    setup4();
+    super.testGenericTorture11();
+    setup5();
+    super.testGenericTorture11();
   }
 
   @Test
   @Override
   public void testGenericTorture12() throws NameResolutionException {
+    setup0();
+    super.testGenericTorture12();
     setup1();
     super.testGenericTorture12();
     setup2();
     super.testGenericTorture12();
+    setup3();
+    super.testGenericTorture12();
+    setup4();
+    super.testGenericTorture12();
+    setup5();
+    super.testGenericTorture12();
   }
 
   @Test
   @Override
   public void testInjectNonStaticLocalArgClass() throws NameResolutionException {
+    setup0();
+    super.testInjectNonStaticLocalArgClass();
     setup1();
     super.testInjectNonStaticLocalArgClass();
     setup2();
     super.testInjectNonStaticLocalArgClass();
     setup3();
     super.testInjectNonStaticLocalArgClass();
+    setup4();
+    super.testInjectNonStaticLocalArgClass();
+    setup5();
+    super.testInjectNonStaticLocalArgClass();
+
   }
 
   @Test
   @Override
   public void testOKShortNames() throws NameResolutionException {
+    setup0();
+    super.testOKShortNames();
     setup1();
     super.testOKShortNames();
     setup2();
     super.testOKShortNames();
     setup3();
     super.testOKShortNames();
+    setup4();
+    super.testOKShortNames();
+    setup5();
+    super.testOKShortNames();
   }
 
   @Test
   @Override
   public void testRoundTripInnerClassNames() throws NameResolutionException, ClassNotFoundException {
+    setup0();
+    super.testRoundTripInnerClassNames();
     setup1();
     super.testRoundTripInnerClassNames();
     setup2();
     super.testRoundTripInnerClassNames();
     setup3();
     super.testRoundTripInnerClassNames();
+    setup4();
+    super.testRoundTripInnerClassNames();
+    setup5();
+    super.testRoundTripInnerClassNames();
   }
 
   @Test
   @Override
   public void testUnitIsInjectable() throws NameResolutionException, InjectionException {
+    setup0();
+    super.testUnitIsInjectable();
     setup1();
     super.testUnitIsInjectable();
     setup2();
     super.testUnitIsInjectable();
     setup3();
     super.testUnitIsInjectable();
+    setup4();
+    super.testUnitIsInjectable();
+    setup5();
+    super.testUnitIsInjectable();
   }
 
   @Test
   @Override
   public void testBadUnitDecl() throws NameResolutionException {
+    setup0();
+    super.testBadUnitDecl();
     setup1();
     super.testBadUnitDecl();
     setup2();
     super.testBadUnitDecl();
     setup3();
     super.testBadUnitDecl();
+    setup4();
+    super.testBadUnitDecl();
+    setup5();
+    super.testBadUnitDecl();
   }
 
   @Test
   @Override
   public void nameCantBindWrongSubclassAsDefault() throws NameResolutionException {
+    setup0();
+    super.nameCantBindWrongSubclassAsDefault();
     setup1();
     super.nameCantBindWrongSubclassAsDefault();
     setup2();
     super.nameCantBindWrongSubclassAsDefault();
     setup3();
     super.nameCantBindWrongSubclassAsDefault();
+    setup4();
+    super.nameCantBindWrongSubclassAsDefault();
+    setup5();
+    super.nameCantBindWrongSubclassAsDefault();
   }
 
   @Test
   @Override
   public void ifaceCantBindWrongImplAsDefault() throws NameResolutionException {
+    setup0();
+    super.ifaceCantBindWrongImplAsDefault();
     setup1();
     super.ifaceCantBindWrongImplAsDefault();
     setup2();
     super.ifaceCantBindWrongImplAsDefault();
     setup3();
     super.ifaceCantBindWrongImplAsDefault();
+    setup4();
+    super.ifaceCantBindWrongImplAsDefault();
+    setup5();
+    super.ifaceCantBindWrongImplAsDefault();
   }
 }


[17/17] incubator-reef git commit: Minor cosmetic issues

Posted by yu...@apache.org.
Minor cosmetic issues


Project: http://git-wip-us.apache.org/repos/asf/incubator-reef/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-reef/commit/a5bfae41
Tree: http://git-wip-us.apache.org/repos/asf/incubator-reef/tree/a5bfae41
Diff: http://git-wip-us.apache.org/repos/asf/incubator-reef/diff/a5bfae41

Branch: refs/heads/REEF-395
Commit: a5bfae41171a68211b64d8dc505f3a8f180c2a9e
Parents: a86ca06
Author: Yunseong Lee <yu...@apache.org>
Authored: Tue Jun 23 00:52:34 2015 +0900
Committer: Yunseong Lee <yu...@apache.org>
Committed: Tue Jun 23 00:52:34 2015 +0900

----------------------------------------------------------------------
 .../src/main/java/org/apache/reef/tang/ClassHierarchy.java     | 2 --
 .../apache/reef/tang/formats/AvroClassHierarchySerializer.java | 5 +----
 .../org/apache/reef/tang/formats/ClassHierarchySerializer.java | 6 +++++-
 .../reef/tang/implementation/java/AvroClassHierarchy.java      | 5 +++--
 .../apache/reef/tang/formats/TestClassHierarchyRoundTrip.java  | 2 +-
 5 files changed, 10 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/a5bfae41/lang/java/reef-tang/tang/src/main/java/org/apache/reef/tang/ClassHierarchy.java
----------------------------------------------------------------------
diff --git a/lang/java/reef-tang/tang/src/main/java/org/apache/reef/tang/ClassHierarchy.java b/lang/java/reef-tang/tang/src/main/java/org/apache/reef/tang/ClassHierarchy.java
index 0a8a3e2..9f89471 100644
--- a/lang/java/reef-tang/tang/src/main/java/org/apache/reef/tang/ClassHierarchy.java
+++ b/lang/java/reef-tang/tang/src/main/java/org/apache/reef/tang/ClassHierarchy.java
@@ -18,7 +18,6 @@
  */
 package org.apache.reef.tang;
 
-import org.apache.reef.tang.exceptions.ClassHierarchyException;
 import org.apache.reef.tang.exceptions.NameResolutionException;
 import org.apache.reef.tang.types.ClassNode;
 import org.apache.reef.tang.types.Node;
@@ -43,7 +42,6 @@ public interface ClassHierarchy {
    * @param fullName The full name of the class that will be looked up.
    * @return A non-null reference to a ClassNode or a NamedParameterNode.
    * @throws NameResolutionException If the class is not found.
-   * @throws ClassHierarchyException If the class does not pass Tang's static analysis.
    */
   Node getNode(String fullName) throws NameResolutionException;
 

http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/a5bfae41/lang/java/reef-tang/tang/src/main/java/org/apache/reef/tang/formats/AvroClassHierarchySerializer.java
----------------------------------------------------------------------
diff --git a/lang/java/reef-tang/tang/src/main/java/org/apache/reef/tang/formats/AvroClassHierarchySerializer.java b/lang/java/reef-tang/tang/src/main/java/org/apache/reef/tang/formats/AvroClassHierarchySerializer.java
index 07804e4..6c95498 100644
--- a/lang/java/reef-tang/tang/src/main/java/org/apache/reef/tang/formats/AvroClassHierarchySerializer.java
+++ b/lang/java/reef-tang/tang/src/main/java/org/apache/reef/tang/formats/AvroClassHierarchySerializer.java
@@ -17,16 +17,13 @@ import java.util.Arrays;
 import java.util.List;
 
 /**
- * (De-)Serializing ClassHierarchy to and from AvroClassHierarchy.
- * <p/>
+ * Serialize and Deserialize ClassHierarchy to and from AvroClassHierarchy.
  * This class is stateless and is therefore safe to reuse.
  */
 public class AvroClassHierarchySerializer implements ClassHierarchySerializer {
 
   /**
    * The Charset used for the JSON encoding.
-   * <p/>
-   * Copied from <code>org.apache.avro.io.JsonDecoder.CHARSET</code>
    */
   private static final String JSON_CHARSET = "ISO-8859-1";
 

http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/a5bfae41/lang/java/reef-tang/tang/src/main/java/org/apache/reef/tang/formats/ClassHierarchySerializer.java
----------------------------------------------------------------------
diff --git a/lang/java/reef-tang/tang/src/main/java/org/apache/reef/tang/formats/ClassHierarchySerializer.java b/lang/java/reef-tang/tang/src/main/java/org/apache/reef/tang/formats/ClassHierarchySerializer.java
index c5a77ae..4b7c357 100644
--- a/lang/java/reef-tang/tang/src/main/java/org/apache/reef/tang/formats/ClassHierarchySerializer.java
+++ b/lang/java/reef-tang/tang/src/main/java/org/apache/reef/tang/formats/ClassHierarchySerializer.java
@@ -39,7 +39,7 @@ public interface ClassHierarchySerializer {
 
   /**
    * Serializes a ClassHierarchy as a String.
-   * @param classHierarchy
+   *
    * @param classHierarchy the ClassHierarchy to store
    * @throws IOException if there is an error in the process
    */
@@ -47,6 +47,7 @@ public interface ClassHierarchySerializer {
 
   /**
    * Loads a ClassHierarchy from a file created with toFile().
+   *
    * @param file the File to read from
    * @throws IOException if the File can't be read or parsed
    */
@@ -54,6 +55,7 @@ public interface ClassHierarchySerializer {
 
   /**
    * Loads a ClassHierarchy from a text file created with toTextFile().
+   *
    * @param file the File to read from
    * @throws IOException if the File can't be read or parsed
    */
@@ -61,6 +63,7 @@ public interface ClassHierarchySerializer {
 
   /**
    * Deserializes a ClassHierarchy from a byte[] created with toByteArray().
+   *
    * @param theBytes the byte[] to deserialize
    * @throws IOException if the byte[] can't be read or parsed
    */
@@ -68,6 +71,7 @@ public interface ClassHierarchySerializer {
 
   /**
    * Deserializes a ClassHierarchy from a String created with toString().
+   *
    * @param theString the String to deserialize
    * @throws IOException if the String can't be read or parsed
    */

http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/a5bfae41/lang/java/reef-tang/tang/src/main/java/org/apache/reef/tang/implementation/java/AvroClassHierarchy.java
----------------------------------------------------------------------
diff --git a/lang/java/reef-tang/tang/src/main/java/org/apache/reef/tang/implementation/java/AvroClassHierarchy.java b/lang/java/reef-tang/tang/src/main/java/org/apache/reef/tang/implementation/java/AvroClassHierarchy.java
index f4f22f0..761fa5d 100644
--- a/lang/java/reef-tang/tang/src/main/java/org/apache/reef/tang/implementation/java/AvroClassHierarchy.java
+++ b/lang/java/reef-tang/tang/src/main/java/org/apache/reef/tang/implementation/java/AvroClassHierarchy.java
@@ -29,14 +29,15 @@ public class AvroClassHierarchy implements ClassHierarchy {
       parseSubHierarchy(namespace, child);
     }
     buildLookupTable(namespace);
-    // Now, register the implementations
+
+    // Register the implementations
     for (final AvroNode child : root.getChildren()) {
       wireUpInheritanceRelationships(child);
     }
   }
 
   /**
-   * Build a table that matches the name and the corresponding Node recursively.
+   * Build a table that maps the name to the corresponding Node recursively.
    */
   private void buildLookupTable(final Node n) {
     for(final Node child : n.getChildren()) {

http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/a5bfae41/lang/java/reef-tang/tang/src/test/java/org/apache/reef/tang/formats/TestClassHierarchyRoundTrip.java
----------------------------------------------------------------------
diff --git a/lang/java/reef-tang/tang/src/test/java/org/apache/reef/tang/formats/TestClassHierarchyRoundTrip.java b/lang/java/reef-tang/tang/src/test/java/org/apache/reef/tang/formats/TestClassHierarchyRoundTrip.java
index b49de46..4029f6c 100644
--- a/lang/java/reef-tang/tang/src/test/java/org/apache/reef/tang/formats/TestClassHierarchyRoundTrip.java
+++ b/lang/java/reef-tang/tang/src/test/java/org/apache/reef/tang/formats/TestClassHierarchyRoundTrip.java
@@ -32,7 +32,7 @@ import java.io.IOException;
 public class TestClassHierarchyRoundTrip extends TestClassHierarchy {
   final ClassHierarchySerializer serializer = new AvroClassHierarchySerializer();
 
-  // The original ClassHierarchy
+  // The default ClassHierarchy
   private void setup0() {
     TangImpl.reset();
     ns = Tang.Factory.getTang().getDefaultClassHierarchy();


[06/17] incubator-reef git commit: Update the comment and catch cluases

Posted by yu...@apache.org.
Update the comment and catch cluases


Project: http://git-wip-us.apache.org/repos/asf/incubator-reef/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-reef/commit/848b5bb0
Tree: http://git-wip-us.apache.org/repos/asf/incubator-reef/tree/848b5bb0
Diff: http://git-wip-us.apache.org/repos/asf/incubator-reef/diff/848b5bb0

Branch: refs/heads/REEF-395
Commit: 848b5bb0130276c9f7aa5174b56358dc67473628
Parents: 7a095da
Author: Yunseong Lee <yu...@apache.org>
Authored: Sun Jun 21 15:56:31 2015 +0900
Committer: Yunseong Lee <yu...@apache.org>
Committed: Sun Jun 21 15:56:31 2015 +0900

----------------------------------------------------------------------
 .../implementation/java/AvroClassHierarchy.java | 61 ++++++++++++--------
 1 file changed, 36 insertions(+), 25 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/848b5bb0/lang/java/reef-tang/tang/src/main/java/org/apache/reef/tang/implementation/java/AvroClassHierarchy.java
----------------------------------------------------------------------
diff --git a/lang/java/reef-tang/tang/src/main/java/org/apache/reef/tang/implementation/java/AvroClassHierarchy.java b/lang/java/reef-tang/tang/src/main/java/org/apache/reef/tang/implementation/java/AvroClassHierarchy.java
index 96ab16c..f4f22f0 100644
--- a/lang/java/reef-tang/tang/src/main/java/org/apache/reef/tang/implementation/java/AvroClassHierarchy.java
+++ b/lang/java/reef-tang/tang/src/main/java/org/apache/reef/tang/implementation/java/AvroClassHierarchy.java
@@ -14,7 +14,6 @@ import java.util.List;
 /**
  * Implementation of ClassHierarchy for Avro
  */
-// TODO Should it be a ClassHierarchy?
 public class AvroClassHierarchy implements ClassHierarchy {
   private final PackageNode namespace;
   private final HashMap<String, Node> lookupTable = new HashMap<>();
@@ -36,6 +35,9 @@ public class AvroClassHierarchy implements ClassHierarchy {
     }
   }
 
+  /**
+   * Build a table that matches the name and the corresponding Node recursively.
+   */
   private void buildLookupTable(final Node n) {
     for(final Node child : n.getChildren()) {
       lookupTable.put(child.getFullName(), child);
@@ -43,24 +45,27 @@ public class AvroClassHierarchy implements ClassHierarchy {
     }
   }
 
+  /**
+   * Parse the constructor definition.
+   */
   private static ConstructorDef<?> parseConstructorDef(final AvroConstructorDef def, final boolean isInjectable) {
     final List<ConstructorArg> args = new ArrayList<>();
     for (final AvroConstructorArg arg : def.getConstructorArg()) {
-      final String namedParameterName = arg.getNamedParameterName() == null ? null : arg.getNamedParameterName();
-      args.add(new ConstructorArgImpl(arg.getFullArgClassName(), namedParameterName,
-              arg.getIsInjectionFuture()));
+      args.add(new ConstructorArgImpl(arg.getFullArgClassName(), arg.getNamedParameterName(), arg.getIsInjectionFuture()));
     }
-    return new ConstructorDefImpl<>(def.getFullArgClassName(), args.toArray(new ConstructorArg[0]),
-            isInjectable);
+    return new ConstructorDefImpl<>(def.getFullArgClassName(), args.toArray(new ConstructorArg[0]), isInjectable);
   }
 
-  private static void parseSubHierarchy(Node parent, AvroNode n) {
+  /**
+   * Register the classes recursively.
+   */
+  private static void parseSubHierarchy(final Node parent, final AvroNode n) {
     final Node parsed;
     if (n.getPackageNode() != null) {
       parsed = new PackageNodeImpl(parent, n.getName(), n.getFullName());
     } else if (n.getNamedParameterNode() != null) {
       final AvroNamedParameterNode np = n.getNamedParameterNode();
-      parsed = new NamedParameterNodeImpl<Object>(parent, n.getName(), n.getFullName(),
+      parsed = new NamedParameterNodeImpl<>(parent, n.getName(), n.getFullName(),
               np.getFullArgClassName(), np.getSimpleArgClassName(), np.getIsSet(), np.getIsList(),
               np.getDocumentation(), np.getShortName(), np.getInstanceDefault().toArray(new String[0]));
     } else if (n.getClassNode() != null) {
@@ -84,14 +89,17 @@ public class AvroClassHierarchy implements ClassHierarchy {
               cn.getIsInjectionCandidate(), cn.getIsExternalConstructor(), injectableConstructors.toArray(dummy),
               allConstructors.toArray(dummy), defaultImpl);
     } else {
-      throw new IllegalStateException("Bad protocol buffer: got abstract node" + n);
+      throw new IllegalStateException("Bad avro node: got abstract node" + n);
     }
-    for (AvroNode child : n.getChildren()) {
+
+    for (final AvroNode child : n.getChildren()) {
       parseSubHierarchy(parsed, child);
     }
   }
 
-
+  /**
+   * Register the implementation for the ClassNode recursively.
+   */
   @SuppressWarnings({"rawtypes", "unchecked"})
   private void wireUpInheritanceRelationships(final AvroNode n) {
     if (n.getClassNode() != null) {
@@ -100,25 +108,30 @@ public class AvroClassHierarchy implements ClassHierarchy {
       try {
         iface = (ClassNode) getNode(n.getFullName());
       } catch (NameResolutionException e) {
-        throw new IllegalStateException("When reading protocol buffer node "
-                + n.getFullName() + " does not exist.  Full record is " + n, e);
+        final String errorMessage = new StringBuilder()
+                .append("When reading avro node ").append(n.getFullName())
+                .append(" does not exist.  Full record is ").append(n).toString();
+        throw new IllegalStateException(errorMessage, e);
       }
       for (final String impl : cn.getImplFullNames()) {
         try {
           iface.putImpl((ClassNode) getNode(impl));
         } catch (NameResolutionException e) {
-          throw new IllegalStateException("When reading protocol buffer node "
-                  + n + " refers to non-existent implementation:" + impl);
+          final String errorMessage = new StringBuilder()
+                  .append("When reading avro node ").append(n)
+                  .append(" refers to non-existent implementation:").append(impl).toString();
+          throw new IllegalStateException(errorMessage, e);
         } catch (ClassCastException e) {
           try {
-            throw new IllegalStateException(
-                    "When reading protocol buffer node " + n
-                            + " found implementation" + getNode(impl)
-                            + " which is not a ClassNode!");
+            final String errorMessage = new StringBuilder()
+                    .append("When reading avro node ").append(n).append(" found implementation").append(getNode(impl))
+                    .append(" which is not a ClassNode!").toString();
+            throw new IllegalStateException(errorMessage, e);
           } catch (NameResolutionException e2) {
-            throw new IllegalStateException(
-                    "Got 'cant happen' exception when producing error message for "
-                            + e);
+            final String errorMessage = new StringBuilder()
+                    .append("Got 'cant happen' exception when producing error message for ")
+                    .append(e).toString();
+            throw new IllegalStateException(errorMessage);
           }
         }
       }
@@ -145,7 +158,7 @@ public class AvroClassHierarchy implements ClassHierarchy {
   }
 
   @Override
-  public ClassHierarchy merge(ClassHierarchy ch) {
+  public ClassHierarchy merge(final ClassHierarchy ch) {
     if (this == ch) {
       return this;
     }
@@ -158,7 +171,6 @@ public class AvroClassHierarchy implements ClassHierarchy {
     for (final String key : ach.lookupTable.keySet()) {
       if (!this.lookupTable.containsKey(key)) {
         this.lookupTable.put(key, ach.lookupTable.get(key));
-        // TODO : Does the order not matter here?
       }
     }
 
@@ -175,7 +187,6 @@ public class AvroClassHierarchy implements ClassHierarchy {
                   cn.isExternalConstructor(), cn.getInjectableConstructors(), cn.getAllConstructors(),
                   cn.getDefaultImplementation());
         }
-        // TODO What if n is the package node?
       }
     }
     return this;


[15/17] incubator-reef git commit: Exclude the test for the internal serialization

Posted by yu...@apache.org.
Exclude the test for the internal serialization


Project: http://git-wip-us.apache.org/repos/asf/incubator-reef/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-reef/commit/fb7c0269
Tree: http://git-wip-us.apache.org/repos/asf/incubator-reef/tree/fb7c0269
Diff: http://git-wip-us.apache.org/repos/asf/incubator-reef/diff/fb7c0269

Branch: refs/heads/REEF-395
Commit: fb7c0269d0b784397d9bfa447decafd0e2d60c8c
Parents: 1441812
Author: Yunseong Lee <yu...@apache.org>
Authored: Mon Jun 22 22:08:07 2015 +0900
Committer: Yunseong Lee <yu...@apache.org>
Committed: Mon Jun 22 22:08:07 2015 +0900

----------------------------------------------------------------------
 .../formats/AvroClassHierarchySerializer.java   |  6 +-
 .../formats/TestClassHierarchyRoundTrip.java    | 96 +-------------------
 2 files changed, 7 insertions(+), 95 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/fb7c0269/lang/java/reef-tang/tang/src/main/java/org/apache/reef/tang/formats/AvroClassHierarchySerializer.java
----------------------------------------------------------------------
diff --git a/lang/java/reef-tang/tang/src/main/java/org/apache/reef/tang/formats/AvroClassHierarchySerializer.java b/lang/java/reef-tang/tang/src/main/java/org/apache/reef/tang/formats/AvroClassHierarchySerializer.java
index 2f5c0d8..07804e4 100644
--- a/lang/java/reef-tang/tang/src/main/java/org/apache/reef/tang/formats/AvroClassHierarchySerializer.java
+++ b/lang/java/reef-tang/tang/src/main/java/org/apache/reef/tang/formats/AvroClassHierarchySerializer.java
@@ -36,19 +36,17 @@ public class AvroClassHierarchySerializer implements ClassHierarchySerializer {
 
   /**
    * Serialize the ClassHierarchy into the AvroNode.
-   * This method is set to be Package private for testing.
    * @param ch ClassHierarchy to serialize
    */
-  static AvroNode toAvro(final ClassHierarchy ch) {
+  private static AvroNode toAvro(final ClassHierarchy ch) {
     return newAvroNode(ch.getNamespace());
   }
 
   /**
    * Deserialize the ClassHierarchy from the AvroNode.
-   * This method is set to be Package private for testing.
    * @param n AvroNode to deserialize
    */
-  static ClassHierarchy fromAvro(final AvroNode n) {
+  private static ClassHierarchy fromAvro(final AvroNode n) {
     return new AvroClassHierarchy(n);
   }
 

http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/fb7c0269/lang/java/reef-tang/tang/src/test/java/org/apache/reef/tang/formats/TestClassHierarchyRoundTrip.java
----------------------------------------------------------------------
diff --git a/lang/java/reef-tang/tang/src/test/java/org/apache/reef/tang/formats/TestClassHierarchyRoundTrip.java b/lang/java/reef-tang/tang/src/test/java/org/apache/reef/tang/formats/TestClassHierarchyRoundTrip.java
index 9a06531..b49de46 100644
--- a/lang/java/reef-tang/tang/src/test/java/org/apache/reef/tang/formats/TestClassHierarchyRoundTrip.java
+++ b/lang/java/reef-tang/tang/src/test/java/org/apache/reef/tang/formats/TestClassHierarchyRoundTrip.java
@@ -30,7 +30,7 @@ import java.io.File;
 import java.io.IOException;
 
 public class TestClassHierarchyRoundTrip extends TestClassHierarchy {
-  final AvroClassHierarchySerializer serializer = new AvroClassHierarchySerializer();
+  final ClassHierarchySerializer serializer = new AvroClassHierarchySerializer();
 
   // The original ClassHierarchy
   private void setup0() {
@@ -38,14 +38,8 @@ public class TestClassHierarchyRoundTrip extends TestClassHierarchy {
     ns = Tang.Factory.getTang().getDefaultClassHierarchy();
   }
 
-  // Serializes ClassHierarchy to AvroClassHierarchy
-  private void setup1() {
-    TangImpl.reset();
-    ns = serializer.fromAvro(serializer.toAvro(ns));
-  }
-
   // Serializes ClassHierarchy to file
-  private void setup2() {
+  private void setup1() {
     TangImpl.reset();
     try {
       final File file = java.io.File.createTempFile("TangTest", "avroch");
@@ -58,7 +52,7 @@ public class TestClassHierarchyRoundTrip extends TestClassHierarchy {
   }
 
   // Serializes ClassHierarchy to TextFile
-  private void setup3() {
+  private void setup2() {
     TangImpl.reset();
     try {
       final File textFile = java.io.File.createTempFile("TangTest", "avroch");
@@ -71,7 +65,7 @@ public class TestClassHierarchyRoundTrip extends TestClassHierarchy {
   }
 
   // Serializes ClassHierarchy to byte[]
-  private void setup4() {
+  private void setup3() {
     TangImpl.reset();
     try {
       ns = serializer.fromByteArray(serializer.toByteArray(ns));
@@ -81,7 +75,7 @@ public class TestClassHierarchyRoundTrip extends TestClassHierarchy {
   }
 
   // Serializes ClassHierarchy to String
-  private void setup5() {
+  private void setup4() {
     TangImpl.reset();
     try {
       ns = serializer.fromString(serializer.toString(ns));
@@ -103,8 +97,6 @@ public class TestClassHierarchyRoundTrip extends TestClassHierarchy {
     super.testJavaString();
     setup4();
     super.testJavaString();
-    setup5();
-    super.testJavaString();
   }
 
   @Test
@@ -120,8 +112,6 @@ public class TestClassHierarchyRoundTrip extends TestClassHierarchy {
     super.testSimpleConstructors();
     setup4();
     super.testSimpleConstructors();
-    setup5();
-    super.testSimpleConstructors();
   }
 
   @Test
@@ -137,8 +127,6 @@ public class TestClassHierarchyRoundTrip extends TestClassHierarchy {
     super.testNamedParameterConstructors();
     setup4();
     super.testNamedParameterConstructors();
-    setup5();
-    super.testNamedParameterConstructors();
   }
 
   @Test
@@ -154,8 +142,6 @@ public class TestClassHierarchyRoundTrip extends TestClassHierarchy {
     super.testArray();
     setup4();
     super.testArray();
-    setup5();
-    super.testArray();
   }
 
   @Test
@@ -171,8 +157,6 @@ public class TestClassHierarchyRoundTrip extends TestClassHierarchy {
     super.testRepeatConstructorArg();
     setup4();
     super.testRepeatConstructorArg();
-    setup5();
-    super.testRepeatConstructorArg();
   }
 
   @Test
@@ -188,8 +172,6 @@ public class TestClassHierarchyRoundTrip extends TestClassHierarchy {
     super.testRepeatConstructorArgClasses();
     setup4();
     super.testRepeatConstructorArgClasses();
-    setup5();
-    super.testRepeatConstructorArgClasses();
   }
 
   @Test
@@ -205,8 +187,6 @@ public class TestClassHierarchyRoundTrip extends TestClassHierarchy {
     super.testLeafRepeatedConstructorArgClasses();
     setup4();
     super.testLeafRepeatedConstructorArgClasses();
-    setup5();
-    super.testLeafRepeatedConstructorArgClasses();
   }
 
   @Test
@@ -222,8 +202,6 @@ public class TestClassHierarchyRoundTrip extends TestClassHierarchy {
     super.testNamedRepeatConstructorArgClasses();
     setup4();
     super.testNamedRepeatConstructorArgClasses();
-    setup5();
-    super.testNamedRepeatConstructorArgClasses();
   }
 
   @Test
@@ -239,8 +217,6 @@ public class TestClassHierarchyRoundTrip extends TestClassHierarchy {
     super.testResolveDependencies();
     setup4();
     super.testResolveDependencies();
-    setup5();
-    super.testResolveDependencies();
   }
 
   @Test
@@ -256,8 +232,6 @@ public class TestClassHierarchyRoundTrip extends TestClassHierarchy {
     super.testDocumentedLocalNamedParameter();
     setup4();
     super.testDocumentedLocalNamedParameter();
-    setup5();
-    super.testDocumentedLocalNamedParameter();
   }
 
   @Test
@@ -273,8 +247,6 @@ public class TestClassHierarchyRoundTrip extends TestClassHierarchy {
     super.testNamedParameterTypeMismatch();
     setup4();
     super.testNamedParameterTypeMismatch();
-    setup5();
-    super.testNamedParameterTypeMismatch();
   }
 
   @Test
@@ -290,8 +262,6 @@ public class TestClassHierarchyRoundTrip extends TestClassHierarchy {
     super.testUnannotatedName();
     setup4();
     super.testUnannotatedName();
-    setup5();
-    super.testUnannotatedName();
   }
 
   @Test
@@ -307,8 +277,6 @@ public class TestClassHierarchyRoundTrip extends TestClassHierarchy {
     super.testAnnotatedNotName();
     setup4();
     super.testAnnotatedNotName();
-    setup5();
-    super.testAnnotatedNotName();
   }
 
   @Test
@@ -324,25 +292,6 @@ public class TestClassHierarchyRoundTrip extends TestClassHierarchy {
     super.testGenericTorture1();
     setup4();
     super.testGenericTorture1();
-    setup5();
-    super.testGenericTorture1();
-  }
-
-  @Test
-  @Override
-  public void testGenericTorture2() throws NameResolutionException {
-    setup0();
-    super.testGenericTorture2();
-    setup1();
-    super.testGenericTorture2();
-    setup2();
-    super.testGenericTorture2();
-    setup3();
-    super.testGenericTorture2();
-    setup4();
-    super.testGenericTorture2();
-    setup5();
-    super.testGenericTorture2();
   }
 
   @Test
@@ -358,8 +307,6 @@ public class TestClassHierarchyRoundTrip extends TestClassHierarchy {
     super.testGenericTorture3();
     setup4();
     super.testGenericTorture3();
-    setup5();
-    super.testGenericTorture3();
   }
 
   @Test
@@ -375,8 +322,6 @@ public class TestClassHierarchyRoundTrip extends TestClassHierarchy {
     super.testGenericTorture4();
     setup4();
     super.testGenericTorture4();
-    setup5();
-    super.testGenericTorture4();
   }
 
   @Test
@@ -392,8 +337,6 @@ public class TestClassHierarchyRoundTrip extends TestClassHierarchy {
     super.testGenericTorture5();
     setup4();
     super.testGenericTorture5();
-    setup5();
-    super.testGenericTorture5();
   }
 
   @Test
@@ -409,8 +352,6 @@ public class TestClassHierarchyRoundTrip extends TestClassHierarchy {
     super.testGenericTorture6();
     setup4();
     super.testGenericTorture6();
-    setup5();
-    super.testGenericTorture6();
   }
 
   @Test
@@ -426,8 +367,6 @@ public class TestClassHierarchyRoundTrip extends TestClassHierarchy {
     super.testGenericTorture7();
     setup4();
     super.testGenericTorture7();
-    setup5();
-    super.testGenericTorture7();
   }
 
   @Test
@@ -443,8 +382,6 @@ public class TestClassHierarchyRoundTrip extends TestClassHierarchy {
     super.testGenericTorture8();
     setup4();
     super.testGenericTorture8();
-    setup5();
-    super.testGenericTorture8();
   }
 
   @Test
@@ -460,8 +397,6 @@ public class TestClassHierarchyRoundTrip extends TestClassHierarchy {
     super.testGenericTorture9();
     setup4();
     super.testGenericTorture9();
-    setup5();
-    super.testGenericTorture9();
   }
 
   @Test
@@ -477,8 +412,6 @@ public class TestClassHierarchyRoundTrip extends TestClassHierarchy {
     super.testGenericTorture10();
     setup4();
     super.testGenericTorture10();
-    setup5();
-    super.testGenericTorture10();
   }
 
   @Test
@@ -494,8 +427,6 @@ public class TestClassHierarchyRoundTrip extends TestClassHierarchy {
     super.testGenericTorture11();
     setup4();
     super.testGenericTorture11();
-    setup5();
-    super.testGenericTorture11();
   }
 
   @Test
@@ -511,8 +442,6 @@ public class TestClassHierarchyRoundTrip extends TestClassHierarchy {
     super.testGenericTorture12();
     setup4();
     super.testGenericTorture12();
-    setup5();
-    super.testGenericTorture12();
   }
 
   @Test
@@ -528,9 +457,6 @@ public class TestClassHierarchyRoundTrip extends TestClassHierarchy {
     super.testInjectNonStaticLocalArgClass();
     setup4();
     super.testInjectNonStaticLocalArgClass();
-    setup5();
-    super.testInjectNonStaticLocalArgClass();
-
   }
 
   @Test
@@ -546,8 +472,6 @@ public class TestClassHierarchyRoundTrip extends TestClassHierarchy {
     super.testOKShortNames();
     setup4();
     super.testOKShortNames();
-    setup5();
-    super.testOKShortNames();
   }
 
   @Test
@@ -563,8 +487,6 @@ public class TestClassHierarchyRoundTrip extends TestClassHierarchy {
     super.testRoundTripInnerClassNames();
     setup4();
     super.testRoundTripInnerClassNames();
-    setup5();
-    super.testRoundTripInnerClassNames();
   }
 
   @Test
@@ -580,8 +502,6 @@ public class TestClassHierarchyRoundTrip extends TestClassHierarchy {
     super.testUnitIsInjectable();
     setup4();
     super.testUnitIsInjectable();
-    setup5();
-    super.testUnitIsInjectable();
   }
 
   @Test
@@ -597,8 +517,6 @@ public class TestClassHierarchyRoundTrip extends TestClassHierarchy {
     super.testBadUnitDecl();
     setup4();
     super.testBadUnitDecl();
-    setup5();
-    super.testBadUnitDecl();
   }
 
   @Test
@@ -614,8 +532,6 @@ public class TestClassHierarchyRoundTrip extends TestClassHierarchy {
     super.nameCantBindWrongSubclassAsDefault();
     setup4();
     super.nameCantBindWrongSubclassAsDefault();
-    setup5();
-    super.nameCantBindWrongSubclassAsDefault();
   }
 
   @Test
@@ -631,7 +547,5 @@ public class TestClassHierarchyRoundTrip extends TestClassHierarchy {
     super.ifaceCantBindWrongImplAsDefault();
     setup4();
     super.ifaceCantBindWrongImplAsDefault();
-    setup5();
-    super.ifaceCantBindWrongImplAsDefault();
   }
 }


[11/17] incubator-reef git commit: Import the missing exception and remove the redundant modifier from the public interface

Posted by yu...@apache.org.
Import the missing exception and remove the redundant modifier from the public interface


Project: http://git-wip-us.apache.org/repos/asf/incubator-reef/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-reef/commit/0e52a168
Tree: http://git-wip-us.apache.org/repos/asf/incubator-reef/tree/0e52a168
Diff: http://git-wip-us.apache.org/repos/asf/incubator-reef/diff/0e52a168

Branch: refs/heads/REEF-395
Commit: 0e52a168e270a4211a941bf44e746ea48b8f4ea8
Parents: cfd9dd3
Author: Yunseong Lee <yu...@apache.org>
Authored: Mon Jun 22 12:34:16 2015 +0900
Committer: Yunseong Lee <yu...@apache.org>
Committed: Mon Jun 22 12:34:16 2015 +0900

----------------------------------------------------------------------
 .../src/main/java/org/apache/reef/tang/ClassHierarchy.java    | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/0e52a168/lang/java/reef-tang/tang/src/main/java/org/apache/reef/tang/ClassHierarchy.java
----------------------------------------------------------------------
diff --git a/lang/java/reef-tang/tang/src/main/java/org/apache/reef/tang/ClassHierarchy.java b/lang/java/reef-tang/tang/src/main/java/org/apache/reef/tang/ClassHierarchy.java
index 8a95e1e..0a8a3e2 100644
--- a/lang/java/reef-tang/tang/src/main/java/org/apache/reef/tang/ClassHierarchy.java
+++ b/lang/java/reef-tang/tang/src/main/java/org/apache/reef/tang/ClassHierarchy.java
@@ -18,6 +18,7 @@
  */
 package org.apache.reef.tang;
 
+import org.apache.reef.tang.exceptions.ClassHierarchyException;
 import org.apache.reef.tang.exceptions.NameResolutionException;
 import org.apache.reef.tang.types.ClassNode;
 import org.apache.reef.tang.types.Node;
@@ -44,12 +45,12 @@ public interface ClassHierarchy {
    * @throws NameResolutionException If the class is not found.
    * @throws ClassHierarchyException If the class does not pass Tang's static analysis.
    */
-  public Node getNode(String fullName) throws NameResolutionException;
+  Node getNode(String fullName) throws NameResolutionException;
 
   /**
    * @return true if impl is a subclass of inter.
    */
-  public boolean isImplementation(ClassNode<?> inter, ClassNode<?> impl);
+  boolean isImplementation(ClassNode<?> inter, ClassNode<?> impl);
 
   /**
    * Merge the contents of this ClassHierarchy and the provided one into a new
@@ -64,7 +65,7 @@ public interface ClassHierarchy {
    * ClassHierarchies derived from applications written in different languages
    * cannot be merged.
    */
-  public ClassHierarchy merge(ClassHierarchy ch);
+  ClassHierarchy merge(ClassHierarchy ch);
 
   /**
    * Return a reference to the root of the ClassHierarchy.  This is needed by


[04/17] incubator-reef git commit: Define AvroClassHierarchy which implements ClassHierarchy interface

Posted by yu...@apache.org.
Define AvroClassHierarchy which implements ClassHierarchy interface


Project: http://git-wip-us.apache.org/repos/asf/incubator-reef/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-reef/commit/1d370951
Tree: http://git-wip-us.apache.org/repos/asf/incubator-reef/tree/1d370951
Diff: http://git-wip-us.apache.org/repos/asf/incubator-reef/diff/1d370951

Branch: refs/heads/REEF-395
Commit: 1d37095112b3069ea180988630ed67c66101bdac
Parents: beb1aef
Author: Yunseong Lee <yu...@apache.org>
Authored: Sat Jun 20 02:16:13 2015 +0900
Committer: Yunseong Lee <yu...@apache.org>
Committed: Sat Jun 20 02:16:13 2015 +0900

----------------------------------------------------------------------
 .../implementation/java/AvroClassHierarchy.java | 188 +++++++++++++++++++
 1 file changed, 188 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/1d370951/lang/java/reef-tang/tang/src/main/java/org/apache/reef/tang/implementation/java/AvroClassHierarchy.java
----------------------------------------------------------------------
diff --git a/lang/java/reef-tang/tang/src/main/java/org/apache/reef/tang/implementation/java/AvroClassHierarchy.java b/lang/java/reef-tang/tang/src/main/java/org/apache/reef/tang/implementation/java/AvroClassHierarchy.java
new file mode 100644
index 0000000..96ab16c
--- /dev/null
+++ b/lang/java/reef-tang/tang/src/main/java/org/apache/reef/tang/implementation/java/AvroClassHierarchy.java
@@ -0,0 +1,188 @@
+package org.apache.reef.tang.implementation.java;
+
+import org.apache.reef.tang.ClassHierarchy;
+import org.apache.reef.tang.annotations.NamedParameter;
+import org.apache.reef.tang.exceptions.NameResolutionException;
+import org.apache.reef.tang.formats.avro.*;
+import org.apache.reef.tang.implementation.types.*;
+import org.apache.reef.tang.types.*;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+
+/**
+ * Implementation of ClassHierarchy for Avro
+ */
+// TODO Should it be a ClassHierarchy?
+public class AvroClassHierarchy implements ClassHierarchy {
+  private final PackageNode namespace;
+  private final HashMap<String, Node> lookupTable = new HashMap<>();
+
+  public AvroClassHierarchy(final AvroNode root) {
+    namespace = new PackageNodeImpl();
+    if (root.getPackageNode() == null) {
+      throw new IllegalArgumentException("Expected a package node. Got: " + root);
+    }
+
+    // Register all the classes.
+    for (final AvroNode child : root.getChildren()) {
+      parseSubHierarchy(namespace, child);
+    }
+    buildLookupTable(namespace);
+    // Now, register the implementations
+    for (final AvroNode child : root.getChildren()) {
+      wireUpInheritanceRelationships(child);
+    }
+  }
+
+  private void buildLookupTable(final Node n) {
+    for(final Node child : n.getChildren()) {
+      lookupTable.put(child.getFullName(), child);
+      buildLookupTable(child);
+    }
+  }
+
+  private static ConstructorDef<?> parseConstructorDef(final AvroConstructorDef def, final boolean isInjectable) {
+    final List<ConstructorArg> args = new ArrayList<>();
+    for (final AvroConstructorArg arg : def.getConstructorArg()) {
+      final String namedParameterName = arg.getNamedParameterName() == null ? null : arg.getNamedParameterName();
+      args.add(new ConstructorArgImpl(arg.getFullArgClassName(), namedParameterName,
+              arg.getIsInjectionFuture()));
+    }
+    return new ConstructorDefImpl<>(def.getFullArgClassName(), args.toArray(new ConstructorArg[0]),
+            isInjectable);
+  }
+
+  private static void parseSubHierarchy(Node parent, AvroNode n) {
+    final Node parsed;
+    if (n.getPackageNode() != null) {
+      parsed = new PackageNodeImpl(parent, n.getName(), n.getFullName());
+    } else if (n.getNamedParameterNode() != null) {
+      final AvroNamedParameterNode np = n.getNamedParameterNode();
+      parsed = new NamedParameterNodeImpl<Object>(parent, n.getName(), n.getFullName(),
+              np.getFullArgClassName(), np.getSimpleArgClassName(), np.getIsSet(), np.getIsList(),
+              np.getDocumentation(), np.getShortName(), np.getInstanceDefault().toArray(new String[0]));
+    } else if (n.getClassNode() != null) {
+      final AvroClassNode cn = n.getClassNode();
+      final List<ConstructorDef<?>> injectableConstructors = new ArrayList<>();
+      final List<ConstructorDef<?>> allConstructors = new ArrayList<>();
+
+      for (final AvroConstructorDef injectable : cn.getInjectableConstructors()) {
+        final ConstructorDef<?> def = parseConstructorDef(injectable, true);
+        injectableConstructors.add(def);
+        allConstructors.add(def);
+      }
+      for (final AvroConstructorDef other : cn.getOtherConstructors()) {
+        final ConstructorDef<?> def = parseConstructorDef(other, false);
+        allConstructors.add(def);
+      }
+      @SuppressWarnings("unchecked")
+      final ConstructorDef<Object>[] dummy = new ConstructorDef[0];
+      final String defaultImpl = cn.getDefaultImplementation() == null ? null : cn.getDefaultImplementation();
+      parsed = new ClassNodeImpl<>(parent, n.getName(), n.getFullName(), cn.getIsUnit(),
+              cn.getIsInjectionCandidate(), cn.getIsExternalConstructor(), injectableConstructors.toArray(dummy),
+              allConstructors.toArray(dummy), defaultImpl);
+    } else {
+      throw new IllegalStateException("Bad protocol buffer: got abstract node" + n);
+    }
+    for (AvroNode child : n.getChildren()) {
+      parseSubHierarchy(parsed, child);
+    }
+  }
+
+
+  @SuppressWarnings({"rawtypes", "unchecked"})
+  private void wireUpInheritanceRelationships(final AvroNode n) {
+    if (n.getClassNode() != null) {
+      final AvroClassNode cn = n.getClassNode();
+      final ClassNode iface;
+      try {
+        iface = (ClassNode) getNode(n.getFullName());
+      } catch (NameResolutionException e) {
+        throw new IllegalStateException("When reading protocol buffer node "
+                + n.getFullName() + " does not exist.  Full record is " + n, e);
+      }
+      for (final String impl : cn.getImplFullNames()) {
+        try {
+          iface.putImpl((ClassNode) getNode(impl));
+        } catch (NameResolutionException e) {
+          throw new IllegalStateException("When reading protocol buffer node "
+                  + n + " refers to non-existent implementation:" + impl);
+        } catch (ClassCastException e) {
+          try {
+            throw new IllegalStateException(
+                    "When reading protocol buffer node " + n
+                            + " found implementation" + getNode(impl)
+                            + " which is not a ClassNode!");
+          } catch (NameResolutionException e2) {
+            throw new IllegalStateException(
+                    "Got 'cant happen' exception when producing error message for "
+                            + e);
+          }
+        }
+      }
+    }
+
+    for (final AvroNode child : n.getChildren()) {
+      wireUpInheritanceRelationships(child);
+    }
+  }
+
+  @Override
+  public Node getNode(final String fullName) throws NameResolutionException {
+    final Node matchedNode = lookupTable.get(fullName);
+    if (matchedNode != null) {
+      return matchedNode;
+    } else {
+      throw new NameResolutionException(fullName, "");
+    }
+  }
+
+  @Override
+  public boolean isImplementation(final ClassNode<?> inter, final ClassNode<?> impl) {
+    return impl.isImplementationOf(inter);
+  }
+
+  @Override
+  public ClassHierarchy merge(ClassHierarchy ch) {
+    if (this == ch) {
+      return this;
+    }
+    if (!(ch instanceof AvroClassHierarchy)) {
+      throw new UnsupportedOperationException(
+              "Cannot merge with class hierarchies of type: " + ch.getClass().getName());
+    }
+
+    final AvroClassHierarchy ach = (AvroClassHierarchy) ch;
+    for (final String key : ach.lookupTable.keySet()) {
+      if (!this.lookupTable.containsKey(key)) {
+        this.lookupTable.put(key, ach.lookupTable.get(key));
+        // TODO : Does the order not matter here?
+      }
+    }
+
+    for (final Node n : ch.getNamespace().getChildren()) {
+      if (!this.namespace.contains(n.getFullName())) {
+        if (n instanceof NamedParameter) {
+          final NamedParameterNode np = (NamedParameterNode) n;
+          new NamedParameterNodeImpl<>(this.namespace, np.getName(), np.getFullName(), np.getFullArgName(),
+                  np.getSimpleArgName(), np.isSet(), np.isList(), np.getDocumentation(), np.getShortName(),
+                  np.getDefaultInstanceAsStrings());
+        } else if (n instanceof ClassNode) {
+          final ClassNode cn = (ClassNode) n;
+          new ClassNodeImpl<>(namespace, cn.getName(), cn.getFullName(), cn.isUnit(), cn.isInjectionCandidate(),
+                  cn.isExternalConstructor(), cn.getInjectableConstructors(), cn.getAllConstructors(),
+                  cn.getDefaultImplementation());
+        }
+        // TODO What if n is the package node?
+      }
+    }
+    return this;
+  }
+
+  @Override
+  public Node getNamespace() {
+    return namespace;
+  }
+}


[08/17] incubator-reef git commit: Update the javadoc comments

Posted by yu...@apache.org.
Update the javadoc comments


Project: http://git-wip-us.apache.org/repos/asf/incubator-reef/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-reef/commit/09ea37b4
Tree: http://git-wip-us.apache.org/repos/asf/incubator-reef/tree/09ea37b4
Diff: http://git-wip-us.apache.org/repos/asf/incubator-reef/diff/09ea37b4

Branch: refs/heads/REEF-395
Commit: 09ea37b48eb622113b622abef509105c718668b5
Parents: 8378efb
Author: Yunseong Lee <yu...@apache.org>
Authored: Sun Jun 21 16:58:16 2015 +0900
Committer: Yunseong Lee <yu...@apache.org>
Committed: Sun Jun 21 16:58:16 2015 +0900

----------------------------------------------------------------------
 .../tang/formats/ClassHierarchySerializer.java  | 52 ++++++++++++++++----
 1 file changed, 42 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/09ea37b4/lang/java/reef-tang/tang/src/main/java/org/apache/reef/tang/formats/ClassHierarchySerializer.java
----------------------------------------------------------------------
diff --git a/lang/java/reef-tang/tang/src/main/java/org/apache/reef/tang/formats/ClassHierarchySerializer.java b/lang/java/reef-tang/tang/src/main/java/org/apache/reef/tang/formats/ClassHierarchySerializer.java
index bf3918b..c5a77ae 100644
--- a/lang/java/reef-tang/tang/src/main/java/org/apache/reef/tang/formats/ClassHierarchySerializer.java
+++ b/lang/java/reef-tang/tang/src/main/java/org/apache/reef/tang/formats/ClassHierarchySerializer.java
@@ -7,37 +7,69 @@ import org.apache.reef.tang.formats.avro.AvroNode;
 import java.io.*;
 
 /**
- *
+ * A base interface for ClassHierarchy serializers.
  */
 @DefaultImplementation(org.apache.reef.tang.formats.AvroClassHierarchySerializer.class)
 public interface ClassHierarchySerializer {
   /**
-   * serialize a class hierarchy into a file.
+   * Writes a ClassHierarchy into a file.
    *
-   * @param file
-   * @param classHierarchy
-   * @throws IOException
+   * @param classHierarchy the ClassHierarchy to store
+   * @param file the file to store the ClassHierarchy
+   * @throws IOException if there is an error in the process
    */
   void toFile(final ClassHierarchy classHierarchy, final File file) throws IOException;
 
+  /**
+   * Writes a ClassHierarchy into a text file.
+   *
+   * @param classHierarchy the ClassHierarchy to store
+   * @param file the text file to store the ClassHierarchy
+   * @throws IOException if there is an error in the process
+   */
   void toTextFile(final ClassHierarchy classHierarchy, final File file) throws IOException;
 
+   /**
+   * Serializes a ClassHierarchy as a byte[].
+   *
+   * @param classHierarchy the ClassHierarchy to store
+   * @throws IOException if there is an error in the process
+   */
   byte[] toByteArray(final ClassHierarchy classHierarchy) throws IOException;
 
+  /**
+   * Serializes a ClassHierarchy as a String.
+   * @param classHierarchy
+   * @param classHierarchy the ClassHierarchy to store
+   * @throws IOException if there is an error in the process
+   */
   String toString(final ClassHierarchy classHierarchy) throws IOException;
 
   /**
-   * Deserialize a class hierarchy from a file. The file can be generated from either Java or C#
-   *
-   * @param file
-   * @return
-   * @throws IOException
+   * Loads a ClassHierarchy from a file created with toFile().
+   * @param file the File to read from
+   * @throws IOException if the File can't be read or parsed
    */
   ClassHierarchy fromFile(final File file) throws IOException;
 
+  /**
+   * Loads a ClassHierarchy from a text file created with toTextFile().
+   * @param file the File to read from
+   * @throws IOException if the File can't be read or parsed
+   */
   ClassHierarchy fromTextFile(final File file) throws IOException;
 
+  /**
+   * Deserializes a ClassHierarchy from a byte[] created with toByteArray().
+   * @param theBytes the byte[] to deserialize
+   * @throws IOException if the byte[] can't be read or parsed
+   */
   ClassHierarchy fromByteArray(final byte[] theBytes) throws IOException;
 
+  /**
+   * Deserializes a ClassHierarchy from a String created with toString().
+   * @param theString the String to deserialize
+   * @throws IOException if the String can't be read or parsed
+   */
   ClassHierarchy fromString(final String theString) throws IOException;
 }


[03/17] incubator-reef git commit: Assign null for the optional values which are not set

Posted by yu...@apache.org.
Assign null for the optional values which are not set


Project: http://git-wip-us.apache.org/repos/asf/incubator-reef/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-reef/commit/beb1aefa
Tree: http://git-wip-us.apache.org/repos/asf/incubator-reef/tree/beb1aefa
Diff: http://git-wip-us.apache.org/repos/asf/incubator-reef/diff/beb1aefa

Branch: refs/heads/REEF-395
Commit: beb1aefa8cc480dde6bf27f86d38e14bcfe07ed3
Parents: f48df1b
Author: Yunseong Lee <yu...@apache.org>
Authored: Sat Jun 20 00:11:12 2015 +0900
Committer: Yunseong Lee <yu...@apache.org>
Committed: Sat Jun 20 00:11:12 2015 +0900

----------------------------------------------------------------------
 .../reef-tang/tang/src/main/avro/class_hierarchy.avsc | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/beb1aefa/lang/java/reef-tang/tang/src/main/avro/class_hierarchy.avsc
----------------------------------------------------------------------
diff --git a/lang/java/reef-tang/tang/src/main/avro/class_hierarchy.avsc b/lang/java/reef-tang/tang/src/main/avro/class_hierarchy.avsc
index 198c70d..058f847 100644
--- a/lang/java/reef-tang/tang/src/main/avro/class_hierarchy.avsc
+++ b/lang/java/reef-tang/tang/src/main/avro/class_hierarchy.avsc
@@ -23,7 +23,7 @@
     "name":"AvroConstructorArg",
     "fields":[
         {"name":"fullArgClassName", "type":"string"},
-        {"name":"namedParameterName", "type":"string"},
+        {"name":"namedParameterName", "type":["null", "string"], "default":null},
         {"name":"isInjectionFuture", "type":"boolean"}
     ]
 },
@@ -45,8 +45,8 @@
         {"name":"fullArgClassName", "type":"string"},
         {"name":"isSet", "type":"boolean"},
         {"name":"isList", "type":"boolean"},
-        {"name":"documentation", "type":["null", "string"]},
-        {"name":"shortName", "type":["null", "string"]},
+        {"name":"documentation", "type":["null", "string"], "default":null},
+        {"name":"shortName", "type":["null", "string"], "default":null},
         {"name":"instanceDefault", "type":{"type":"array","items":"string"}}
     ]
 },
@@ -61,7 +61,7 @@
         {"name":"injectableConstructors", "type":{"type":"array", "items":"AvroConstructorDef"}},
         {"name":"otherConstructors", "type":{"type":"array", "items":"AvroConstructorDef"}},
         {"name":"implFullNames", "type":{"type":"array", "items":"string"}},
-        {"name":"defaultImplementation", "type":["null", "string"]}
+        {"name":"defaultImplementation", "type":["null", "string"], "default":null}
     ]
 },
 /*
@@ -101,9 +101,9 @@
          * Exactly one of the next three fields must be defined. See below for
          * their documentation.
          */
-        {"name":"classNode", "type":["null", "AvroClassNode"]},
-        {"name":"namedParameterNode", "type":["null", "AvroNamedParameterNode"]},
-        {"name":"packageNode", "type":["null", "AvroPackageNode"]},
+        {"name":"classNode", "type":["null", "AvroClassNode"], "default":null},
+        {"name":"namedParameterNode", "type":["null", "AvroNamedParameterNode"], "default":null},
+        {"name":"packageNode", "type":["null", "AvroPackageNode"], "default":null},
 
         /*
            Class hierarchy nodes are arranged in a tree that mirrors the language


[02/17] incubator-reef git commit: Change the existing code to use String in place of CharSequence as well

Posted by yu...@apache.org.
Change the existing code to use String in place of CharSequence as well


Project: http://git-wip-us.apache.org/repos/asf/incubator-reef/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-reef/commit/f48df1b4
Tree: http://git-wip-us.apache.org/repos/asf/incubator-reef/tree/f48df1b4
Diff: http://git-wip-us.apache.org/repos/asf/incubator-reef/diff/f48df1b4

Branch: refs/heads/REEF-395
Commit: f48df1b45a5e209fab3ebd0fd50df90af99e24aa
Parents: 7d4baf0
Author: Yunseong Lee <yu...@apache.org>
Authored: Sat Jun 20 00:07:53 2015 +0900
Committer: Yunseong Lee <yu...@apache.org>
Committed: Sat Jun 20 00:07:53 2015 +0900

----------------------------------------------------------------------
 .../io/network/naming/serialization/NamingLookupRequestCodec.java  | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/f48df1b4/lang/java/reef-io/src/main/java/org/apache/reef/io/network/naming/serialization/NamingLookupRequestCodec.java
----------------------------------------------------------------------
diff --git a/lang/java/reef-io/src/main/java/org/apache/reef/io/network/naming/serialization/NamingLookupRequestCodec.java b/lang/java/reef-io/src/main/java/org/apache/reef/io/network/naming/serialization/NamingLookupRequestCodec.java
index da8b47a..b7261b4 100644
--- a/lang/java/reef-io/src/main/java/org/apache/reef/io/network/naming/serialization/NamingLookupRequestCodec.java
+++ b/lang/java/reef-io/src/main/java/org/apache/reef/io/network/naming/serialization/NamingLookupRequestCodec.java
@@ -52,7 +52,7 @@ public final class NamingLookupRequestCodec implements Codec<NamingLookupRequest
    */
   @Override
   public byte[] encode(final NamingLookupRequest obj) {
-    final List<CharSequence> ids = new ArrayList<>();
+    final List<String> ids = new ArrayList<>();
     for (final Identifier id : obj.getIdentifiers()) {
       ids.add(id.toString());
     }


[07/17] incubator-reef git commit: Rewrite and reorder the methods

Posted by yu...@apache.org.
Rewrite and reorder the methods


Project: http://git-wip-us.apache.org/repos/asf/incubator-reef/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-reef/commit/8378efb5
Tree: http://git-wip-us.apache.org/repos/asf/incubator-reef/tree/8378efb5
Diff: http://git-wip-us.apache.org/repos/asf/incubator-reef/diff/8378efb5

Branch: refs/heads/REEF-395
Commit: 8378efb5685216769f92b8a76f9a946dca66edef
Parents: 848b5bb
Author: Yunseong Lee <yu...@apache.org>
Authored: Sun Jun 21 16:47:46 2015 +0900
Committer: Yunseong Lee <yu...@apache.org>
Committed: Sun Jun 21 16:47:46 2015 +0900

----------------------------------------------------------------------
 .../formats/AvroClassHierarchySerializer.java   | 202 +++++++++----------
 1 file changed, 99 insertions(+), 103 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/8378efb5/lang/java/reef-tang/tang/src/main/java/org/apache/reef/tang/formats/AvroClassHierarchySerializer.java
----------------------------------------------------------------------
diff --git a/lang/java/reef-tang/tang/src/main/java/org/apache/reef/tang/formats/AvroClassHierarchySerializer.java b/lang/java/reef-tang/tang/src/main/java/org/apache/reef/tang/formats/AvroClassHierarchySerializer.java
index e4f52cf..282314e 100644
--- a/lang/java/reef-tang/tang/src/main/java/org/apache/reef/tang/formats/AvroClassHierarchySerializer.java
+++ b/lang/java/reef-tang/tang/src/main/java/org/apache/reef/tang/formats/AvroClassHierarchySerializer.java
@@ -34,6 +34,53 @@ public class AvroClassHierarchySerializer implements ClassHierarchySerializer {
   public AvroClassHierarchySerializer() {
   }
 
+  private static ClassHierarchy fromAvro(final AvroNode n) {
+    return new AvroClassHierarchy(n);
+  }
+
+  private static AvroNode toAvro(final ClassHierarchy ch) {
+    return newAvroNode(ch.getNamespace());
+  }
+
+  private static AvroNode newAvroNode(final Node n) {
+    final List<AvroNode> children = new ArrayList<>();
+    for (final Node child : n.getChildren()) {
+      children.add(newAvroNode(child));
+    }
+    if (n instanceof ClassNode) {
+      final ClassNode<?> cn = (ClassNode<?>) n;
+      final ConstructorDef<?>[] injectable = cn.getInjectableConstructors();
+      final ConstructorDef<?>[] all = cn.getAllConstructors();
+      final List<ConstructorDef<?>> others = new ArrayList<>(Arrays.asList(all));
+      others.removeAll(Arrays.asList(injectable));
+
+      final List<AvroConstructorDef> injectableConstructors = new ArrayList<>();
+      for (final ConstructorDef<?> inj : injectable) {
+        injectableConstructors.add(newConstructorDef(inj));
+      }
+      final List<AvroConstructorDef> otherConstructors = new ArrayList<>();
+      for (final ConstructorDef<?> other : others) {
+        otherConstructors.add(newConstructorDef(other));
+      }
+      final List<String> implFullNames = new ArrayList<>();
+      for (final ClassNode<?> impl : cn.getKnownImplementations()) {
+        implFullNames.add(impl.getFullName());
+      }
+      return newClassNode(n.getName(), n.getFullName(), cn.isInjectionCandidate(), cn.isExternalConstructor(),
+              cn.isUnit(), injectableConstructors, otherConstructors, implFullNames, cn.getDefaultImplementation(),
+              children);
+    } else if (n instanceof NamedParameterNode) {
+      final NamedParameterNode<?> np = (NamedParameterNode<?>) n;
+      return newNamedParameterNode(np.getName(), np.getFullName(), np.getSimpleArgName(), np.getFullArgName(),
+              np.isSet(), np.isList(), np.getDocumentation(), np.getShortName(),
+              Arrays.asList(np.getDefaultInstanceAsStrings()), children);
+    } else if (n instanceof PackageNode) {
+      return newPackageNode(n.getName(), n.getFullName(), children);
+    } else {
+      throw new IllegalStateException("Encountered unknown type of Node: " + n);
+    }
+  }
+
   private static AvroNode newClassNode(final String name,
                                        final String fullName,
                                        final boolean isInjectionCandidate,
@@ -65,23 +112,23 @@ public class AvroClassHierarchySerializer implements ClassHierarchySerializer {
                                                 final String fullArgClassName,
                                                 final boolean isSet,
                                                 final boolean isList,
-                                                final String documentation, // can be null
-                                                final String shortName, // can be null
-                                                final List<String> instanceDefault, // can be null
+                                                final String documentation,
+                                                final String shortName,
+                                                final List<String> instanceDefault,
                                                 final List<AvroNode> children) {
-    final AvroNamedParameterNode.Builder namedParameterNodeBuilder = AvroNamedParameterNode.newBuilder()
-            .setSimpleArgClassName(simpleArgClassName)
-            .setFullArgClassName(fullArgClassName)
-            .setIsSet(isSet)
-            .setIsList(isList);
-      namedParameterNodeBuilder.setDocumentation(documentation);
-      namedParameterNodeBuilder.setShortName(shortName);
-      namedParameterNodeBuilder.setInstanceDefault(instanceDefault);
 
     return AvroNode.newBuilder()
             .setName(name)
             .setFullName(fullName)
-            .setNamedParameterNode(namedParameterNodeBuilder.build())
+            .setNamedParameterNode(AvroNamedParameterNode.newBuilder()
+                    .setSimpleArgClassName(simpleArgClassName)
+                    .setFullArgClassName(fullArgClassName)
+                    .setIsSet(isSet)
+                    .setIsList(isList)
+                    .setDocumentation(documentation)
+                    .setShortName(shortName)
+                    .setInstanceDefault(instanceDefault)
+                    .build())
             .setChildren(children).build();
   }
 
@@ -93,141 +140,77 @@ public class AvroClassHierarchySerializer implements ClassHierarchySerializer {
             .setName(name).setFullName(fullName).setChildren(children).build();
   }
 
-  private static AvroConstructorDef newConstructorDef(
-          String fullClassName, List<AvroConstructorArg> args) {
-    return AvroConstructorDef.newBuilder()
-            .setFullArgClassName(fullClassName).setConstructorArg(args).build();
-  }
-
   private static AvroConstructorArg newConstructorArg(final String fullArgClassName,
                                                       final String namedParameterName,
                                                       final boolean isFuture) {
-    AvroConstructorArg.Builder builder = AvroConstructorArg.newBuilder()
+    return AvroConstructorArg.newBuilder()
             .setFullArgClassName(fullArgClassName)
-            .setIsInjectionFuture(isFuture);
-    if (namedParameterName != null) {
-      builder.setNamedParameterName(namedParameterName).build();
-    }
-    return builder.build();
+            .setIsInjectionFuture(isFuture)
+            .setNamedParameterName(namedParameterName).build();
   }
 
-  private static AvroConstructorDef serializeConstructorDef(final ConstructorDef<?> def) {
+  private static AvroConstructorDef newConstructorDef(final ConstructorDef<?> def) {
     final List<AvroConstructorArg> args = new ArrayList<>();
-    for (ConstructorArg arg : def.getArgs()) {
+    for (final ConstructorArg arg : def.getArgs()) {
       args.add(newConstructorArg(arg.getType(), arg.getNamedParameterName(), arg.isInjectionFuture()));
     }
-    return newConstructorDef(def.getClassName(), args);
-  }
-
-  private static AvroNode toAvroNode(final Node n) {
-    final List<AvroNode> children = new ArrayList<>();
-    for (final Node child : n.getChildren()) {
-      children.add(toAvroNode(child));
-    }
-    if (n instanceof ClassNode) {
-      final ClassNode<?> cn = (ClassNode<?>) n;
-      final ConstructorDef<?>[] injectable = cn.getInjectableConstructors();
-      final ConstructorDef<?>[] all = cn.getAllConstructors();
-      final List<ConstructorDef<?>> others = new ArrayList<>(Arrays.asList(all));
-      others.removeAll(Arrays.asList(injectable));
-
-      final List<AvroConstructorDef> injectableConstructors = new ArrayList<>();
-      for (final ConstructorDef<?> inj : injectable) {
-        injectableConstructors.add(serializeConstructorDef(inj));
-      }
-      final List<AvroConstructorDef> otherConstructors = new ArrayList<>();
-      for (final ConstructorDef<?> other : others) {
-        otherConstructors.add(serializeConstructorDef(other));
-      }
-      final List<String> implFullNames = new ArrayList<>();
-      for (final ClassNode<?> impl : cn.getKnownImplementations()) {
-        implFullNames.add(impl.getFullName());
-      }
-      return newClassNode(n.getName(), n.getFullName(), cn.isInjectionCandidate(), cn.isExternalConstructor(),
-              cn.isUnit(), injectableConstructors, otherConstructors, implFullNames, cn.getDefaultImplementation(), children);
-    } else if (n instanceof NamedParameterNode) {
-      final NamedParameterNode<?> np = (NamedParameterNode<?>) n;
-      return newNamedParameterNode(np.getName(), np.getFullName(), np.getSimpleArgName(), np.getFullArgName(),
-              np.isSet(), np.isList(), np.getDocumentation(), np.getShortName(),
-              Arrays.asList(np.getDefaultInstanceAsStrings()), children);
-    } else if (n instanceof PackageNode) {
-      return newPackageNode(n.getName(), n.getFullName(), children);
-    } else {
-      throw new IllegalStateException("Encountered unknown type of Node: " + n);
-    }
-  }
-
-  public AvroClassHierarchy toAvro(final ClassHierarchy classHierarchy) {
-    return new AvroClassHierarchy(toAvroNode(classHierarchy.getNamespace()));
+    return AvroConstructorDef.newBuilder()
+            .setFullArgClassName(def.getClassName())
+            .setConstructorArg(args).build();
   }
 
   @Override
   public void toFile(final ClassHierarchy classHierarchy, final File file) throws IOException {
-    final AvroNode avroNode = toAvroNode(classHierarchy.getNamespace());
+    final AvroNode avroNode = toAvro(classHierarchy);
     final DatumWriter<AvroNode> avroNodeWriter = new SpecificDatumWriter<>(AvroNode.class);
-    try (DataFileWriter<AvroNode> dataFileWriter = new DataFileWriter<>(avroNodeWriter)) {
+    try (final DataFileWriter<AvroNode> dataFileWriter = new DataFileWriter<>(avroNodeWriter)) {
       dataFileWriter.create(avroNode.getSchema(), file);
       dataFileWriter.append(avroNode);
     }
   }
 
   @Override
-  public void toTextFile(ClassHierarchy classHierarchy, File file) throws IOException {
-    try (final Writer w = new FileWriter(file)) {
-      w.write(this.toString(classHierarchy));
-    }
-  }
-
-  @Override
   public byte[] toByteArray(final ClassHierarchy classHierarchy) throws IOException {
     final DatumWriter<AvroNode> requestWriter = new SpecificDatumWriter<>(AvroNode.class);
     try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) {
       final BinaryEncoder encoder = EncoderFactory.get().binaryEncoder(out, null);
-      // TODO Should be a better way
-      requestWriter.write(toAvroNode(classHierarchy.getNamespace()), encoder);
+      requestWriter.write(toAvro(classHierarchy), encoder);
       encoder.flush();
       return out.toByteArray();
-    } catch (final IOException e) {
+    } catch (IOException e) {
       throw new RuntimeException(e);
     }
   }
 
-  // TODO Improve the interface?
   @Override
-  public String toString(ClassHierarchy classHierarchy) throws IOException {
+  public String toString(final ClassHierarchy classHierarchy) throws IOException {
     final DatumWriter<AvroNode> configurationWriter = new SpecificDatumWriter<>(AvroNode.class);
     try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) {
       final JsonEncoder encoder = EncoderFactory.get().jsonEncoder(AvroConfiguration.SCHEMA$, out);
-      configurationWriter.write(toAvroNode(classHierarchy.getNamespace()), encoder);
+      configurationWriter.write(toAvro(classHierarchy), encoder);
       encoder.flush();
       out.flush();
       return out.toString(JSON_CHARSET);
-    } catch (final IOException e) {
+    } catch (IOException e) {
       throw new RuntimeException(e);
     }
   }
 
   @Override
+  public void toTextFile(ClassHierarchy classHierarchy, File file) throws IOException {
+    try (final Writer w = new FileWriter(file)) {
+      w.write(this.toString(classHierarchy));
+    }
+  }
+
+  @Override
   public ClassHierarchy fromFile(final File file) throws IOException {
     final AvroNode avroNode;
     try (final DataFileReader<AvroNode> dataFileReader =
                  new DataFileReader<>(file, new SpecificDatumReader<>(AvroNode.class))) {
       avroNode = dataFileReader.next();
     }
-    return new AvroClassHierarchy(avroNode);
-  }
-
-  @Override
-  public ClassHierarchy fromTextFile(final File file) throws IOException {
-    final StringBuilder stringBuilder = new StringBuilder();
-    try (final BufferedReader reader = new BufferedReader(new FileReader(file))) {
-      String line = reader.readLine();
-      while (line != null) {
-        stringBuilder.append(line);
-        line = reader.readLine();
-      }
-    }
-    return fromString(stringBuilder.toString());
+    return fromAvro(avroNode);
   }
 
   @Override
@@ -235,8 +218,8 @@ public class AvroClassHierarchySerializer implements ClassHierarchySerializer {
     try {
       final BinaryDecoder decoder = DecoderFactory.get().binaryDecoder(theBytes, null);
       final SpecificDatumReader<AvroNode> reader = new SpecificDatumReader<>(AvroNode.class);
-      return new AvroClassHierarchy(reader.read(null, decoder));
-    } catch (final IOException e) {
+      return fromAvro(reader.read(null, decoder));
+    } catch (IOException e) {
       throw new RuntimeException(e);
     }
   }
@@ -246,9 +229,22 @@ public class AvroClassHierarchySerializer implements ClassHierarchySerializer {
     try {
       final JsonDecoder decoder = DecoderFactory.get().jsonDecoder(AvroNode.getClassSchema(), theString);
       final SpecificDatumReader<AvroNode> reader = new SpecificDatumReader<>(AvroNode.class);
-      return new AvroClassHierarchy(reader.read(null, decoder));
-    } catch (final IOException e) {
+      return fromAvro(reader.read(null, decoder));
+    } catch (IOException e) {
       throw new RuntimeException(e);
     }
   }
+
+  @Override
+  public ClassHierarchy fromTextFile(final File file) throws IOException {
+    final StringBuilder stringBuilder = new StringBuilder();
+    try (final BufferedReader reader = new BufferedReader(new FileReader(file))) {
+      String line = reader.readLine();
+      while (line != null) {
+        stringBuilder.append(line);
+        line = reader.readLine();
+      }
+    }
+    return fromString(stringBuilder.toString());
+  }
 }


[10/17] incubator-reef git commit: Move the round trip test for ClassHierarchy to another package

Posted by yu...@apache.org.
Move the round trip test for ClassHierarchy to another package


Project: http://git-wip-us.apache.org/repos/asf/incubator-reef/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-reef/commit/cfd9dd33
Tree: http://git-wip-us.apache.org/repos/asf/incubator-reef/tree/cfd9dd33
Diff: http://git-wip-us.apache.org/repos/asf/incubator-reef/diff/cfd9dd33

Branch: refs/heads/REEF-395
Commit: cfd9dd3312d123aa74cea631950e7ef9c5556974
Parents: 01d0dff
Author: Yunseong Lee <yu...@apache.org>
Authored: Mon Jun 22 12:20:43 2015 +0900
Committer: Yunseong Lee <yu...@apache.org>
Committed: Mon Jun 22 12:20:43 2015 +0900

----------------------------------------------------------------------
 .../formats/TestClassHierarchyRoundTrip.java    | 407 +++++++++++++++++++
 .../protobuf/TestClassHierarchyRoundTrip.java   | 401 ------------------
 2 files changed, 407 insertions(+), 401 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/cfd9dd33/lang/java/reef-tang/tang/src/test/java/org/apache/reef/tang/formats/TestClassHierarchyRoundTrip.java
----------------------------------------------------------------------
diff --git a/lang/java/reef-tang/tang/src/test/java/org/apache/reef/tang/formats/TestClassHierarchyRoundTrip.java b/lang/java/reef-tang/tang/src/test/java/org/apache/reef/tang/formats/TestClassHierarchyRoundTrip.java
new file mode 100644
index 0000000..558ba0f
--- /dev/null
+++ b/lang/java/reef-tang/tang/src/test/java/org/apache/reef/tang/formats/TestClassHierarchyRoundTrip.java
@@ -0,0 +1,407 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.reef.tang.formats;
+
+import org.apache.reef.tang.Tang;
+import org.apache.reef.tang.exceptions.InjectionException;
+import org.apache.reef.tang.exceptions.NameResolutionException;
+import org.apache.reef.tang.implementation.TangImpl;
+import org.apache.reef.tang.implementation.TestClassHierarchy;
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.io.File;
+import java.io.IOException;
+
+public class TestClassHierarchyRoundTrip extends TestClassHierarchy {
+
+  private void setup1() {
+    TangImpl.reset();
+    ns = Tang.Factory.getTang().getDefaultClassHierarchy();
+  }
+
+  private void setup2() {
+    TangImpl.reset();
+    final AvroClassHierarchySerializer serializer = new AvroClassHierarchySerializer();
+    try {
+      ns = serializer.fromString(serializer.toString(ns));
+    } catch (IOException e) {
+      Assert.fail(String.format("IOException when serialize/deserialize the string created by Avro", e));
+    }
+  }
+
+  private void setup3() {
+    TangImpl.reset();
+    try {
+      final File file = java.io.File.createTempFile("testAvro", "tmp");
+      final ClassHierarchySerializer serializer = new AvroClassHierarchySerializer();
+      serializer.toFile(ns, file);
+      ns = serializer.fromFile(file);
+      file.delete();
+    } catch (IOException e) {
+      Assert.fail(String.format("IOException when serialize/deserialize the file created by Avro", e));
+    }
+  }
+
+  @Test
+  @Override
+  public void testJavaString() throws NameResolutionException {
+    setup1();
+    super.testJavaString();
+    setup2();
+    super.testJavaString();
+    setup3();
+    super.testJavaString();
+  }
+
+  @Test
+  @Override
+  public void testSimpleConstructors() throws NameResolutionException {
+    setup1();
+    super.testSimpleConstructors();
+    setup2();
+    super.testSimpleConstructors();
+    setup3();
+    super.testSimpleConstructors();
+  }
+
+  @Test
+  @Override
+  public void testNamedParameterConstructors() throws NameResolutionException {
+    setup1();
+    super.testNamedParameterConstructors();
+    setup2();
+    super.testNamedParameterConstructors();
+    setup3();
+    super.testNamedParameterConstructors();
+  }
+
+  @Test
+  @Override
+  public void testArray() throws NameResolutionException {
+    setup1();
+    super.testArray();
+    setup2();
+    super.testArray();
+    setup3();
+    super.testArray();
+  }
+
+  @Test
+  @Override
+  public void testRepeatConstructorArg() throws NameResolutionException {
+    setup1();
+    super.testRepeatConstructorArg();
+    setup2();
+    super.testRepeatConstructorArg();
+    setup3();
+    super.testRepeatConstructorArg();
+  }
+
+  @Test
+  @Override
+  public void testRepeatConstructorArgClasses() throws NameResolutionException {
+    setup1();
+    super.testRepeatConstructorArgClasses();
+    setup2();
+    super.testRepeatConstructorArgClasses();
+    setup3();
+    super.testRepeatConstructorArgClasses();
+  }
+
+  @Test
+  @Override
+  public void testLeafRepeatedConstructorArgClasses() throws NameResolutionException {
+    setup1();
+    super.testLeafRepeatedConstructorArgClasses();
+    setup2();
+    super.testLeafRepeatedConstructorArgClasses();
+    setup3();
+    super.testLeafRepeatedConstructorArgClasses();
+  }
+
+  @Test
+  @Override
+  public void testNamedRepeatConstructorArgClasses() throws NameResolutionException {
+    setup1();
+    super.testNamedRepeatConstructorArgClasses();
+    setup2();
+    super.testNamedRepeatConstructorArgClasses();
+    setup3();
+    super.testNamedRepeatConstructorArgClasses();
+  }
+
+  @Test
+  @Override
+  public void testResolveDependencies() throws NameResolutionException {
+    setup1();
+    super.testResolveDependencies();
+    setup2();
+    super.testResolveDependencies();
+    setup3();
+    super.testResolveDependencies();
+  }
+
+  @Test
+  @Override
+  public void testDocumentedLocalNamedParameter() throws NameResolutionException {
+    setup1();
+    super.testDocumentedLocalNamedParameter();
+    setup2();
+    super.testDocumentedLocalNamedParameter();
+    setup3();
+    super.testDocumentedLocalNamedParameter();
+  }
+
+  @Test
+  @Override
+  public void testNamedParameterTypeMismatch() throws NameResolutionException {
+    setup1();
+    super.testNamedParameterTypeMismatch();
+    setup2();
+    super.testNamedParameterTypeMismatch();
+    setup3();
+    super.testNamedParameterTypeMismatch();
+  }
+
+  @Test
+  @Override
+  public void testUnannotatedName() throws NameResolutionException {
+    setup1();
+    super.testUnannotatedName();
+    setup2();
+    super.testUnannotatedName();
+    setup3();
+    super.testUnannotatedName();
+  }
+
+  @Test
+  @Override
+  public void testAnnotatedNotName() throws NameResolutionException {
+    setup1();
+    super.testAnnotatedNotName();
+    setup2();
+    super.testAnnotatedNotName();
+    setup3();
+    super.testAnnotatedNotName();
+  }
+
+  @Test
+  @Override
+  public void testGenericTorture1() throws NameResolutionException {
+    setup1();
+    super.testGenericTorture1();
+    setup2();
+    super.testGenericTorture1();
+    setup3();
+    super.testGenericTorture1();
+  }
+
+  @Test
+  @Override
+  public void testGenericTorture2() throws NameResolutionException {
+    setup1();
+    super.testGenericTorture2();
+    setup2();
+    super.testGenericTorture2();
+    setup3();
+    super.testGenericTorture2();
+  }
+
+  @Test
+  @Override
+  public void testGenericTorture3() throws NameResolutionException {
+    setup1();
+    super.testGenericTorture3();
+    setup2();
+    super.testGenericTorture3();
+    setup3();
+    super.testGenericTorture3();
+  }
+
+  @Test
+  @Override
+  public void testGenericTorture4() throws NameResolutionException {
+    setup1();
+    super.testGenericTorture4();
+    setup2();
+    super.testGenericTorture4();
+    setup3();
+    super.testGenericTorture4();
+  }
+
+  @Test
+  @Override
+  public void testGenericTorture5() throws NameResolutionException {
+    setup1();
+    super.testGenericTorture5();
+    setup2();
+    super.testGenericTorture5();
+    setup3();
+    super.testGenericTorture5();
+  }
+
+  @Test
+  @Override
+  public void testGenericTorture6() throws NameResolutionException {
+    setup1();
+    super.testGenericTorture6();
+    setup2();
+    super.testGenericTorture6();
+    setup3();
+    super.testGenericTorture6();
+  }
+
+  @Test
+  @Override
+  public void testGenericTorture7() throws NameResolutionException {
+    setup1();
+    super.testGenericTorture7();
+    setup2();
+    super.testGenericTorture7();
+    setup3();
+    super.testGenericTorture7();
+  }
+
+  @Test
+  @Override
+  public void testGenericTorture8() throws NameResolutionException {
+    setup1();
+    super.testGenericTorture8();
+    setup2();
+    super.testGenericTorture8();
+    setup3();
+    super.testGenericTorture8();
+  }
+
+  @Test
+  @Override
+  public void testGenericTorture9() throws NameResolutionException {
+    setup1();
+    super.testGenericTorture9();
+    setup2();
+    super.testGenericTorture9();
+    setup3();
+    super.testGenericTorture9();
+  }
+
+  @Test
+  @Override
+  public void testGenericTorture10() throws NameResolutionException {
+    setup1();
+    super.testGenericTorture10();
+    setup2();
+    super.testGenericTorture10();
+  }
+
+  @Test
+  @Override
+  public void testGenericTorture11() throws NameResolutionException {
+    setup1();
+    super.testGenericTorture11();
+    setup2();
+    super.testGenericTorture11();
+  }
+
+  @Test
+  @Override
+  public void testGenericTorture12() throws NameResolutionException {
+    setup1();
+    super.testGenericTorture12();
+    setup2();
+    super.testGenericTorture12();
+  }
+
+  @Test
+  @Override
+  public void testInjectNonStaticLocalArgClass() throws NameResolutionException {
+    setup1();
+    super.testInjectNonStaticLocalArgClass();
+    setup2();
+    super.testInjectNonStaticLocalArgClass();
+    setup3();
+    super.testInjectNonStaticLocalArgClass();
+  }
+
+  @Test
+  @Override
+  public void testOKShortNames() throws NameResolutionException {
+    setup1();
+    super.testOKShortNames();
+    setup2();
+    super.testOKShortNames();
+    setup3();
+    super.testOKShortNames();
+  }
+
+  @Test
+  @Override
+  public void testRoundTripInnerClassNames() throws NameResolutionException, ClassNotFoundException {
+    setup1();
+    super.testRoundTripInnerClassNames();
+    setup2();
+    super.testRoundTripInnerClassNames();
+    setup3();
+    super.testRoundTripInnerClassNames();
+  }
+
+  @Test
+  @Override
+  public void testUnitIsInjectable() throws NameResolutionException, InjectionException {
+    setup1();
+    super.testUnitIsInjectable();
+    setup2();
+    super.testUnitIsInjectable();
+    setup3();
+    super.testUnitIsInjectable();
+  }
+
+  @Test
+  @Override
+  public void testBadUnitDecl() throws NameResolutionException {
+    setup1();
+    super.testBadUnitDecl();
+    setup2();
+    super.testBadUnitDecl();
+    setup3();
+    super.testBadUnitDecl();
+  }
+
+  @Test
+  @Override
+  public void nameCantBindWrongSubclassAsDefault() throws NameResolutionException {
+    setup1();
+    super.nameCantBindWrongSubclassAsDefault();
+    setup2();
+    super.nameCantBindWrongSubclassAsDefault();
+    setup3();
+    super.nameCantBindWrongSubclassAsDefault();
+  }
+
+  @Test
+  @Override
+  public void ifaceCantBindWrongImplAsDefault() throws NameResolutionException {
+    setup1();
+    super.ifaceCantBindWrongImplAsDefault();
+    setup2();
+    super.ifaceCantBindWrongImplAsDefault();
+    setup3();
+    super.ifaceCantBindWrongImplAsDefault();
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/cfd9dd33/lang/java/reef-tang/tang/src/test/java/org/apache/reef/tang/implementation/protobuf/TestClassHierarchyRoundTrip.java
----------------------------------------------------------------------
diff --git a/lang/java/reef-tang/tang/src/test/java/org/apache/reef/tang/implementation/protobuf/TestClassHierarchyRoundTrip.java b/lang/java/reef-tang/tang/src/test/java/org/apache/reef/tang/implementation/protobuf/TestClassHierarchyRoundTrip.java
deleted file mode 100644
index d50ded5..0000000
--- a/lang/java/reef-tang/tang/src/test/java/org/apache/reef/tang/implementation/protobuf/TestClassHierarchyRoundTrip.java
+++ /dev/null
@@ -1,401 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.reef.tang.implementation.protobuf;
-
-import org.apache.reef.tang.Tang;
-import org.apache.reef.tang.exceptions.InjectionException;
-import org.apache.reef.tang.exceptions.NameResolutionException;
-import org.apache.reef.tang.implementation.TangImpl;
-import org.apache.reef.tang.implementation.TestClassHierarchy;
-import org.junit.Assert;
-import org.junit.Test;
-
-import java.io.File;
-import java.io.IOException;
-
-public class TestClassHierarchyRoundTrip extends TestClassHierarchy {
-
-  private void setup1() {
-    TangImpl.reset();
-    ns = Tang.Factory.getTang().getDefaultClassHierarchy();
-  }
-
-  private void setup2() {
-    TangImpl.reset();
-    ns = new ProtocolBufferClassHierarchy(ProtocolBufferClassHierarchy.serialize(ns));
-  }
-
-  private void setup3() {
-    TangImpl.reset();
-    try {
-      final File file = java.io.File.createTempFile("testProto", "bin");
-      ProtocolBufferClassHierarchy.serialize(file, ns);
-      ns = ProtocolBufferClassHierarchy.deserialize(file);
-      file.delete();
-    } catch (IOException e) {
-      Assert.fail(String.format("IOException when serialize/deserialize proto buffer file ", e));
-    }
-  }
-
-  @Test
-  @Override
-  public void testJavaString() throws NameResolutionException {
-    setup1();
-    super.testJavaString();
-    setup2();
-    super.testJavaString();
-    setup3();
-    super.testJavaString();
-  }
-
-  @Test
-  @Override
-  public void testSimpleConstructors() throws NameResolutionException {
-    setup1();
-    super.testSimpleConstructors();
-    setup2();
-    super.testSimpleConstructors();
-    setup3();
-    super.testSimpleConstructors();
-  }
-
-  @Test
-  @Override
-  public void testNamedParameterConstructors() throws NameResolutionException {
-    setup1();
-    super.testNamedParameterConstructors();
-    setup2();
-    super.testNamedParameterConstructors();
-    setup3();
-    super.testNamedParameterConstructors();
-  }
-
-  @Test
-  @Override
-  public void testArray() throws NameResolutionException {
-    setup1();
-    super.testArray();
-    setup2();
-    super.testArray();
-    setup3();
-    super.testArray();
-  }
-
-  @Test
-  @Override
-  public void testRepeatConstructorArg() throws NameResolutionException {
-    setup1();
-    super.testRepeatConstructorArg();
-    setup2();
-    super.testRepeatConstructorArg();
-    setup3();
-    super.testRepeatConstructorArg();
-  }
-
-  @Test
-  @Override
-  public void testRepeatConstructorArgClasses() throws NameResolutionException {
-    setup1();
-    super.testRepeatConstructorArgClasses();
-    setup2();
-    super.testRepeatConstructorArgClasses();
-    setup3();
-    super.testRepeatConstructorArgClasses();
-  }
-
-  @Test
-  @Override
-  public void testLeafRepeatedConstructorArgClasses() throws NameResolutionException {
-    setup1();
-    super.testLeafRepeatedConstructorArgClasses();
-    setup2();
-    super.testLeafRepeatedConstructorArgClasses();
-    setup3();
-    super.testLeafRepeatedConstructorArgClasses();
-  }
-
-  @Test
-  @Override
-  public void testNamedRepeatConstructorArgClasses() throws NameResolutionException {
-    setup1();
-    super.testNamedRepeatConstructorArgClasses();
-    setup2();
-    super.testNamedRepeatConstructorArgClasses();
-    setup3();
-    super.testNamedRepeatConstructorArgClasses();
-  }
-
-  @Test
-  @Override
-  public void testResolveDependencies() throws NameResolutionException {
-    setup1();
-    super.testResolveDependencies();
-    setup2();
-    super.testResolveDependencies();
-    setup3();
-    super.testResolveDependencies();
-  }
-
-  @Test
-  @Override
-  public void testDocumentedLocalNamedParameter() throws NameResolutionException {
-    setup1();
-    super.testDocumentedLocalNamedParameter();
-    setup2();
-    super.testDocumentedLocalNamedParameter();
-    setup3();
-    super.testDocumentedLocalNamedParameter();
-  }
-
-  @Test
-  @Override
-  public void testNamedParameterTypeMismatch() throws NameResolutionException {
-    setup1();
-    super.testNamedParameterTypeMismatch();
-    setup2();
-    super.testNamedParameterTypeMismatch();
-    setup3();
-    super.testNamedParameterTypeMismatch();
-  }
-
-  @Test
-  @Override
-  public void testUnannotatedName() throws NameResolutionException {
-    setup1();
-    super.testUnannotatedName();
-    setup2();
-    super.testUnannotatedName();
-    setup3();
-    super.testUnannotatedName();
-  }
-
-  @Test
-  @Override
-  public void testAnnotatedNotName() throws NameResolutionException {
-    setup1();
-    super.testAnnotatedNotName();
-    setup2();
-    super.testAnnotatedNotName();
-    setup3();
-    super.testAnnotatedNotName();
-  }
-
-  @Test
-  @Override
-  public void testGenericTorture1() throws NameResolutionException {
-    setup1();
-    super.testGenericTorture1();
-    setup2();
-    super.testGenericTorture1();
-    setup3();
-    super.testGenericTorture1();
-  }
-
-  @Test
-  @Override
-  public void testGenericTorture2() throws NameResolutionException {
-    setup1();
-    super.testGenericTorture2();
-    setup2();
-    super.testGenericTorture2();
-    setup3();
-    super.testGenericTorture2();
-  }
-
-  @Test
-  @Override
-  public void testGenericTorture3() throws NameResolutionException {
-    setup1();
-    super.testGenericTorture3();
-    setup2();
-    super.testGenericTorture3();
-    setup3();
-    super.testGenericTorture3();
-  }
-
-  @Test
-  @Override
-  public void testGenericTorture4() throws NameResolutionException {
-    setup1();
-    super.testGenericTorture4();
-    setup2();
-    super.testGenericTorture4();
-    setup3();
-    super.testGenericTorture4();
-  }
-
-  @Test
-  @Override
-  public void testGenericTorture5() throws NameResolutionException {
-    setup1();
-    super.testGenericTorture5();
-    setup2();
-    super.testGenericTorture5();
-    setup3();
-    super.testGenericTorture5();
-  }
-
-  @Test
-  @Override
-  public void testGenericTorture6() throws NameResolutionException {
-    setup1();
-    super.testGenericTorture6();
-    setup2();
-    super.testGenericTorture6();
-    setup3();
-    super.testGenericTorture6();
-  }
-
-  @Test
-  @Override
-  public void testGenericTorture7() throws NameResolutionException {
-    setup1();
-    super.testGenericTorture7();
-    setup2();
-    super.testGenericTorture7();
-    setup3();
-    super.testGenericTorture7();
-  }
-
-  @Test
-  @Override
-  public void testGenericTorture8() throws NameResolutionException {
-    setup1();
-    super.testGenericTorture8();
-    setup2();
-    super.testGenericTorture8();
-    setup3();
-    super.testGenericTorture8();
-  }
-
-  @Test
-  @Override
-  public void testGenericTorture9() throws NameResolutionException {
-    setup1();
-    super.testGenericTorture9();
-    setup2();
-    super.testGenericTorture9();
-    setup3();
-    super.testGenericTorture9();
-  }
-
-  @Test
-  @Override
-  public void testGenericTorture10() throws NameResolutionException {
-    setup1();
-    super.testGenericTorture10();
-    setup2();
-    super.testGenericTorture10();
-  }
-
-  @Test
-  @Override
-  public void testGenericTorture11() throws NameResolutionException {
-    setup1();
-    super.testGenericTorture11();
-    setup2();
-    super.testGenericTorture11();
-  }
-
-  @Test
-  @Override
-  public void testGenericTorture12() throws NameResolutionException {
-    setup1();
-    super.testGenericTorture12();
-    setup2();
-    super.testGenericTorture12();
-  }
-
-  @Test
-  @Override
-  public void testInjectNonStaticLocalArgClass() throws NameResolutionException {
-    setup1();
-    super.testInjectNonStaticLocalArgClass();
-    setup2();
-    super.testInjectNonStaticLocalArgClass();
-    setup3();
-    super.testInjectNonStaticLocalArgClass();
-  }
-
-  @Test
-  @Override
-  public void testOKShortNames() throws NameResolutionException {
-    setup1();
-    super.testOKShortNames();
-    setup2();
-    super.testOKShortNames();
-    setup3();
-    super.testOKShortNames();
-  }
-
-  @Test
-  @Override
-  public void testRoundTripInnerClassNames() throws NameResolutionException, ClassNotFoundException {
-    setup1();
-    super.testRoundTripInnerClassNames();
-    setup2();
-    super.testRoundTripInnerClassNames();
-    setup3();
-    super.testRoundTripInnerClassNames();
-  }
-
-  @Test
-  @Override
-  public void testUnitIsInjectable() throws NameResolutionException, InjectionException {
-    setup1();
-    super.testUnitIsInjectable();
-    setup2();
-    super.testUnitIsInjectable();
-    setup3();
-    super.testUnitIsInjectable();
-  }
-
-  @Test
-  @Override
-  public void testBadUnitDecl() throws NameResolutionException {
-    setup1();
-    super.testBadUnitDecl();
-    setup2();
-    super.testBadUnitDecl();
-    setup3();
-    super.testBadUnitDecl();
-  }
-
-  @Test
-  @Override
-  public void nameCantBindWrongSubclassAsDefault() throws NameResolutionException {
-    setup1();
-    super.nameCantBindWrongSubclassAsDefault();
-    setup2();
-    super.nameCantBindWrongSubclassAsDefault();
-    setup3();
-    super.nameCantBindWrongSubclassAsDefault();
-  }
-
-  @Test
-  @Override
-  public void ifaceCantBindWrongImplAsDefault() throws NameResolutionException {
-    setup1();
-    super.ifaceCantBindWrongImplAsDefault();
-    setup2();
-    super.ifaceCantBindWrongImplAsDefault();
-    setup3();
-    super.ifaceCantBindWrongImplAsDefault();
-  }
-}


[09/17] incubator-reef git commit: Fix the error from using the wrong codec

Posted by yu...@apache.org.
Fix the error from using the wrong codec


Project: http://git-wip-us.apache.org/repos/asf/incubator-reef/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-reef/commit/01d0dff8
Tree: http://git-wip-us.apache.org/repos/asf/incubator-reef/tree/01d0dff8
Diff: http://git-wip-us.apache.org/repos/asf/incubator-reef/diff/01d0dff8

Branch: refs/heads/REEF-395
Commit: 01d0dff856a783f9eced249b8ac9ae448b59457c
Parents: 09ea37b
Author: Yunseong Lee <yu...@apache.org>
Authored: Mon Jun 22 12:18:41 2015 +0900
Committer: Yunseong Lee <yu...@apache.org>
Committed: Mon Jun 22 12:18:45 2015 +0900

----------------------------------------------------------------------
 .../apache/reef/tang/formats/AvroClassHierarchySerializer.java | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/01d0dff8/lang/java/reef-tang/tang/src/main/java/org/apache/reef/tang/formats/AvroClassHierarchySerializer.java
----------------------------------------------------------------------
diff --git a/lang/java/reef-tang/tang/src/main/java/org/apache/reef/tang/formats/AvroClassHierarchySerializer.java b/lang/java/reef-tang/tang/src/main/java/org/apache/reef/tang/formats/AvroClassHierarchySerializer.java
index 282314e..f096021 100644
--- a/lang/java/reef-tang/tang/src/main/java/org/apache/reef/tang/formats/AvroClassHierarchySerializer.java
+++ b/lang/java/reef-tang/tang/src/main/java/org/apache/reef/tang/formats/AvroClassHierarchySerializer.java
@@ -184,10 +184,10 @@ public class AvroClassHierarchySerializer implements ClassHierarchySerializer {
 
   @Override
   public String toString(final ClassHierarchy classHierarchy) throws IOException {
-    final DatumWriter<AvroNode> configurationWriter = new SpecificDatumWriter<>(AvroNode.class);
+    final DatumWriter<AvroNode> classHierarchyWriter = new SpecificDatumWriter<>(AvroNode.class);
     try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) {
-      final JsonEncoder encoder = EncoderFactory.get().jsonEncoder(AvroConfiguration.SCHEMA$, out);
-      configurationWriter.write(toAvro(classHierarchy), encoder);
+      final JsonEncoder encoder = EncoderFactory.get().jsonEncoder(AvroNode.SCHEMA$, out);
+      classHierarchyWriter.write(toAvro(classHierarchy), encoder);
       encoder.flush();
       out.flush();
       return out.toString(JSON_CHARSET);


[16/17] incubator-reef git commit: Mark ProtocolBufferClassHierarchy as deprecated

Posted by yu...@apache.org.
Mark ProtocolBufferClassHierarchy as deprecated


Project: http://git-wip-us.apache.org/repos/asf/incubator-reef/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-reef/commit/a86ca062
Tree: http://git-wip-us.apache.org/repos/asf/incubator-reef/tree/a86ca062
Diff: http://git-wip-us.apache.org/repos/asf/incubator-reef/diff/a86ca062

Branch: refs/heads/REEF-395
Commit: a86ca062fb88c9329bb4a64d32cd38d0bc92afbd
Parents: fb7c026
Author: Yunseong Lee <yu...@apache.org>
Authored: Mon Jun 22 22:15:09 2015 +0900
Committer: Yunseong Lee <yu...@apache.org>
Committed: Mon Jun 22 22:15:09 2015 +0900

----------------------------------------------------------------------
 .../tang/implementation/protobuf/ProtocolBufferClassHierarchy.java  | 1 +
 1 file changed, 1 insertion(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/a86ca062/lang/java/reef-tang/tang/src/main/java/org/apache/reef/tang/implementation/protobuf/ProtocolBufferClassHierarchy.java
----------------------------------------------------------------------
diff --git a/lang/java/reef-tang/tang/src/main/java/org/apache/reef/tang/implementation/protobuf/ProtocolBufferClassHierarchy.java b/lang/java/reef-tang/tang/src/main/java/org/apache/reef/tang/implementation/protobuf/ProtocolBufferClassHierarchy.java
index 29b4939..8429c4a 100644
--- a/lang/java/reef-tang/tang/src/main/java/org/apache/reef/tang/implementation/protobuf/ProtocolBufferClassHierarchy.java
+++ b/lang/java/reef-tang/tang/src/main/java/org/apache/reef/tang/implementation/protobuf/ProtocolBufferClassHierarchy.java
@@ -31,6 +31,7 @@ import java.util.Arrays;
 import java.util.HashMap;
 import java.util.List;
 
+@Deprecated
 public class ProtocolBufferClassHierarchy implements ClassHierarchy {
 
   private static final String regex = "[\\.\\$\\+]";


[05/17] incubator-reef git commit: Add Serializer which serialize/deserialize the ClassHierarchy from/to many type of formats

Posted by yu...@apache.org.
Add Serializer which serialize/deserialize the ClassHierarchy from/to many type of formats


Project: http://git-wip-us.apache.org/repos/asf/incubator-reef/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-reef/commit/7a095daf
Tree: http://git-wip-us.apache.org/repos/asf/incubator-reef/tree/7a095daf
Diff: http://git-wip-us.apache.org/repos/asf/incubator-reef/diff/7a095daf

Branch: refs/heads/REEF-395
Commit: 7a095daf13dd8d690c16ad31ce3abba3ec3b0120
Parents: 1d37095
Author: Yunseong Lee <yu...@apache.org>
Authored: Sat Jun 20 02:16:56 2015 +0900
Committer: Yunseong Lee <yu...@apache.org>
Committed: Sat Jun 20 02:16:56 2015 +0900

----------------------------------------------------------------------
 .../formats/AvroClassHierarchySerializer.java   | 254 +++++++++++++++++++
 .../tang/formats/ClassHierarchySerializer.java  |  43 ++++
 2 files changed, 297 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/7a095daf/lang/java/reef-tang/tang/src/main/java/org/apache/reef/tang/formats/AvroClassHierarchySerializer.java
----------------------------------------------------------------------
diff --git a/lang/java/reef-tang/tang/src/main/java/org/apache/reef/tang/formats/AvroClassHierarchySerializer.java b/lang/java/reef-tang/tang/src/main/java/org/apache/reef/tang/formats/AvroClassHierarchySerializer.java
new file mode 100644
index 0000000..e4f52cf
--- /dev/null
+++ b/lang/java/reef-tang/tang/src/main/java/org/apache/reef/tang/formats/AvroClassHierarchySerializer.java
@@ -0,0 +1,254 @@
+package org.apache.reef.tang.formats;
+
+import org.apache.avro.file.DataFileReader;
+import org.apache.avro.file.DataFileWriter;
+import org.apache.avro.io.*;
+import org.apache.avro.specific.SpecificDatumReader;
+import org.apache.avro.specific.SpecificDatumWriter;
+import org.apache.reef.tang.ClassHierarchy;
+import org.apache.reef.tang.formats.avro.*;
+import org.apache.reef.tang.implementation.java.AvroClassHierarchy;
+import org.apache.reef.tang.types.*;
+
+import javax.inject.Inject;
+import java.io.*;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+/**
+ * (De-)Serializing ClassHierarchy to and from AvroClassHierarchy.
+ * <p/>
+ * This class is stateless and is therefore safe to reuse.
+ */
+public class AvroClassHierarchySerializer implements ClassHierarchySerializer {
+
+  /**
+   * The Charset used for the JSON encoding.
+   * <p/>
+   * Copied from <code>org.apache.avro.io.JsonDecoder.CHARSET</code>
+   */
+  private static final String JSON_CHARSET = "ISO-8859-1";
+
+  @Inject
+  public AvroClassHierarchySerializer() {
+  }
+
+  private static AvroNode newClassNode(final String name,
+                                       final String fullName,
+                                       final boolean isInjectionCandidate,
+                                       final boolean isExternalConstructor,
+                                       final boolean isUnit,
+                                       final List<AvroConstructorDef> injectableConstructors,
+                                       final List<AvroConstructorDef> otherConstructors,
+                                       final List<String> implFullNames,
+                                       final String defaultImplementation,
+                                       final List<AvroNode> children) {
+    return AvroNode.newBuilder()
+            .setName(name)
+            .setFullName(fullName)
+            .setClassNode(AvroClassNode.newBuilder()
+                    .setIsInjectionCandidate(isInjectionCandidate)
+                    .setIsExternalConstructor(isExternalConstructor)
+                    .setIsUnit(isUnit)
+                    .setInjectableConstructors(injectableConstructors)
+                    .setOtherConstructors(otherConstructors)
+                    .setImplFullNames(implFullNames)
+                    .setDefaultImplementation(defaultImplementation)
+                    .build())
+            .setChildren(children).build();
+  }
+
+  private static AvroNode newNamedParameterNode(final String name,
+                                                final String fullName,
+                                                final String simpleArgClassName,
+                                                final String fullArgClassName,
+                                                final boolean isSet,
+                                                final boolean isList,
+                                                final String documentation, // can be null
+                                                final String shortName, // can be null
+                                                final List<String> instanceDefault, // can be null
+                                                final List<AvroNode> children) {
+    final AvroNamedParameterNode.Builder namedParameterNodeBuilder = AvroNamedParameterNode.newBuilder()
+            .setSimpleArgClassName(simpleArgClassName)
+            .setFullArgClassName(fullArgClassName)
+            .setIsSet(isSet)
+            .setIsList(isList);
+      namedParameterNodeBuilder.setDocumentation(documentation);
+      namedParameterNodeBuilder.setShortName(shortName);
+      namedParameterNodeBuilder.setInstanceDefault(instanceDefault);
+
+    return AvroNode.newBuilder()
+            .setName(name)
+            .setFullName(fullName)
+            .setNamedParameterNode(namedParameterNodeBuilder.build())
+            .setChildren(children).build();
+  }
+
+  private static AvroNode newPackageNode(final String name,
+                                         final String fullName,
+                                         final List<AvroNode> children) {
+    return AvroNode.newBuilder()
+            .setPackageNode(AvroPackageNode.newBuilder().build())
+            .setName(name).setFullName(fullName).setChildren(children).build();
+  }
+
+  private static AvroConstructorDef newConstructorDef(
+          String fullClassName, List<AvroConstructorArg> args) {
+    return AvroConstructorDef.newBuilder()
+            .setFullArgClassName(fullClassName).setConstructorArg(args).build();
+  }
+
+  private static AvroConstructorArg newConstructorArg(final String fullArgClassName,
+                                                      final String namedParameterName,
+                                                      final boolean isFuture) {
+    AvroConstructorArg.Builder builder = AvroConstructorArg.newBuilder()
+            .setFullArgClassName(fullArgClassName)
+            .setIsInjectionFuture(isFuture);
+    if (namedParameterName != null) {
+      builder.setNamedParameterName(namedParameterName).build();
+    }
+    return builder.build();
+  }
+
+  private static AvroConstructorDef serializeConstructorDef(final ConstructorDef<?> def) {
+    final List<AvroConstructorArg> args = new ArrayList<>();
+    for (ConstructorArg arg : def.getArgs()) {
+      args.add(newConstructorArg(arg.getType(), arg.getNamedParameterName(), arg.isInjectionFuture()));
+    }
+    return newConstructorDef(def.getClassName(), args);
+  }
+
+  private static AvroNode toAvroNode(final Node n) {
+    final List<AvroNode> children = new ArrayList<>();
+    for (final Node child : n.getChildren()) {
+      children.add(toAvroNode(child));
+    }
+    if (n instanceof ClassNode) {
+      final ClassNode<?> cn = (ClassNode<?>) n;
+      final ConstructorDef<?>[] injectable = cn.getInjectableConstructors();
+      final ConstructorDef<?>[] all = cn.getAllConstructors();
+      final List<ConstructorDef<?>> others = new ArrayList<>(Arrays.asList(all));
+      others.removeAll(Arrays.asList(injectable));
+
+      final List<AvroConstructorDef> injectableConstructors = new ArrayList<>();
+      for (final ConstructorDef<?> inj : injectable) {
+        injectableConstructors.add(serializeConstructorDef(inj));
+      }
+      final List<AvroConstructorDef> otherConstructors = new ArrayList<>();
+      for (final ConstructorDef<?> other : others) {
+        otherConstructors.add(serializeConstructorDef(other));
+      }
+      final List<String> implFullNames = new ArrayList<>();
+      for (final ClassNode<?> impl : cn.getKnownImplementations()) {
+        implFullNames.add(impl.getFullName());
+      }
+      return newClassNode(n.getName(), n.getFullName(), cn.isInjectionCandidate(), cn.isExternalConstructor(),
+              cn.isUnit(), injectableConstructors, otherConstructors, implFullNames, cn.getDefaultImplementation(), children);
+    } else if (n instanceof NamedParameterNode) {
+      final NamedParameterNode<?> np = (NamedParameterNode<?>) n;
+      return newNamedParameterNode(np.getName(), np.getFullName(), np.getSimpleArgName(), np.getFullArgName(),
+              np.isSet(), np.isList(), np.getDocumentation(), np.getShortName(),
+              Arrays.asList(np.getDefaultInstanceAsStrings()), children);
+    } else if (n instanceof PackageNode) {
+      return newPackageNode(n.getName(), n.getFullName(), children);
+    } else {
+      throw new IllegalStateException("Encountered unknown type of Node: " + n);
+    }
+  }
+
+  public AvroClassHierarchy toAvro(final ClassHierarchy classHierarchy) {
+    return new AvroClassHierarchy(toAvroNode(classHierarchy.getNamespace()));
+  }
+
+  @Override
+  public void toFile(final ClassHierarchy classHierarchy, final File file) throws IOException {
+    final AvroNode avroNode = toAvroNode(classHierarchy.getNamespace());
+    final DatumWriter<AvroNode> avroNodeWriter = new SpecificDatumWriter<>(AvroNode.class);
+    try (DataFileWriter<AvroNode> dataFileWriter = new DataFileWriter<>(avroNodeWriter)) {
+      dataFileWriter.create(avroNode.getSchema(), file);
+      dataFileWriter.append(avroNode);
+    }
+  }
+
+  @Override
+  public void toTextFile(ClassHierarchy classHierarchy, File file) throws IOException {
+    try (final Writer w = new FileWriter(file)) {
+      w.write(this.toString(classHierarchy));
+    }
+  }
+
+  @Override
+  public byte[] toByteArray(final ClassHierarchy classHierarchy) throws IOException {
+    final DatumWriter<AvroNode> requestWriter = new SpecificDatumWriter<>(AvroNode.class);
+    try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) {
+      final BinaryEncoder encoder = EncoderFactory.get().binaryEncoder(out, null);
+      // TODO Should be a better way
+      requestWriter.write(toAvroNode(classHierarchy.getNamespace()), encoder);
+      encoder.flush();
+      return out.toByteArray();
+    } catch (final IOException e) {
+      throw new RuntimeException(e);
+    }
+  }
+
+  // TODO Improve the interface?
+  @Override
+  public String toString(ClassHierarchy classHierarchy) throws IOException {
+    final DatumWriter<AvroNode> configurationWriter = new SpecificDatumWriter<>(AvroNode.class);
+    try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) {
+      final JsonEncoder encoder = EncoderFactory.get().jsonEncoder(AvroConfiguration.SCHEMA$, out);
+      configurationWriter.write(toAvroNode(classHierarchy.getNamespace()), encoder);
+      encoder.flush();
+      out.flush();
+      return out.toString(JSON_CHARSET);
+    } catch (final IOException e) {
+      throw new RuntimeException(e);
+    }
+  }
+
+  @Override
+  public ClassHierarchy fromFile(final File file) throws IOException {
+    final AvroNode avroNode;
+    try (final DataFileReader<AvroNode> dataFileReader =
+                 new DataFileReader<>(file, new SpecificDatumReader<>(AvroNode.class))) {
+      avroNode = dataFileReader.next();
+    }
+    return new AvroClassHierarchy(avroNode);
+  }
+
+  @Override
+  public ClassHierarchy fromTextFile(final File file) throws IOException {
+    final StringBuilder stringBuilder = new StringBuilder();
+    try (final BufferedReader reader = new BufferedReader(new FileReader(file))) {
+      String line = reader.readLine();
+      while (line != null) {
+        stringBuilder.append(line);
+        line = reader.readLine();
+      }
+    }
+    return fromString(stringBuilder.toString());
+  }
+
+  @Override
+  public ClassHierarchy fromByteArray(final byte[] theBytes) throws IOException {
+    try {
+      final BinaryDecoder decoder = DecoderFactory.get().binaryDecoder(theBytes, null);
+      final SpecificDatumReader<AvroNode> reader = new SpecificDatumReader<>(AvroNode.class);
+      return new AvroClassHierarchy(reader.read(null, decoder));
+    } catch (final IOException e) {
+      throw new RuntimeException(e);
+    }
+  }
+
+  @Override
+  public ClassHierarchy fromString(final String theString) throws IOException {
+    try {
+      final JsonDecoder decoder = DecoderFactory.get().jsonDecoder(AvroNode.getClassSchema(), theString);
+      final SpecificDatumReader<AvroNode> reader = new SpecificDatumReader<>(AvroNode.class);
+      return new AvroClassHierarchy(reader.read(null, decoder));
+    } catch (final IOException e) {
+      throw new RuntimeException(e);
+    }
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/7a095daf/lang/java/reef-tang/tang/src/main/java/org/apache/reef/tang/formats/ClassHierarchySerializer.java
----------------------------------------------------------------------
diff --git a/lang/java/reef-tang/tang/src/main/java/org/apache/reef/tang/formats/ClassHierarchySerializer.java b/lang/java/reef-tang/tang/src/main/java/org/apache/reef/tang/formats/ClassHierarchySerializer.java
new file mode 100644
index 0000000..bf3918b
--- /dev/null
+++ b/lang/java/reef-tang/tang/src/main/java/org/apache/reef/tang/formats/ClassHierarchySerializer.java
@@ -0,0 +1,43 @@
+package org.apache.reef.tang.formats;
+
+import org.apache.reef.tang.ClassHierarchy;
+import org.apache.reef.tang.annotations.DefaultImplementation;
+import org.apache.reef.tang.formats.avro.AvroNode;
+
+import java.io.*;
+
+/**
+ *
+ */
+@DefaultImplementation(org.apache.reef.tang.formats.AvroClassHierarchySerializer.class)
+public interface ClassHierarchySerializer {
+  /**
+   * serialize a class hierarchy into a file.
+   *
+   * @param file
+   * @param classHierarchy
+   * @throws IOException
+   */
+  void toFile(final ClassHierarchy classHierarchy, final File file) throws IOException;
+
+  void toTextFile(final ClassHierarchy classHierarchy, final File file) throws IOException;
+
+  byte[] toByteArray(final ClassHierarchy classHierarchy) throws IOException;
+
+  String toString(final ClassHierarchy classHierarchy) throws IOException;
+
+  /**
+   * Deserialize a class hierarchy from a file. The file can be generated from either Java or C#
+   *
+   * @param file
+   * @return
+   * @throws IOException
+   */
+  ClassHierarchy fromFile(final File file) throws IOException;
+
+  ClassHierarchy fromTextFile(final File file) throws IOException;
+
+  ClassHierarchy fromByteArray(final byte[] theBytes) throws IOException;
+
+  ClassHierarchy fromString(final String theString) throws IOException;
+}


[12/17] incubator-reef git commit: Add comments in Serializer

Posted by yu...@apache.org.
Add comments in Serializer


Project: http://git-wip-us.apache.org/repos/asf/incubator-reef/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-reef/commit/47865fd3
Tree: http://git-wip-us.apache.org/repos/asf/incubator-reef/tree/47865fd3
Diff: http://git-wip-us.apache.org/repos/asf/incubator-reef/diff/47865fd3

Branch: refs/heads/REEF-395
Commit: 47865fd34a97b1d4e52b55abf4d2a60032167560
Parents: 0e52a16
Author: Yunseong Lee <yu...@apache.org>
Authored: Mon Jun 22 21:08:34 2015 +0900
Committer: Yunseong Lee <yu...@apache.org>
Committed: Mon Jun 22 21:08:34 2015 +0900

----------------------------------------------------------------------
 .../formats/AvroClassHierarchySerializer.java     | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/47865fd3/lang/java/reef-tang/tang/src/main/java/org/apache/reef/tang/formats/AvroClassHierarchySerializer.java
----------------------------------------------------------------------
diff --git a/lang/java/reef-tang/tang/src/main/java/org/apache/reef/tang/formats/AvroClassHierarchySerializer.java b/lang/java/reef-tang/tang/src/main/java/org/apache/reef/tang/formats/AvroClassHierarchySerializer.java
index f096021..2f5c0d8 100644
--- a/lang/java/reef-tang/tang/src/main/java/org/apache/reef/tang/formats/AvroClassHierarchySerializer.java
+++ b/lang/java/reef-tang/tang/src/main/java/org/apache/reef/tang/formats/AvroClassHierarchySerializer.java
@@ -34,12 +34,22 @@ public class AvroClassHierarchySerializer implements ClassHierarchySerializer {
   public AvroClassHierarchySerializer() {
   }
 
-  private static ClassHierarchy fromAvro(final AvroNode n) {
-    return new AvroClassHierarchy(n);
+  /**
+   * Serialize the ClassHierarchy into the AvroNode.
+   * This method is set to be Package private for testing.
+   * @param ch ClassHierarchy to serialize
+   */
+  static AvroNode toAvro(final ClassHierarchy ch) {
+    return newAvroNode(ch.getNamespace());
   }
 
-  private static AvroNode toAvro(final ClassHierarchy ch) {
-    return newAvroNode(ch.getNamespace());
+  /**
+   * Deserialize the ClassHierarchy from the AvroNode.
+   * This method is set to be Package private for testing.
+   * @param n AvroNode to deserialize
+   */
+  static ClassHierarchy fromAvro(final AvroNode n) {
+    return new AvroClassHierarchy(n);
   }
 
   private static AvroNode newAvroNode(final Node n) {