Search in sources :

Example 6 with MethodClosure

use of org.codehaus.groovy.runtime.MethodClosure in project ysoserial by frohoff.

the class Groovy1 method getObject.

public InvocationHandler getObject(final String command) throws Exception {
    final ConvertedClosure closure = new ConvertedClosure(new MethodClosure(command, "execute"), "entrySet");
    final Map map = Gadgets.createProxy(closure, Map.class);
    final InvocationHandler handler = Gadgets.createMemoizedInvocationHandler(map);
    return handler;
}
Also used : ConvertedClosure(org.codehaus.groovy.runtime.ConvertedClosure) MethodClosure(org.codehaus.groovy.runtime.MethodClosure) Map(java.util.Map) InvocationHandler(java.lang.reflect.InvocationHandler)

Example 7 with MethodClosure

use of org.codehaus.groovy.runtime.MethodClosure in project gradle-apt-plugin by tbroyer.

the class AptEclipsePlugin method configureEclipse.

/**
 * Inspired by
 * https://github.com/mkarneim/pojobuilder/wiki/Enabling-PojoBuilder-for-Eclipse-Using-Gradle
 */
private void configureEclipse(final Project project, final SourceSet mainSourceSet, final SourceSet testSourceSet) {
    final EclipseModel eclipseModel = project.getExtensions().getByType(EclipseModel.class);
    project.afterEvaluate(new Action<Project>() {

        @Override
        public void execute(final Project project) {
            eclipseModel.getClasspath().getPlusConfigurations().addAll(Arrays.asList(project.getConfigurations().getByName(new DslObject(mainSourceSet).getConvention().getPlugin(AptPlugin.AptSourceSetConvention.class).getCompileOnlyConfigurationName()), project.getConfigurations().getByName(new DslObject(testSourceSet).getConvention().getPlugin(AptPlugin.AptSourceSetConvention.class).getCompileOnlyConfigurationName())));
        }
    });
    if (project.getTasks().findByName("eclipseJdtApt") == null) {
        GenerateEclipseJdtApt task = project.getTasks().create("eclipseJdtApt", GenerateEclipseJdtApt.class, new Action<GenerateEclipseJdtApt>() {

            @Override
            public void execute(GenerateEclipseJdtApt generateEclipseJdtApt) {
                generateEclipseJdtApt.setDescription("Generates the Eclipse JDT APT settings file.");
                generateEclipseJdtApt.setInputFile(project.file(".settings/org.eclipse.jdt.apt.core.prefs"));
                generateEclipseJdtApt.setOutputFile(project.file(".settings/org.eclipse.jdt.apt.core.prefs"));
                final EclipseJdtApt jdtApt = generateEclipseJdtApt.getJdtApt();
                new DslObject(eclipseModel.getJdt()).getConvention().getPlugins().put("net.ltgt.apt-eclipse", new JdtAptConvention(jdtApt));
                ConventionMapping conventionMapping = new DslObject(jdtApt).getConventionMapping();
                conventionMapping.map("aptEnabled", new Callable<Boolean>() {

                    @Override
                    public Boolean call() throws Exception {
                        return project.getTasks().findByName(mainSourceSet.getCompileJavaTaskName()).getConvention().getPlugin(AptPlugin.AptConvention.class).getAptOptions().isAnnotationProcessing();
                    }
                });
                conventionMapping.map("genSrcDir", new Callable<File>() {

                    @Override
                    public File call() throws Exception {
                        return project.file(".apt_generated");
                    }
                });
                conventionMapping.map("processorOptions", new Callable<Map<String, ?>>() {

                    @Override
                    public Map<String, ?> call() throws Exception {
                        return project.getTasks().findByName(mainSourceSet.getCompileJavaTaskName()).getConvention().getPlugin(AptPlugin.AptConvention.class).getAptOptions().getProcessorArgs();
                    }
                });
                eclipseModel.getJdt().getFile().withProperties(// withProperties(Action) overload was added in Gradle 2.14
                new MethodClosure(new Action<Properties>() {

                    @Override
                    public void execute(Properties properties) {
                        properties.setProperty("org.eclipse.jdt.core.compiler.processAnnotations", jdtApt.isAptEnabled() ? "enabled" : "disabled");
                    }
                }, "execute"));
            }
        });
        project.getTasks().getByName("eclipse").dependsOn(task);
        Delete cleanTask = project.getTasks().create("cleanEclipseJdtApt", Delete.class);
        cleanTask.delete(getOutputs(task));
        project.getTasks().getByName("cleanEclipse").dependsOn(cleanTask);
    }
    if (project.getTasks().findByName("eclipseFactorypath") == null) {
        GenerateEclipseFactorypath task = project.getTasks().create("eclipseFactorypath", GenerateEclipseFactorypath.class, new Action<GenerateEclipseFactorypath>() {

            @Override
            public void execute(GenerateEclipseFactorypath generateEclipseFactorypath) {
                generateEclipseFactorypath.setDescription("Generates the Eclipse factorypath file.");
                generateEclipseFactorypath.setInputFile(project.file(".factorypath"));
                generateEclipseFactorypath.setOutputFile(project.file(".factorypath"));
                EclipseFactorypath factorypath = generateEclipseFactorypath.getFactorypath();
                new DslObject(eclipseModel).getConvention().getPlugins().put("net.ltgt.apt-eclipse", new FactorypathConvention(factorypath));
                factorypath.setPlusConfigurations(new ArrayList<>(Arrays.asList(project.getConfigurations().getByName(new DslObject(mainSourceSet).getConvention().getPlugin(AptPlugin.AptSourceSetConvention.class).getAnnotationProcessorConfigurationName()), project.getConfigurations().getByName(new DslObject(testSourceSet).getConvention().getPlugin(AptPlugin.AptSourceSetConvention.class).getAnnotationProcessorConfigurationName()))));
                generateEclipseFactorypath.dependsOn(factorypath.getPlusConfigurations().toArray());
            }
        });
        project.getTasks().getByName("eclipse").dependsOn(task);
        Delete cleanTask = project.getTasks().create("cleanEclipseFactorypath", Delete.class);
        cleanTask.delete(getOutputs(task));
        project.getTasks().getByName("cleanEclipse").dependsOn(cleanTask);
    }
}
Also used : Delete(org.gradle.api.tasks.Delete) DslObject(org.gradle.api.internal.plugins.DslObject) ArrayList(java.util.ArrayList) MethodClosure(org.codehaus.groovy.runtime.MethodClosure) Properties(java.util.Properties) ConventionMapping(org.gradle.api.internal.ConventionMapping) Callable(java.util.concurrent.Callable) Project(org.gradle.api.Project) EclipseModel(org.gradle.plugins.ide.eclipse.model.EclipseModel)

