groovy.lang.Binding.hasVariable()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(10.2k)|赞(0)|评价(0)|浏览(607)

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

Binding.hasVariable介绍

[英]Simple check for whether the binding contains a particular variable or not.
[中]简单检查绑定是否包含特定变量。

代码示例

代码示例来源:origin: spring-projects/spring-framework

if (binding != null && binding.hasVariable(name)) {
  return binding.getVariable(name);

代码示例来源:origin: org.springframework/spring-beans

if (binding != null && binding.hasVariable(name)) {
  return binding.getVariable(name);

代码示例来源:origin: io.snamp/scripting

/**
 * Simple check for whether the binding contains a particular variable or not.
 *
 * @param name the name of the variable to check for
 */
@Override
public boolean hasVariable(final String name) {
  return first.hasVariable(name) && second.hasVariable(name);
}

代码示例来源:origin: io.snamp/scripting

/**
 * @param name the name of the variable to lookup
 * @return the variable value
 */
@Override
public Object getVariable(final String name) {
  return first.hasVariable(name) ? first.getVariable(name) : second.getVariable(name);
}

代码示例来源:origin: io.snamp/scripting

/**
 * Sets the value of the given variable
 *
 * @param name  the name of the variable to set
 * @param value the new value for the given variable
 */
@Override
public void setVariable(final String name, final Object value) {
  if(first.hasVariable(name))
    first.setVariable(name, value);
  else
    second.setVariable(name, value);
}

代码示例来源:origin: org.gradle/gradle-core

@Override
public boolean hasProperty(String name) {
  return binding.hasVariable(name) || scriptObject.hasProperty(name) || dynamicTarget.hasProperty(name);
}

代码示例来源:origin: jenkinsci/docker-workflow-plugin

@Override public Object getValue(CpsScript script) throws Exception {
  Binding binding = script.getBinding();
  Object docker;
  if (binding.hasVariable(getName())) {
    docker = binding.getVariable(getName());
  } else {
    // Note that if this were a method rather than a constructor, we would need to mark it @NonCPS lest it throw CpsCallableInvocation.
    docker = script.getClass().getClassLoader().loadClass("org.jenkinsci.plugins.docker.workflow.Docker").getConstructor(CpsScript.class).newInstance(script);
    binding.setVariable(getName(), docker);
  }
  return docker;
}

代码示例来源:origin: com.disney.groovity/groovity-core

public Object getProperty(String property) {
    Binding b = getBinding();
    if(b.hasVariable(property)) {
      return b.getVariable(property);
    }
    return getMetaClass().getProperty(this, property);
  }
}

代码示例来源:origin: disney/groovity

public Object getProperty(String property) {
    Binding b = getBinding();
    if(b.hasVariable(property)) {
      return b.getVariable(property);
    }
    return getMetaClass().getProperty(this, property);
  }
}

代码示例来源:origin: jenkinsci/kubernetes-pipeline-plugin

@Nonnull
@Override
public Object getValue(CpsScript script) throws Exception {
  Binding binding = script.getBinding();
  Object kubernetes;
  if (binding.hasVariable(getName())) {
    kubernetes = binding.getVariable(getName());
  } else {
    // Note that if this were a method rather than a constructor, we would need to mark it @NonCPS lest it throw CpsCallableInvocation.
    kubernetes = script.getClass().getClassLoader().loadClass("io.fabric8.kubernetes.pipeline.Kubernetes").getConstructor(CpsScript.class).newInstance(script);
    binding.setVariable(getName(), kubernetes);
  }
  return kubernetes;
}

代码示例来源:origin: jenkinsci/kubernetes-pipeline-plugin

@Nonnull
@Override
public Object getValue(CpsScript script) throws Exception {
  Binding binding = script.getBinding();
  Object kubernetes;
  if (binding.hasVariable(getName())) {
    kubernetes = binding.getVariable(getName());
  } else {
    // Note that if this were a method rather than a constructor, we would need to mark it @NonCPS lest it throw CpsCallableInvocation.
    kubernetes = CubeDSL.class.getClassLoader().loadClass("io.fabric8.kubernetes.pipeline.arquillian.cube.kubernetes.Cube").getConstructor(CpsScript.class).newInstance(script);
    binding.setVariable(getName(), kubernetes);
  }
  return kubernetes;
}

