/**
 * @license
 * Copyright 2018 The FOAM Authors. All Rights Reserved.
 * http://www.apache.org/licenses/LICENSE-2.0
 */

package foam.lib.json;

import foam.core.ClassInfo;
import foam.lib.parse.*;

public class ClassReferenceParser
  extends ProxyParser
{
  public ClassReferenceParser() {
    super(new Alt(
      new NullParser(),
      new Seq1(15,
        new Whitespace(),
        new Literal("{"),
        new Whitespace(),
        new KeyParser("class"),
        new Whitespace(),
        new Literal(":"),
        new Whitespace(),
        new Literal("\"__Class__\""),
        new Whitespace(),
        new Literal(","),
        new Whitespace(),
        new KeyParser("forClass_"),
        new Whitespace(),
        new Literal(":"),
        new Whitespace(),
        new StringParser(),
        new Whitespace(),
        new Literal("}")),
      new StringParser()
    ));
  }

  public PStream parse(PStream ps, ParserContext x) {
    if ( ( ps = super.parse(ps, x)) == null ) {
      return null;
    }

    String classId = (String) ps.value();
    // Expects classId be a fully qualified name of a modelled class
    // with Java code generation for class lookup and returns 
    // ClassInfo of the modelled class if found, otherwise return null.
    //
    // Eg.,
    // When parsing
    //   { "class": "__Class__", "forClass_": "foam.nanos.auth.User" }
    // returns User.getOwnClassInfo() instead of the actual User class.
    //
    // And when parsing
    //   { "class": "__Class__", "forClass_": "java.lang.Object" }
    // returns null because java.lang.Object is not a modelled class.
    try {
      Class cls = Class.forName(classId);
      ClassInfo info = (ClassInfo) cls.getMethod("getOwnClassInfo") .invoke(null);
      return ps.setValue(info);
    } catch ( Throwable t ) {
      System.err.println(classId + " is not a modelled class.");
      return null;
    }
  }
}

@@ -1302,54 +1302,8 @@
 sing
-%0A    //   %7B %22class%22: %22__Class__%22, %22forClass_%22:
  %22fo
@@ -1321,25 +1321,17 @@
 th.User%22
- %7D%0A    //
+,
  returns
@@ -1357,119 +1357,40 @@
 fo()
- instead of the actual User class.%0A    //%0A    // And when parsing%0A    //   %7B %22class%22: %22__Class__%22, %22forClass_%22:
+.%0A    //%0A    // And when parsing
  %22ja
@@ -1408,17 +1408,9 @@
 ect%22
- %7D%0A    //
+,
  ret
@@ -1418,16 +1418,23 @@
 rns null
+%0A    //
  because

Update comment