Java Code Examples for org.codehaus.groovy.runtime.InvokerHelper#getProperty()

The following examples show how to use org.codehaus.groovy.runtime.InvokerHelper#getProperty() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RestrictiveGroovyInterceptor.java    From cerberus-source with GNU General Public License v3.0 6 votes vote down vote up
private static boolean hasProperty(Object object, String property) {
    if (InvokerHelper.getMetaClass(object).hasProperty(object, property) != null) {
        return true;
    }

    // The only way to be sure whether something is handled as a property in
    // Groovy is to actually get it and catch a MissingPropertyException.
    // But this actually accesses the property (-> side effects?)!
    // Here this is no problem, since we only disallow some write access...
    // The only allowed class with side effects should be InstanceAccessor,
    // which is in "allAllowedClasses" and thus shouldn't reach here
    try {
        InvokerHelper.getProperty(object, property);
        return true;
    } catch (MissingPropertyException e) {
        return false;
    }
}
 
Example 2
Source File: BindPath.java    From groovy with Apache License 2.0 6 votes vote down vote up
private Object extractNewValue(Object newObject) {
    Object newValue;
    try {
        newValue = InvokerHelper.getProperty(newObject, propertyName);

    } catch (MissingPropertyException mpe) {
        //todo we should flag this when the path is created that this is a field not a prop...
        // try direct method...
        try {
            newValue = InvokerHelper.getAttribute(newObject, propertyName);
            if (newValue instanceof Reference) {
                newValue = ((Reference) newValue).get();
            }
        } catch (Exception e) {
            //LOGME?
            newValue = null;
        }
    }
    return newValue;
}
 
Example 3
Source File: DefaultRunners.java    From groovy with Apache License 2.0 6 votes vote down vote up
/**
 * Run the specified class extending TestCase as a unit test.
 * This is done through reflection, to avoid adding a dependency to the JUnit framework.
 * Otherwise, developers embedding Groovy and using GroovyShell to load/parse/compile
 * groovy scripts and classes would have to add another dependency on their classpath.
 *
 * @param scriptClass the class to be run as a unit test
 * @param loader the class loader
 */
@Override
public Object run(Class<?> scriptClass, GroovyClassLoader loader) {
    try {
        Class<?> junitCoreClass = loader.loadClass("org.junit.runner.JUnitCore");
        Object result = InvokerHelper.invokeStaticMethod(junitCoreClass,
                "runClasses", new Object[]{scriptClass});
        System.out.print("JUnit 4 Runner, Tests: " + InvokerHelper.getProperty(result, "runCount"));
        System.out.print(", Failures: " + InvokerHelper.getProperty(result, "failureCount"));
        System.out.println(", Time: " + InvokerHelper.getProperty(result, "runTime"));
        List<?> failures = (List<?>) InvokerHelper.getProperty(result, "failures");
        for (Object f : failures) {
            System.out.println("Test Failure: " + InvokerHelper.getProperty(f, "description"));
            System.out.println(InvokerHelper.getProperty(f, "trace"));
        }
        return result;
    } catch (ClassNotFoundException e) {
        throw new GroovyRuntimeException("Error running JUnit 4 test.", e);
    }
}
 
Example 4
Source File: ObjectGraphBuilder.java    From groovy with Apache License 2.0 5 votes vote down vote up
public void setChild(Object parent, Object child, String parentName, String propertyName) {
    try {
        Object property = InvokerHelper.getProperty(parent, propertyName);
        if (property != null && Collection.class.isAssignableFrom(property.getClass())) {
            ((Collection) property).add(child);
        } else {
            InvokerHelper.setProperty(parent, propertyName, child);
        }
    } catch (MissingPropertyException mpe) {
        // ignore
    }
}
 
Example 5
Source File: Reference.java    From groovy with Apache License 2.0 5 votes vote down vote up
public Object getProperty(String property) {
    Object value = get();
    if (value != null) {
        return InvokerHelper.getProperty(value, property);
    }
    return super.getProperty(property);
}
 