Example 8 with MethodClosure

use of org.codehaus.groovy.runtime.MethodClosure in project gradle-apt-plugin by tbroyer.

the class AptIdeaPlugin method configureIdeaProject.

private void configureIdeaProject(final Project project) {
    if (project.getParent() == null) {
        final IdeaProject ideaProject = project.getExtensions().getByType(IdeaModel.class).getProject();
        final ProjectAptConvention apt = new ProjectAptConvention();
        new DslObject(ideaProject).getConvention().getPlugins().put("net.ltgt.apt-idea", apt);
        ideaProject.getIpr().withXml(// withXml(Action) overload was added in Gradle 2.14
        new MethodClosure(new Action<XmlProvider>() {

            @Override
            public void execute(XmlProvider xmlProvider) {
                if (!apt.isConfigureAnnotationProcessing()) {
                    return;
                }
                for (Object it : (NodeList) xmlProvider.asNode().get("component")) {
                    Node compilerConfiguration = (Node) it;
                    if (!Objects.equals(compilerConfiguration.attribute("name"), "CompilerConfiguration")) {
                        continue;
                    }
                    for (Object n : (NodeList) compilerConfiguration.get("annotationProcessing")) {
                        compilerConfiguration.remove((Node) n);
                    }
                    Node annotationProcessing = compilerConfiguration.appendNode("annotationProcessing");
                    Map<String, Object> profileAttributes = new LinkedHashMap<>();
                    profileAttributes.put("name", "Default");
                    profileAttributes.put("enabled", true);
                    profileAttributes.put("default", true);
                    Node profile = annotationProcessing.appendNode("profile", profileAttributes);
                    // XXX: this assumes that all subprojects use the same name for their
                    // buildDir
                    profile.appendNode("sourceOutputDir", Collections.singletonMap("name", project.relativePath(project.getBuildDir()) + "/generated/source/apt/" + SourceSet.MAIN_SOURCE_SET_NAME));
                    profile.appendNode("sourceTestOutputDir", Collections.singletonMap("name", project.relativePath(project.getBuildDir()) + "/generated/source/apt/" + SourceSet.TEST_SOURCE_SET_NAME));
                    profile.appendNode("outputRelativeToContentRoot", Collections.singletonMap("value", true));
                    profile.appendNode("processorPath", Collections.singletonMap("useClasspath", true));
                }
            }
        }, "execute"));
    }
}
Also used : Action(org.gradle.api.Action) IdeaProject(org.gradle.plugins.ide.idea.model.IdeaProject) DslObject(org.gradle.api.internal.plugins.DslObject) NodeList(groovy.util.NodeList) Node(groovy.util.Node) MethodClosure(org.codehaus.groovy.runtime.MethodClosure) LinkedHashMap(java.util.LinkedHashMap) IdeaModel(org.gradle.plugins.ide.idea.model.IdeaModel) XmlProvider(org.gradle.api.XmlProvider) DslObject(org.gradle.api.internal.plugins.DslObject)

