org.yaml.snakeyaml.constructor.Constructor类的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(11.9k)|赞(0)|评价(0)|浏览(521)

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

Constructor介绍

[英]Construct a custom Java instance.
[中]构造一个自定义Java实例。

代码示例

代码示例来源:origin: apache/incubator-dubbo

private static <T> T parseObject(String rawConfig) {
  Constructor constructor = new Constructor(ConfiguratorConfig.class);
  TypeDescription itemDescription = new TypeDescription(ConfiguratorConfig.class);
  itemDescription.addPropertyParameters("items", ConfigItem.class);
  constructor.addTypeDescription(itemDescription);
  Yaml yaml = new Yaml(constructor);
  return yaml.load(rawConfig);
}

代码示例来源:origin: lealone/Lealone

public Config loadConfig(URL url) throws ConfigException {
  try {
    logger.info("Loading config from {}", url);
    byte[] configBytes;
    try (InputStream is = url.openStream()) {
      configBytes = IOUtils.toByteArray(is);
    } catch (IOException e) {
      // getConfigURL should have ruled this out
      throw new AssertionError(e);
    }
    Constructor configConstructor = new Constructor(getConfigClass());
    addTypeDescription(configConstructor);
    MissingPropertiesChecker propertiesChecker = new MissingPropertiesChecker();
    configConstructor.setPropertyUtils(propertiesChecker);
    Yaml yaml = new Yaml(configConstructor);
    Config result = (Config) yaml.loadAs(new ByteArrayInputStream(configBytes), getConfigClass());
    propertiesChecker.check();
    return result;
  } catch (YAMLException e) {
    throw new ConfigException("Invalid yaml", e);
  }
}

代码示例来源:origin: redisson/redisson

