com.vmware.xenon.common.Utils.getTypeFromKind()方法的使用及代码示例

x33g5p2x  于2022-02-01 转载在 其他  
字(3.9k)|赞(0)|评价(0)|浏览(116)

本文整理了Java中com.vmware.xenon.common.Utils.getTypeFromKind()方法的一些代码示例,展示了Utils.getTypeFromKind()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Utils.getTypeFromKind()方法的具体详情如下:
包路径:com.vmware.xenon.common.Utils
类名称:Utils
方法名:getTypeFromKind

Utils.getTypeFromKind介绍

[英]Obtain the class for the specified kind. Only classes registered via Utils#registerKind(Class, String) will be returned
[中]获取指定类型的类。只返回通过Utils#registerKind(Class,String)注册的类

代码示例

代码示例来源:origin: vmware/admiral

@SuppressWarnings("unchecked")
private static Class<? extends ServiceDocument> getTypeFromKind(String documentKind) {
  // TODO: how to find class?
  // Check if kind was registered
  Class<?> clazz = Utils.getTypeFromKind(documentKind);
  if (clazz == null) {
    String className = documentKind.replace(':', '.');
    while (true) {
      try {
        clazz = Class.forName(className);
        break;
      } catch (ClassNotFoundException e) {
        int i = className.lastIndexOf('.');
        if (i == -1) {
          logger.warning("State type not found for " + documentKind);
          return null;
        }
        // Check if inner class, replace last '.' with '$'
        StringBuilder sb = new StringBuilder(className);
        sb.setCharAt(i, '$');
        className = sb.toString();
      }
    }
    // Register kind
    Utils.registerKind(clazz, documentKind);
  }
  return (Class<? extends ServiceDocument>) clazz;
}

代码示例来源:origin: com.vmware.photon.controller/photon-model

@Override
  public ResourceState deserialize(JsonElement json, Type typeOfT,
      JsonDeserializationContext context) throws JsonParseException {
    JsonElement documentKind = json.getAsJsonObject().get("documentKind");
    if (documentKind == null) {
      // deserialize as ResourceState if documentKink is not available
      Utils.log(ResourceStateDeserializer.class, "deserialize", Level.WARNING,
          "Json document of type %s does not have a documentKind field",
          typeOfT.getTypeName());
      return Utils.fromJson(json, typeOfT);
    }
    Class<?> clazz = Utils.getTypeFromKind(documentKind.getAsString());
    if (clazz != null) {
      return context.deserialize(json, clazz);
    } else {
      // deserialize as ResourceState if type is not registered
      Utils.log(ResourceStateDeserializer.class, "deserialize", Level.WARNING,
          "Json document of type %s and kind %s does not declare derived deserializer",
          typeOfT, documentKind.getAsString());
      return Utils.fromJson(json, typeOfT);
    }
  }
}

代码示例来源:origin: vmware/xenon

private static ServiceDocument extractServiceState(Operation getOp, ServiceHost serviceHost) {
  ServiceDocument userState = QueryFilterUtils.getServiceState(getOp, serviceHost);
  if (userState == null) {
    Object rawBody = getOp.getBodyRaw();
    Class<?> serviceTypeClass = null;
    if (rawBody instanceof String) {
      String kind = Utils.getJsonMapValue(rawBody, ServiceDocument.FIELD_NAME_KIND,
          String.class);
      serviceTypeClass = Utils.getTypeFromKind(kind);
    } else {
      serviceTypeClass = rawBody.getClass();
    }
    if (serviceTypeClass != null) {
      userState = (ServiceDocument)Utils.fromJson(rawBody, serviceTypeClass);
    }
  }
  return userState;
}

代码示例来源:origin: vmware/xenon

@Test
public void registerKind() {
  String kindBefore = Utils.buildKind(ExampleServiceState.class);
  String newKind = "e";
  Utils.registerKind(ExampleServiceState.class, newKind);
  String kindAfter = Utils.buildKind(ExampleServiceState.class);
  assertEquals(newKind, kindAfter);
  Utils.registerKind(ExampleServiceState.class, kindBefore);
  kindAfter = Utils.buildKind(ExampleServiceState.class);
  assertEquals(kindBefore, kindAfter);
  Class<?> stateClass = Utils.getTypeFromKind(kindAfter);
  assertEquals(stateClass.getCanonicalName(), ExampleServiceState.class.getCanonicalName());
}

代码示例来源:origin: com.vmware.xenon/xenon-common

@Test
public void registerKind() {
  String kindBefore = Utils.buildKind(ExampleServiceState.class);
  String newKind = "e";
  Utils.registerKind(ExampleServiceState.class, newKind);
  String kindAfter = Utils.buildKind(ExampleServiceState.class);
  assertEquals(newKind, kindAfter);
  Utils.registerKind(ExampleServiceState.class, kindBefore);
  kindAfter = Utils.buildKind(ExampleServiceState.class);
  assertEquals(kindBefore, kindAfter);
  Class<?> stateClass = Utils.getTypeFromKind(kindAfter);
  assertEquals(stateClass.getCanonicalName(), ExampleServiceState.class.getCanonicalName());
}

相关文章