代码示例来源:origin: io.fabric8.pipeline/kubernetes-pipeline-arquillian-steps

@Nonnull
@Override
public Object getValue(CpsScript script) throws Exception {
  Binding binding = script.getBinding();
  Object kubernetes;
  if (binding.hasVariable(getName())) {
    kubernetes = binding.getVariable(getName());
  } else {
    // Note that if this were a method rather than a constructor, we would need to mark it @NonCPS lest it throw CpsCallableInvocation.
    kubernetes = CubeDSL.class.getClassLoader().loadClass("io.fabric8.kubernetes.pipeline.arquillian.cube.kubernetes.Cube").getConstructor(CpsScript.class).newInstance(script);
    binding.setVariable(getName(), kubernetes);
  }
  return kubernetes;
}

代码示例来源:origin: org.apereo.cas/cas-server-core-util-api

/**
 * Execute groovy shell script t.
 *
 * @param <T>       the type parameter
 * @param script    the script
 * @param variables the variables
 * @param clazz     the clazz
 * @return the t
 */
public static <T> T executeGroovyShellScript(final String script,
                       final Map<String, Object> variables,
                       final Class<T> clazz) {
  try {
    val binding = new Binding();
    val shell = new GroovyShell(binding);
    if (variables != null && !variables.isEmpty()) {
      variables.forEach(binding::setVariable);
    }
    if (!binding.hasVariable("logger")) {
      binding.setVariable("logger", LOGGER);
    }
    LOGGER.debug("Executing groovy script [{}] with variables [{}]", script, binding.getVariables());
    val result = shell.evaluate(script);
    return getGroovyScriptExecutionResultOrThrow(clazz, result);
  } catch (final Exception e) {
    LOGGER.error(e.getMessage(), e);
  }
  return null;
}

代码示例来源:origin: openshift/jenkins-client-plugin

@Nonnull
  @Override
  public Object getValue(@Nonnull CpsScript script) throws Exception {
    Binding binding = script.getBinding();
    script.println();
    Object openshift;
    if (binding.hasVariable(getName())) {
      openshift = binding.getVariable(getName());
    } else {
      // Note that if this were a method rather than a constructor, we
      // would need to mark it @NonCPS lest it throw
      // CpsCallableInvocation.
      openshift = script.getClass().getClassLoader()
          .loadClass("com.openshift.jenkins.plugins.OpenShiftDSL")
          .getConstructor(CpsScript.class).newInstance(script);
      binding.setVariable(getName(), openshift);
    }
    return openshift;

  }
}

代码示例来源:origin: com.disney.groovity/groovity-core

@SuppressWarnings("rawtypes")
public static AsyncChannel open(DeadlockFreeExecutor asyncChannelExecutor, Object key, int queueSize, Policy policy, Closure handler, Closure closer, Object owner, Binding binding, Execution parentStack){
  AsyncChannel channel = new AsyncChannel(asyncChannelExecutor, key, queueSize, policy, handler, closer, owner, binding,parentStack);
  if(key!=null){
    Collection<AsyncChannel> channels = ASYNC_CHANNEL_ROUTING.get(key);
    if(channels==null) {
      channels = ConcurrentHashMap.newKeySet();
      Collection<AsyncChannel> oldChannels = ASYNC_CHANNEL_ROUTING.putIfAbsent(key, channels);
      if(oldChannels!=null) {
        channels = oldChannels;
      }
    }
    channels.add(channel);
  }else{
    //update AnonymousChannel MBean when anonymous channel is opened
    asyncChannelAnonymousManager.incrementNumberOfTotalChannelsOpened();
  }
  if(binding.hasVariable(ASYNC_CHANNEL_OBSERVER_KEY)) {
    channel.observer = ((AsyncChannelObserver)binding.getVariable(ASYNC_CHANNEL_OBSERVER_KEY));
    channel.observer.opened(channel);
  }
  return channel;
}

代码示例来源:origin: disney/groovity

