/**
 * @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();
    // NOTE: ClassReferenceParser expects fully qualified name of a modelled class 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" }
    // it would return User.getOwnClassInfo() instead of the actual User class.
    //
    // And when parsing { "class": "__Class__", "forClass_": "java.lang.Object" }
    // it would return 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;
    }
  }
}

@@ -1067,42 +1067,28 @@
  // 
-NOTE: ClassReferenceParser expects
+Expects classId be a
  ful
@@ -1124,16 +1124,49 @@
 ed class
+%0A    // with Java code generation
  for cla
@@ -1174,23 +1174,16 @@
 s lookup
-%0A    //
  and ret
@@ -1187,16 +1187,24 @@
 returns 
+%0A    // 
 ClassInf
@@ -1290,32 +1290,41 @@
  // When parsing
+%0A    //  
  %7B %22class%22: %22__C
@@ -1376,32 +1376,24 @@
 %7D%0A    //
- it would
  return
+s
  User.ge
@@ -1472,16 +1472,25 @@
  parsing
+%0A    //  
  %7B %22clas
@@ -1550,24 +1550,16 @@
   //
- it would
  return
+s
  nul

Update comments