Example 9 with MethodClosure

use of org.codehaus.groovy.runtime.MethodClosure in project rest-assured by rest-assured.

the class EncoderRegistry method buildDefaultEncoderMap.

/**
 * Returns a map of default encoders.  Override this method to change
 * what encoders are registered by default.  You can of course call
 * <code>super.buildDefaultEncoderMap()</code> and then add or remove
 * from that result as well.
 */
protected Map<String, Closure> buildDefaultEncoderMap() {
    Map<String, Closure> encoders = new HashMap<String, Closure>();
    encoders.put(ContentType.BINARY.toString(), new MethodClosure(this, "encodeStream"));
    encoders.put(ContentType.TEXT.toString(), new MethodClosure(this, "encodeText"));
    encoders.put(ContentType.URLENC.toString(), new MethodClosure(this, "encodeForm"));
    Closure encClosure = new MethodClosure(this, "encodeXML");
    for (String ct : ContentType.XML.getContentTypeStrings()) encoders.put(ct, encClosure);
    encoders.put(ContentType.HTML.toString(), encClosure);
    encClosure = new MethodClosure(this, "encodeJSON");
    for (String ct : ContentType.JSON.getContentTypeStrings()) encoders.put(ct, encClosure);
    return encoders;
}
Also used : MethodClosure(org.codehaus.groovy.runtime.MethodClosure) Closure(groovy.lang.Closure) FileReader.readToString(io.restassured.internal.support.FileReader.readToString) GString(groovy.lang.GString) MethodClosure(org.codehaus.groovy.runtime.MethodClosure)

Example 10 with MethodClosure

use of org.codehaus.groovy.runtime.MethodClosure in project groovy-core by groovy.

the class ClosureMetaMethod method createMethodList.

public static List<MetaMethod> createMethodList(final String name, final Class declaringClass, final Closure closure) {
    List<MetaMethod> res = new ArrayList<MetaMethod>();
    if (closure instanceof MethodClosure) {
        MethodClosure methodClosure = (MethodClosure) closure;
        Object owner = closure.getOwner();
        Class ownerClass = (Class) (owner instanceof Class ? owner : owner.getClass());
        for (CachedMethod method : ReflectionCache.getCachedClass(ownerClass).getMethods()) {
            if (method.getName().equals(methodClosure.getMethod())) {
                MetaMethod metaMethod = new MethodClosureMetaMethod(name, declaringClass, closure, method);
                res.add(adjustParamTypesForStdMethods(metaMethod, name));
            }
        }
    } else {
        if (closure instanceof GeneratedClosure) {
            for (CachedMethod method : ReflectionCache.getCachedClass(closure.getClass()).getMethods()) {
                if (method.getName().equals("doCall")) {
                    MetaMethod metaMethod = new ClosureMetaMethod(name, declaringClass, closure, method);
                    res.add(adjustParamTypesForStdMethods(metaMethod, name));
                }
            }
        } else {
            MetaMethod metaMethod = new AnonymousMetaMethod(closure, name, declaringClass);
            res.add(adjustParamTypesForStdMethods(metaMethod, name));
        }
    }
    return res;
}
Also used : MetaMethod(groovy.lang.MetaMethod) ArrayList(java.util.ArrayList) CachedMethod(org.codehaus.groovy.reflection.CachedMethod) CachedClass(org.codehaus.groovy.reflection.CachedClass) MethodClosure(org.codehaus.groovy.runtime.MethodClosure) GeneratedClosure(org.codehaus.groovy.runtime.GeneratedClosure)

Aggregations

MethodClosure (org.codehaus.groovy.runtime.MethodClosure)28 IOException (java.io.IOException)5 CachedClass (org.codehaus.groovy.reflection.CachedClass)5 GeneratedClosure (org.codehaus.groovy.runtime.GeneratedClosure)4 Test (org.junit.Test)4 Binding (groovy.lang.Binding)3 Closure (groovy.lang.Closure)3 DelegatingMetaClass (groovy.lang.DelegatingMetaClass)3 MetaClass (groovy.lang.MetaClass)3 MissingMethodException (groovy.lang.MissingMethodException)3 MissingPropertyException (groovy.lang.MissingPropertyException)3 Script (groovy.lang.Script)3 Tuple (groovy.lang.Tuple)3 PrintWriter (java.io.PrintWriter)3 Method (java.lang.reflect.Method)3 ArrayList (java.util.ArrayList)3 CompiledScript (javax.script.CompiledScript)3 ScriptException (javax.script.ScriptException)3 CompilationFailedException (org.codehaus.groovy.control.CompilationFailedException)3 InvokerInvocationException (org.codehaus.groovy.runtime.InvokerInvocationException)3