Example 6
Source File: NullCallSite.java    From groovy with Apache License 2.0 5 votes vote down vote up
public Object getProperty(Object receiver) throws Throwable {
    if (receiver == null) {
        try{
            return InvokerHelper.getProperty(NullObject.getNullObject(), name);
        } catch (GroovyRuntimeException gre) {
            throw ScriptBytecodeAdapter.unwrap(gre);
        }
    } else {
        return acceptGetProperty(receiver).getProperty(receiver);
    }
}
 
Example 7
Source File: AbstractCallSite.java    From groovy with Apache License 2.0 5 votes vote down vote up
@Override
public Object callGroovyObjectGetProperty(final Object receiver) throws Throwable {
    if (receiver == null) {
        try {
            return InvokerHelper.getProperty(NullObject.getNullObject(), name);
        } catch (GroovyRuntimeException gre) {
            throw ScriptBytecodeAdapter.unwrap(gre);
        }
    } else {
        return acceptGroovyObjectGetProperty(receiver).getProperty(receiver);
    }
}
 
Example 8
Source File: PojoMetaClassGetPropertySite.java    From groovy with Apache License 2.0 5 votes vote down vote up
public Object callGetProperty(Object receiver) throws Throwable {
    try {
        return InvokerHelper.getProperty(receiver, name);
    } catch (GroovyRuntimeException gre) {
        throw ScriptBytecodeAdapter.unwrap(gre);
    }
}
 
Example 9
Source File: PojoMetaClassGetPropertySite.java    From groovy with Apache License 2.0 5 votes vote down vote up
public final Object getProperty(Object receiver) throws Throwable {
    try {
        return InvokerHelper.getProperty(receiver, name);
    } catch (GroovyRuntimeException gre) {
        throw ScriptBytecodeAdapter.unwrap(gre);
    }
}
 
Example 10
Source File: GroovyTypeCheckingExtensionSupport.java    From groovy with Apache License 2.0 5 votes vote down vote up
@Override
public Object getProperty(final String property) {
    try {
        return InvokerHelper.getProperty(extension, property);
    } catch (Exception e) {
        return super.getProperty(property);
    }
}
 
Example 11
Source File: PropertyModel.java    From groovy with Apache License 2.0 5 votes vote down vote up
public Object getValue() {
    Object source = sourceModel.getValue();
    if (source != null) {
        return InvokerHelper.getProperty(source, property);
    }
    return null;
}
 
Example 12
Source File: SourceSetUtils.java    From transport with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Returns the {@link SourceDirectorySet} for a given {@link SourceSet} depending on the language of the sources
 */
static SourceDirectorySet getSourceDirectorySet(SourceSet sourceSet, Language language) {
  switch (language) {
    case JAVA:
      return sourceSet.getJava();
    case SCALA:
      Convention sourceSetConvention = (Convention) InvokerHelper.getProperty(sourceSet, "convention");
      ScalaSourceSet scalaSourceSet = sourceSetConvention.getPlugin(ScalaSourceSet.class);
      return scalaSourceSet.getScala();
    default:
      throw new UnsupportedOperationException("Language " + language + " not supported");
  }
}
 
Example 13
Source File: Web3jPlugin.java    From web3j-gradle-plugin with Apache License 2.0 5 votes vote down vote up
private File buildOutputDir(final SourceSet sourceSet) {
    final Convention convention =
            (Convention) InvokerHelper.getProperty(sourceSet, "convention");

    final SoliditySourceSet soliditySourceSet =
            (SoliditySourceSet) convention.getPlugins().get(SoliditySourceSet.NAME);

    return soliditySourceSet.getSolidity().getOutputDir();
}
 
Example 14
Source File: GroovyBeanDefinitionReader.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
public Object getProperty(String name) {
	return InvokerHelper.getProperty(this.propertyValue, name);
}
 
Example 15
Source File: GroovyBeanDefinitionReader.java    From blog_demos with Apache License 2.0 4 votes vote down vote up
public Object getProperty(String name) {
	return InvokerHelper.getProperty(this.propertyValue, name);
}
 