@SuppressWarnings("rawtypes")
public static AsyncChannel open(DeadlockFreeExecutor asyncChannelExecutor, Object key, int queueSize, Policy policy, Closure handler, Closure closer, Object owner, Binding binding, Execution parentStack){
  AsyncChannel channel = new AsyncChannel(asyncChannelExecutor, key, queueSize, policy, handler, closer, owner, binding,parentStack);
  if(key!=null){
    Collection<AsyncChannel> channels = ASYNC_CHANNEL_ROUTING.get(key);
    if(channels==null) {
      channels = ConcurrentHashMap.newKeySet();
      Collection<AsyncChannel> oldChannels = ASYNC_CHANNEL_ROUTING.putIfAbsent(key, channels);
      if(oldChannels!=null) {
        channels = oldChannels;
      }
    }
    channels.add(channel);
  }else{
    //update AnonymousChannel MBean when anonymous channel is opened
    asyncChannelAnonymousManager.incrementNumberOfTotalChannelsOpened();
  }
  if(binding.hasVariable(ASYNC_CHANNEL_OBSERVER_KEY)) {
    channel.observer = ((AsyncChannelObserver)binding.getVariable(ASYNC_CHANNEL_OBSERVER_KEY));
    channel.observer.opened(channel);
  }
  return channel;
}

代码示例来源:origin: jenkinsci/artifactory-plugin

@Nonnull
  @Override
  public Object getValue(@Nonnull CpsScript cpsScript) throws Exception {
    Binding binding = cpsScript.getBinding();
    Object artifactory;
    if (binding.hasVariable(getName())) {
      artifactory = binding.getVariable(getName());
    } else {
      artifactory = new ArtifactoryPipelineGlobal(cpsScript);
      binding.setVariable(getName(), artifactory);
    }
    return artifactory;
  }
}

代码示例来源:origin: org.gradle/gradle-core

@Override
public DynamicInvokeResult tryGetProperty(String property) {
  if (binding.hasVariable(property)) {
    return DynamicInvokeResult.found(binding.getVariable(property));
  }
  DynamicInvokeResult result = scriptObject.tryGetProperty(property);
  if (result.isFound()) {
    return result;
  }
  return dynamicTarget.tryGetProperty(property);
}

代码示例来源:origin: org.jenkins-ci.plugins.workflow/workflow-cps-global-lib

@Nonnull
@Override
public Object getValue(@Nonnull CpsScript script) throws Exception {
  Binding binding = script.getBinding();
  Object instance;
  if (binding.hasVariable(getName())) {
    instance = binding.getVariable(getName());
  } else {
    CpsThread c = CpsThread.current();
    if (c==null)
      throw new IllegalStateException("Expected to be called from CpsThread");
    instance = c.getExecution().getShell().getClassLoader().loadClass(getName()).newInstance();
    /* We could also skip registration of vars in GroovyShellDecoratorImpl and use:
       instance = c.getExecution().getShell().parse(source(".groovy"));
      But then the source will appear in CpsFlowExecution.loadedScripts and be offered up for ReplayAction.
      We might *want* to support replay of global vars & classes at some point, but to make it actually work
      we would also need to start calling LoadStepExecution.Replacer.
    */
    binding.setVariable(getName(), instance);
  }
  return instance;
}

代码示例来源:origin: org.integratedmodelling/klab-common

private void setBindings(Binding binding, Map<String, Object> parameters) {
  for (String key : parameters.keySet()) {
    binding.setVariable(key, parameters.get(key));
  }
  for (String v : defineIfAbsent) {
    if (!binding.hasVariable(v)) {
      binding.setVariable(v, null);
    }
  }
  
  binding.setVariable("_p", parameters);
  binding.setVariable("_ns", namespace);
  binding.setVariable("_mmanager", KLAB.MMANAGER);
  binding.setVariable("_engine", KLAB.ENGINE);
  binding.setVariable("_pmanager", KLAB.PMANAGER);
  binding.setVariable("_kmanager", KLAB.KM);
  binding.setVariable("_config", KLAB.CONFIG);
  binding.setVariable("_network", KLAB.ENGINE.getNetwork());
}

相关文章