@SuppressWarnings("unchecked")
public Object construct(Node node) {
  SequenceNode snode = (SequenceNode) node;
  if (Set.class.isAssignableFrom(node.getType())) {
    if (node.isTwoStepsConstruction()) {
      throw new YAMLException("Set cannot be recursive.");
    } else {
      return constructSet(snode);
  } else if (Collection.class.isAssignableFrom(node.getType())) {
    if (node.isTwoStepsConstruction()) {
      return newList(snode);
    } else {
      return constructSequence(snode);
  } else if (node.getType().isArray()) {
    if (node.isTwoStepsConstruction()) {
      return createArray(node.getType(), snode.getValue().size());
    } else {
      return constructArray(snode);
          argumentNode.setType(type);
          argumentList[index++] = constructObject(argumentNode);
      List<Object> argumentList = (List<Object>) constructSequence(snode);
      Class<?>[] parameterTypes = new Class[argumentList.size()];
      int index = 0;

代码示例来源:origin: redisson/redisson

private Object newInstance(TypeDescription memberDescription, String propertyName,
    Node node) {
  Object newInstance = memberDescription.newInstance(propertyName, node);
  if (newInstance != null) {
    constructedObjects.put(node, newInstance);
    return constructObjectNoCheck(node);
  }
  return constructObject(node);
}

代码示例来源:origin: apache/incubator-shardingsphere

private static YamlOrchestrationShardingRuleConfiguration unmarshal(final byte[] yamlByteArray) {
    return new Yaml(new Constructor(YamlOrchestrationShardingRuleConfiguration.class)).loadAs(new ByteArrayInputStream(yamlByteArray), YamlOrchestrationShardingRuleConfiguration.class);
  }
}

代码示例来源:origin: apache/incubator-dubbo

public static ConditionRouterRule parse(String rawRule) {
  Constructor constructor = new Constructor(ConditionRouterRule.class);
  Yaml yaml = new Yaml(constructor);
  ConditionRouterRule rule = yaml.load(rawRule);
  rule.setRawRule(rawRule);
  if (CollectionUtils.isEmpty(rule.getConditions())) {
    rule.setValid(false);
  }
  return rule;
}

代码示例来源:origin: com.strapdata.elasticsearch/elasticsearch

org.yaml.snakeyaml.constructor.Constructor constructor = new org.yaml.snakeyaml.constructor.Constructor(Config.class);
TypeDescription seedDesc = new TypeDescription(ParameterizedClass.class);
seedDesc.putMapPropertyType("parameters", String.class, String.class);
constructor.addTypeDescription(seedDesc);
MissingPropertiesChecker propertiesChecker = new MissingPropertiesChecker();
constructor.setPropertyUtils(propertiesChecker);
Yaml yaml = new Yaml(constructor);
Config result = yaml.loadAs(new ByteArrayInputStream(configBytes), Config.class);

代码示例来源:origin: org.testng/testng

Constructor constructor = new TestNGConstructor(XmlSuite.class);
 TypeDescription suiteDescription = new TypeDescription(XmlSuite.class);
 suiteDescription.putListPropertyType("packages", XmlPackage.class);
 suiteDescription.putListPropertyType("listeners", String.class);
 suiteDescription.putListPropertyType("tests", XmlTest.class);
 suiteDescription.putListPropertyType("method-selectors", XmlMethodSelector.class);
 constructor.addTypeDescription(suiteDescription);
 testDescription.putMapPropertyType("metaGroups", String.class, List.class);
 testDescription.putListPropertyType("method-selectors", XmlMethodSelector.class);
 constructor.addTypeDescription(testDescription);
org.yaml.snakeyaml.Yaml y = new org.yaml.snakeyaml.Yaml(constructor);
if (is == null) {
 is = new FileInputStream(new File(filePath));
XmlSuite result = (XmlSuite) y.load(is);

代码示例来源:origin: airbnb/okreplay

private static Yaml getYaml() {
  Representer representer = new TapeRepresenter();
  representer.addClassTag(YamlTape.class, YamlTape.TAPE_TAG);
  Constructor constructor = new TapeConstructor();
  constructor.addTypeDescription(new TypeDescription(YamlTape.class, YamlTape.TAPE_TAG));
  DumperOptions dumperOptions = new DumperOptions();
  dumperOptions.setDefaultFlowStyle(BLOCK);
  dumperOptions.setWidth(256);
  Yaml yaml = new Yaml(constructor, representer, dumperOptions);
  yaml.setBeanAccess(BeanAccess.FIELD);
  return yaml;
 }
}

代码示例来源:origin: cereda/arara

throws AraraException {
Representer representer = new Representer();
representer.addClassTag(Rule.class, new Tag("!config"));
Yaml yaml = new Yaml(new Constructor(Rule.class), representer);
Rule rule = null;
try {
  rule = yaml.loadAs(new FileReader(file), Rule.class);
} catch (MarkedYAMLException yamlException) {
  throw new AraraException(

代码示例来源:origin: org.apache.cassandra/cassandra-all

public Config loadConfig(URL url) throws ConfigurationException
{
  try
  {
    logger.debug("Loading settings from {}", url);
    byte[] configBytes;
    try (InputStream is = url.openStream())
    {
      configBytes = ByteStreams.toByteArray(is);
    }
    catch (IOException e)
    {
      // getStorageConfigURL should have ruled this out
      throw new AssertionError(e);
    }
    Constructor constructor = new CustomConstructor(Config.class);
    PropertiesChecker propertiesChecker = new PropertiesChecker();
    constructor.setPropertyUtils(propertiesChecker);
    Yaml yaml = new Yaml(constructor);
    Config result = loadConfig(yaml, configBytes);
    propertiesChecker.check();
    return result;
  }
  catch (YAMLException e)
  {
    throw new ConfigurationException("Invalid yaml: " + url + SystemUtils.LINE_SEPARATOR
                     +  " Error: " + e.getMessage(), false);
  }
}

代码示例来源:origin: org.qsardb.cargo/bodo

public BODODescriptor loadBodoDescriptor() throws IOException {
  TypeDescription descriptorDescription = new TypeDescription(BODODescriptor.class);
  descriptorDescription.putListPropertyType("implementations", BODODescriptor.Implementation.class);
  Yaml yaml = new Yaml(new Constructor(descriptorDescription));
  return (BODODescriptor)yaml.load(loadString());
}

代码示例来源:origin: google/cdep

@NotNull
public static CDepManifestYml convertStringToManifest(@NotNull String url, @NotNull String content) {
 Invariant.registerYamlFile(url);
 Yaml yaml = new Yaml(new Constructor(CDepManifestYml.class));
 CDepManifestYml manifest;
 byte[] bytes = content.getBytes(StandardCharsets.UTF_8);
 try {
  manifest = (CDepManifestYml) yaml.load(new ByteArrayInputStream(bytes));
  // Try to read current version
  if (manifest != null) {
   manifest.sourceVersion = CDepManifestYmlVersion.vlatest;
  }
 } catch (YAMLException e) {
  try {
   manifest = V3Reader.convertStringToManifest(content);
  } catch (YAMLException e2) {
   if (!tryCreateSensibleParseError(e, 0)) {
    // If older readers also couldn't read it then report the original exception.
    require(false, e.toString());
   }
   return new CDepManifestYml(EMPTY_COORDINATE);
  }
 }
 require(manifest != null, "Manifest was empty");
 assert manifest != null;
 manifest = new ConvertNullToDefaultRewriter().visitCDepManifestYml(manifest);
 Node nodes = yaml.compose(new InputStreamReader(new ByteArrayInputStream(bytes)));
 SnakeYmlUtils.mapAndRegisterNodes(url, manifest, nodes);
 return manifest;
}

代码示例来源:origin: SpigotMC/BungeeCord

@SuppressWarnings("unchecked")
public PluginManager(ProxyServer proxy)
{
  this.proxy = proxy;
  // Ignore unknown entries in the plugin descriptions
  Constructor yamlConstructor = new Constructor();
  PropertyUtils propertyUtils = yamlConstructor.getPropertyUtils();
  propertyUtils.setSkipMissingProperties( true );
  yamlConstructor.setPropertyUtils( propertyUtils );
  yaml = new Yaml( yamlConstructor );
  eventBus = new EventBus( proxy.getLogger() );
}

代码示例来源:origin: SpigotMC/BungeeCord

@Override
  protected Yaml initialValue()
  {
    Representer representer = new Representer()
    {
      {
        representers.put( Configuration.class, new Represent()
        {
          @Override
          public Node representData(Object data)
          {
            return represent( ( (Configuration) data ).self );
          }
        } );
      }
    };
    DumperOptions options = new DumperOptions();
    options.setDefaultFlowStyle( DumperOptions.FlowStyle.BLOCK );
    return new Yaml( new Constructor(), representer, options );
  }
};

代码示例来源:origin: redisson/redisson

protected Object constructJavaBean2ndStep(MappingNode node, Object object) {
  flattenMapping(node);
  Class<? extends Object> beanType = node.getType();
  List<NodeTuple> nodeValue = node.getValue();
      throw new YAMLException(
          "Keys must be scalars but found: " + tuple.getKeyNode());
    String key = (String) constructObject(keyNode);
    try {
      TypeDescription memberDescription = typeDefinitions.get(beanType);
      Property property = memberDescription == null ? getProperty(beanType, key)
          : memberDescription.getProperty(key);
        throw new YAMLException("No writable property '" + key + "' on class: "
            + beanType.getName());
          if (valueNode.getNodeId() == NodeId.sequence) {
            Class<?> t = arguments[0];
            SequenceNode snode = (SequenceNode) valueNode;
          : constructObject(valueNode);
      if (property.getType() == String.class && Tag.BINARY.equals(valueNode.getTag())
          && value instanceof byte[]) {
        value = new String((byte[]) value);
          || !memberDescription.setProperty(object, key, value)) {
        property.set(object, value);

代码示例来源:origin: redisson/redisson

/**
 * Create Yaml instance. It is safe to create a few instances and use them
 * in different Threads.
 *
 * @param representer
 *            Representer to emit outgoing objects
 */
public Yaml(Representer representer) {
  this(new Constructor(), representer);
}

代码示例来源:origin: pl.droidsonroids.yaml/snakeyaml

protected Object constructJavaBean2ndStep(MappingNode node, Object object) {
  flattenMapping(node);
  Class<? extends Object> beanType = node.getType();
  List<NodeTuple> nodeValue = node.getValue();
      throw new YAMLException("Keys must be scalars but found: " + tuple.getKeyNode());
    String key = (String) constructObject(keyNode);
    try {
      Property property = getProperty(beanType, key);
      valueNode.setType(property.getType());
      TypeDescription memberDescription = typeDefinitions.get(beanType);
      boolean typeDetected = false;
      if (memberDescription != null) {
        switch (valueNode.getNodeId()) {
        case sequence:
          SequenceNode snode = (SequenceNode) valueNode;
          Class<? extends Object> memberType = memberDescription
              .getListPropertyType(key);
          if (memberType != null) {
            snode.setListType(memberType);
      if (!typeDetected && valueNode.getNodeId() != NodeId.scalar) {
      Object value = constructObject(valueNode);
      if (property.getType() == String.class && Tag.BINARY.equals(valueNode.getTag()) && value instanceof byte[]) {
        value = new String((byte[])value);

代码示例来源:origin: lealone/Lealone

protected void addTypeDescription(Constructor configConstructor) {
  TypeDescription mapPropertyTypeDesc = new TypeDescription(MapPropertyTypeDef.class);
  mapPropertyTypeDesc.putMapPropertyType("parameters", String.class, String.class);
  configConstructor.addTypeDescription(mapPropertyTypeDesc);
}

代码示例来源:origin: redisson/redisson

public Constructor(TypeDescription theRoot, Collection<TypeDescription> moreTDs) {
  if (theRoot == null) {
    throw new NullPointerException("Root type must be provided.");
  }
  this.yamlConstructors.put(null, new ConstructYamlObject());
  if (!Object.class.equals(theRoot.getType())) {
    rootTag = new Tag(theRoot.getType());
  }
  yamlClassConstructors.put(NodeId.scalar, new ConstructScalar());
  yamlClassConstructors.put(NodeId.mapping, new ConstructMapping());
  yamlClassConstructors.put(NodeId.sequence, new ConstructSequence());
  addTypeDescription(theRoot);
  if (moreTDs != null) {
    for (TypeDescription td : moreTDs) {
      addTypeDescription(td);
    }
  }
}

相关文章