Example 16
Source File: GroovyBeanDefinitionReader.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public Object getProperty(String name) {
	return InvokerHelper.getProperty(this.propertyValue, name);
}
 
Example 17
Source File: Closure.java    From groovy with Apache License 2.0 4 votes vote down vote up
public Object getProperty(final String property) {
    if ("delegate".equals(property)) {
        return getDelegate();
    }
    if ("owner".equals(property)) {
        return getOwner();
    }
    if ("maximumNumberOfParameters".equals(property)) {
        return getMaximumNumberOfParameters();
    }
    if ("parameterTypes".equals(property)) {
        return getParameterTypes();
    }
    if ("metaClass".equals(property)) {
        return getMetaClass();
    }
    if ("class".equals(property)) {
        return getClass();
    }
    if ("directive".equals(property)) {
        return getDirective();
    }
    if ("resolveStrategy".equals(property)) {
        return getResolveStrategy();
    }
    if ("thisObject".equals(property)) {
        return getThisObject();
    }
    switch(resolveStrategy) {
        case DELEGATE_FIRST:
            return getPropertyDelegateFirst(property);
        case DELEGATE_ONLY:
            return InvokerHelper.getProperty(this.delegate, property);
        case OWNER_ONLY:
            return InvokerHelper.getProperty(this.owner, property);
        case TO_SELF:
            return super.getProperty(property);
        default:
            return getPropertyOwnerFirst(property);
    }
}
 
Example 18
Source File: Web3jPlugin.java    From web3j-gradle-plugin with Apache License 2.0 4 votes vote down vote up
/**
 * Configures code generation tasks for the Solidity source sets defined in the project (e.g.
 * main, test).
 *
 * <p>The generated task name for the <code>main</code> source set will be <code>
 * generateContractWrappers</code>, and for <code>test</code> <code>generateTestContractWrappers
 * </code>.
 */
private void configure(final Project project, final SourceSet sourceSet) {

    final Web3jExtension extension =
            (Web3jExtension) InvokerHelper.getProperty(project, Web3jExtension.NAME);

    final File outputDir = buildSourceDir(extension, sourceSet);

    // Add source set to the project Java source sets
    sourceSet.getJava().srcDir(outputDir);

    final String srcSetName =
            sourceSet.getName().equals("main")
                    ? ""
                    : capitalize((CharSequence) sourceSet.getName());

    final String generateTaskName = "generate" + srcSetName + "ContractWrappers";

    final GenerateContractWrappers task =
            project.getTasks().create(generateTaskName, GenerateContractWrappers.class);

    // Set the sources for the generation task
    task.setSource(buildSourceDirectorySet(sourceSet));
    task.setDescription(
            "Generates " + sourceSet.getName() + " Java contract wrappers from Solidity ABIs.");

    // Set the task output directory
    task.getOutputs().dir(outputDir);

    // Set the task generated package name, classpath and group
    task.setGeneratedJavaPackageName(extension.getGeneratedPackageName());
    task.setUseNativeJavaTypes(extension.getUseNativeJavaTypes());
    task.setGroup(Web3jExtension.NAME);

    // Set task excluded contracts
    task.setExcludedContracts(extension.getExcludedContracts());
    task.setIncludedContracts(extension.getIncludedContracts());

    // Set the contract addresses length (default 160)
    task.setAddressLength(extension.getAddressBitLength());

    task.dependsOn(
            project.getTasks()
                    .withType(SolidityCompile.class)
                    .named("compile" + srcSetName + "Solidity"));

    final SourceTask compileJava =
            (SourceTask) project.getTasks().getByName("compile" + srcSetName + "Java");

    compileJava.source(task.getOutputs().getFiles().getSingleFile());
    compileJava.dependsOn(task);
}
 
Example 19
Source File: GroovyBeanDefinitionReader.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
public Object getProperty(String name) {
	return InvokerHelper.getProperty(this.propertyValue, name);
}
 
Example 20
Source File: GroovyBeanDefinitionReader.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
public Object getProperty(String name) {
	return InvokerHelper.getProperty(this.propertyValue, name);
}