<commit_before>//
// $Id$
//
// Narya library - tools for developing networked games
// Copyright (C) 2002-2007 Three Rings Design, Inc., All Rights Reserved
// http://www.threerings.net/code/narya/
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

package com.threerings.presents.tools;

import java.io.File;
import java.io.StringWriter;

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

import java.util.HashSet;

import org.apache.velocity.VelocityContext;

import com.samskivert.util.ComparableArrayList;
import com.samskivert.util.StringUtil;

import com.threerings.presents.client.Client;
import com.threerings.presents.client.InvocationService;
import com.threerings.presents.data.ClientObject;
import com.threerings.presents.data.InvocationMarshaller;
import com.threerings.presents.dobj.InvocationResponseEvent;
import com.threerings.presents.net.Transport;
import com.threerings.presents.server.InvocationDispatcher;
import com.threerings.presents.server.InvocationException;
import com.threerings.presents.server.InvocationProvider;

/**
 * An Ant task for generating invocation service marshalling and
 * unmarshalling classes.
 * TODO: when generating the imports for exported action script files, there are just enough
 * conversions of primitive types (e.g. float -> Number), array types (e.g. int[] -> TypedArray)
 * and three rings utility types (e.g. float -> Float) to make the existing serivces work. It
 * should be possible to create a complete list of these conversions so that future services
 * can be generated without problems.
 */
public class GenServiceTask extends InvocationTask
{
    /** Used to keep track of custom InvocationListener derivations. */
    public class ServiceListener implements Comparable<ServiceListener>
    {
        public Class listener;

        public ComparableArrayList<ServiceMethod> methods =
            new ComparableArrayList<ServiceMethod>();

        /** Contains all imports required for the parameters of the methods in this listener. */
        public ImportSet imports = new ImportSet();

        public ServiceListener (Class service, Class listener)
        {
            this.listener = listener;
            Method[] methdecls = listener.getDeclaredMethods();
            for (int ii = 0; ii < methdecls.length; ii++) {
                Method m = methdecls[ii];
                // service interface methods must be public and abstract
                if (!Modifier.isPublic(m.getModifiers()) &&
                    !Modifier.isAbstract(m.getModifiers())) {
                    continue;
                }
                if (_verbose) {
                    System.out.println("Adding " + m + ", imports are " +
                        StringUtil.toString(imports));
                }
                methods.add(new ServiceMethod(m, imports));
                if (_verbose) {
                    System.out.println("Added " + m + ", imports are " +
                        StringUtil.toString(imports));
                }
            }
            methods.sort();
        }

        public int compareTo (ServiceListener other)
        {
            return getName().compareTo(other.getName());
        }

        public boolean equals (Object other)
        {
            return getClass().equals(other.getClass()) &&
                listener.equals(((ServiceListener)other).listener);
        }

        public String getName ()
        {
            String name = GenUtil.simpleName(listener, null);
            name = StringUtil.replace(name, "Listener", "");
            int didx = name.indexOf(".");
            return name.substring(didx+1);
        }
    }

    /** Used to track services for which we should not generate a provider
     * interface. */
    public class Providerless
    {
        public void setService (String className)
        {
            _providerless.add(className);
        }
    }

    /**
     * Configures the path to our ActionScript source files.
     */
    public void setAsroot (File asroot)
    {
        _asroot = asroot;
    }

    // documentation inherited
    public Providerless createProviderless ()
    {
        return new Providerless();
    }

    // documentation inherited
    protected void processService (File source, Class service)
    {
        System.out.println("Processing " + service.getName() + "...");

        // verify that the service class name is as we expect it to be
        if (!service.getName().endsWith("Service")) {
            System.err.println("Cannot process '" + service.getName() + "':");
            System.err.println(
                "Service classes must be named SomethingService.");
            return;
        }

        ServiceDescription desc = new ServiceDescription(service);
        generateMarshaller(source, desc);
        generateDispatcher(source, desc);
        if (!_providerless.contains(service.getSimpleName())) {
            generateProvider(source, desc);
        }
    }

    protected void generateMarshaller (File source, ServiceDescription sdesc)
    {
        if (_verbose) {
            System.out.println("Generating marshaller");
        }

        String sname = sdesc.sname;
        String name = StringUtil.replace(sname, "Service", "");
        String mname = StringUtil.replace(sname, "Service", "Marshaller");
        String mpackage = StringUtil.replace(
            sdesc.spackage, ".client", ".data");

        // ----------- Part I - java marshaller

        // start with all imports (service methods and listener methods)
        ImportSet imports = sdesc.constructAllImports();

        // import things marshaller will always need
        imports.add(sdesc.service);
        imports.add(Client.class);
        imports.add(InvocationMarshaller.class);
        imports.add(Transport.class);

        // if any listeners are to be present, they need the response event
        if (sdesc.listeners.size() > 0) {
            imports.add(InvocationResponseEvent.class);
        }

        // import classes contained in arrays
        imports.translateClassArrays();

        // get rid of java.lang stuff and primitives
        imports.removeGlobals();

        // get rid of all arrays (they are automatic in java)
        imports.removeArrays();

        // for each listener type, also import the corresponding marshaller
        imports.duplicateAndMunge("*Listener",
            "Service", "Marshaller",
            "Listener", "Marshaller",
            ".client.", ".data.");

        // import the parent class of Foo$Bar
        imports.swapInnerClassesForParents();

        // remove imports in our own package
        imports.removeSamePackage(mpackage);

        VelocityContext ctx = new VelocityContext();
        ctx.put("name", name);
        ctx.put("package", mpackage);
        ctx.put("methods", sdesc.methods);
        ctx.put("listeners", sdesc.listeners);
        ctx.put("imports", imports.toList());

        // determine the path to our marshaller file
        String mpath = source.getPath();
        mpath = StringUtil.replace(mpath, "Service", "Marshaller");
        mpath = replacePath(mpath, "/client/", "/data/");

        try {
            StringWriter sw = new StringWriter();
            _velocity.mergeTemplate(MARSHALLER_TMPL, "UTF-8", ctx, sw);
            writeFile(mpath, sw.toString());

        } catch (Exception e) {
            System.err.println("Failed processing template");
            e.printStackTrace(System.err);
        }

        // if we're not configured with an ActionScript source root, don't
        // generate the ActionScript versions
        if (_asroot == null) {
            return;
        }

        // ----------- Part II - as marshaller

        // start with the service method imports
        imports = sdesc.imports.clone();

        // add some things that marshallers just need
        imports.add(sdesc.service);
        imports.add(Client.class);
        imports.add(InvocationMarshaller.class);

        // replace inner classes with action script equivalents
        imports.translateInnerClasses();

        // replace primitive types with OOO types (required for unboxing)
        imports.replace("byte", "com.threerings.util.Byte");
        imports.replace("int", "com.threerings.util.Integer");
        imports.replace("boolean", "com.threerings.util.langBoolean");
        imports.replace("[B", "flash.utils.ByteArray");
        imports.replace("float", "com.threerings.util.Float");
        imports.replace("[I", "com.threerings.io.TypedArray");

        // ye olde special case - any method that uses a default listener
        // causes the need for the default listener marshaller
        imports.duplicateAndMunge("*.InvocationService_InvocationListener",
            "InvocationService_InvocationListener",
            "InvocationMarshaller_ListenerMarshaller",
            ".client.", ".data.");

        // any use of a listener requires the listener marshaller
        imports.pushOut("*.InvocationService_InvocationListener");
        imports.duplicateAndMunge("*Listener",
            "Service", "Marshaller",
            "Listener", "Marshaller",
            ".client.", ".data.");
        imports.popIn();

        // get rid of java.lang stuff and any remaining primitives
        imports.removeGlobals();

        if (imports.removeAll("[L*") > 0) {
            imports.add("com.threerings.io.TypedArray");
        }

        // get rid of remaining arrays
        imports.removeArrays();

        // remove imports in our own package
        imports.removeSamePackage(mpackage);

        ctx.put("imports", imports.toList());

        // now generate ActionScript versions of our marshaller
        try {
            // make sure our marshaller directory exists
            String mppath = mpackage.replace('.', File.separatorChar);
            new File(_asroot + File.separator + mppath).mkdirs();

            // generate an ActionScript version of our marshaller
            String ampath = _asroot + File.separator + mppath +
                File.separator + mname + ".as";
            StringWriter sw = new StringWriter();
            _velocity.mergeTemplate(AS_MARSHALLER_TMPL, "UTF-8", ctx, sw);
            writeFile(ampath, sw.toString());

            // ----------- Part III - as listener marshallers

            Class imlm = InvocationMarshaller.ListenerMarshaller.class;

            // now generate ActionScript versions of our listener marshallers
            // because those have to be in separate files
            for (ServiceListener listener : sdesc.listeners) {

                // start imports with just those used by listener methods
                imports = listener.imports.clone();

                // always need the super class and the listener class
                imports.add(imlm);
                imports.add(listener.listener);

                // replace '$' with '_' for action script naming convention
                imports.translateInnerClasses();

                // convert primitive java types to ooo util types
                imports.replace("long", "com.threerings.util.Long");

                // convert object arrays to typed arrays
                if (imports.removeAll("[L*") > 0) {
                    imports.add("com.threerings.io.TypedArray");
                }

                // get rid of remaining primitives and java.lang types
                imports.removeGlobals();

                // remove imports in our own package
                imports.removeSamePackage(mpackage);

                ctx.put("imports", imports.toList());
                ctx.put("listener", listener);
                sw = new StringWriter();
                _velocity.mergeTemplate(
                    AS_LISTENER_MARSHALLER_TMPL, "UTF-8", ctx, sw);
                String aslpath = _asroot + File.separator + mppath +
                    File.separator + mname + "_" +
                    listener.getName() + "Marshaller.as";
                writeFile(aslpath, sw.toString());
            }

        } catch (Exception e) {
            System.err.println("Failed processing template");
            e.printStackTrace(System.err);
        }

        // ----------- Part IV - as service

        // then make some changes to the context and generate ActionScript
        // versions of the service interface itself

        // start with the service methods' imports
        imports = sdesc.imports.clone();

        // add some things required by action script
        imports.add(Client.class);
        imports.add(InvocationService.class);

        // allow primitive types in service methods
        imports.replace("[B", "flash.utils.ByteArray");
        imports.replace("[I", "com.threerings.io.TypedArray");

        if (imports.removeAll("[L*") > 0) {
            imports.add("com.threerings.io.TypedArray");
        }

        // get rid of primitives and java.lang classes
        imports.removeGlobals();

        // get rid of remaining arrays
        imports.removeArrays();

        // change imports of Foo$Bar to Foo_Bar
        imports.translateInnerClasses();<commit_msg>weirdly, we have exactly one service that uses Integer instead of int for some reason  TODO: provide a convertAllLangTypes method instead of adding new types as they crop up<commit_after>//
// $Id$
//
// Narya library - tools for developing networked games
// Copyright (C) 2002-2007 Three Rings Design, Inc., All Rights Reserved
// http://www.threerings.net/code/narya/
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

package com.threerings.presents.tools;

import java.io.File;
import java.io.StringWriter;

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

import java.util.HashSet;

import org.apache.velocity.VelocityContext;

import com.samskivert.util.ComparableArrayList;
import com.samskivert.util.StringUtil;

import com.threerings.presents.client.Client;
import com.threerings.presents.client.InvocationService;
import com.threerings.presents.data.ClientObject;
import com.threerings.presents.data.InvocationMarshaller;
import com.threerings.presents.dobj.InvocationResponseEvent;
import com.threerings.presents.net.Transport;
import com.threerings.presents.server.InvocationDispatcher;
import com.threerings.presents.server.InvocationException;
import com.threerings.presents.server.InvocationProvider;

/**
 * An Ant task for generating invocation service marshalling and
 * unmarshalling classes.
 * TODO: when generating the imports for exported action script files, there are just enough
 * conversions of primitive types (e.g. float -> Number), array types (e.g. int[] -> TypedArray)
 * and three rings utility types (e.g. float -> Float) to make the existing serivces work. It
 * should be possible to create a complete list of these conversions so that future services
 * can be generated without problems.
 */
public class GenServiceTask extends InvocationTask
{
    /** Used to keep track of custom InvocationListener derivations. */
    public class ServiceListener implements Comparable<ServiceListener>
    {
        public Class listener;

        public ComparableArrayList<ServiceMethod> methods =
            new ComparableArrayList<ServiceMethod>();

        /** Contains all imports required for the parameters of the methods in this listener. */
        public ImportSet imports = new ImportSet();

        public ServiceListener (Class service, Class listener)
        {
            this.listener = listener;
            Method[] methdecls = listener.getDeclaredMethods();
            for (int ii = 0; ii < methdecls.length; ii++) {
                Method m = methdecls[ii];
                // service interface methods must be public and abstract
                if (!Modifier.isPublic(m.getModifiers()) &&
                    !Modifier.isAbstract(m.getModifiers())) {
                    continue;
                }
                if (_verbose) {
                    System.out.println("Adding " + m + ", imports are " +
                        StringUtil.toString(imports));
                }
                methods.add(new ServiceMethod(m, imports));
                if (_verbose) {
                    System.out.println("Added " + m + ", imports are " +
                        StringUtil.toString(imports));
                }
            }
            methods.sort();
        }

        public int compareTo (ServiceListener other)
        {
            return getName().compareTo(other.getName());
        }

        public boolean equals (Object other)
        {
            return getClass().equals(other.getClass()) &&
                listener.equals(((ServiceListener)other).listener);
        }

        public String getName ()
        {
            String name = GenUtil.simpleName(listener, null);
            name = StringUtil.replace(name, "Listener", "");
            int didx = name.indexOf(".");
            return name.substring(didx+1);
        }
    }

    /** Used to track services for which we should not generate a provider
     * interface. */
    public class Providerless
    {
        public void setService (String className)
        {
            _providerless.add(className);
        }
    }

    /**
     * Configures the path to our ActionScript source files.
     */
    public void setAsroot (File asroot)
    {
        _asroot = asroot;
    }

    // documentation inherited
    public Providerless createProviderless ()
    {
        return new Providerless();
    }

    // documentation inherited
    protected void processService (File source, Class service)
    {
        System.out.println("Processing " + service.getName() + "...");

        // verify that the service class name is as we expect it to be
        if (!service.getName().endsWith("Service")) {
            System.err.println("Cannot process '" + service.getName() + "':");
            System.err.println(
                "Service classes must be named SomethingService.");
            return;
        }

        ServiceDescription desc = new ServiceDescription(service);
        generateMarshaller(source, desc);
        generateDispatcher(source, desc);
        if (!_providerless.contains(service.getSimpleName())) {
            generateProvider(source, desc);
        }
    }

    protected void generateMarshaller (File source, ServiceDescription sdesc)
    {
        if (_verbose) {
            System.out.println("Generating marshaller");
        }

        String sname = sdesc.sname;
        String name = StringUtil.replace(sname, "Service", "");
        String mname = StringUtil.replace(sname, "Service", "Marshaller");
        String mpackage = StringUtil.replace(
            sdesc.spackage, ".client", ".data");

        // ----------- Part I - java marshaller

        // start with all imports (service methods and listener methods)
        ImportSet imports = sdesc.constructAllImports();

        // import things marshaller will always need
        imports.add(sdesc.service);
        imports.add(Client.class);
        imports.add(InvocationMarshaller.class);
        imports.add(Transport.class);

        // if any listeners are to be present, they need the response event
        if (sdesc.listeners.size() > 0) {
            imports.add(InvocationResponseEvent.class);
        }

        // import classes contained in arrays
        imports.translateClassArrays();

        // get rid of java.lang stuff and primitives
        imports.removeGlobals();

        // get rid of all arrays (they are automatic in java)
        imports.removeArrays();

        // for each listener type, also import the corresponding marshaller
        imports.duplicateAndMunge("*Listener",
            "Service", "Marshaller",
            "Listener", "Marshaller",
            ".client.", ".data.");

        // import the parent class of Foo$Bar
        imports.swapInnerClassesForParents();

        // remove imports in our own package
        imports.removeSamePackage(mpackage);

        VelocityContext ctx = new VelocityContext();
        ctx.put("name", name);
        ctx.put("package", mpackage);
        ctx.put("methods", sdesc.methods);
        ctx.put("listeners", sdesc.listeners);
        ctx.put("imports", imports.toList());

        // determine the path to our marshaller file
        String mpath = source.getPath();
        mpath = StringUtil.replace(mpath, "Service", "Marshaller");
        mpath = replacePath(mpath, "/client/", "/data/");

        try {
            StringWriter sw = new StringWriter();
            _velocity.mergeTemplate(MARSHALLER_TMPL, "UTF-8", ctx, sw);
            writeFile(mpath, sw.toString());

        } catch (Exception e) {
            System.err.println("Failed processing template");
            e.printStackTrace(System.err);
        }

        // if we're not configured with an ActionScript source root, don't
        // generate the ActionScript versions
        if (_asroot == null) {
            return;
        }

        // ----------- Part II - as marshaller

        // start with the service method imports
        imports = sdesc.imports.clone();

        // add some things that marshallers just need
        imports.add(sdesc.service);
        imports.add(Client.class);
        imports.add(InvocationMarshaller.class);

        // replace inner classes with action script equivalents
        imports.translateInnerClasses();

        // replace primitive types with OOO types (required for unboxing)
        imports.replace("byte", "com.threerings.util.Byte");
        imports.replace("int", "com.threerings.util.Integer");
        imports.replace("boolean", "com.threerings.util.langBoolean");
        imports.replace("[B", "flash.utils.ByteArray");
        imports.replace("float", "com.threerings.util.Float");
        imports.replace("[I", "com.threerings.io.TypedArray");

        // ye olde special case - any method that uses a default listener
        // causes the need for the default listener marshaller
        imports.duplicateAndMunge("*.InvocationService_InvocationListener",
            "InvocationService_InvocationListener",
            "InvocationMarshaller_ListenerMarshaller",
            ".client.", ".data.");

        // any use of a listener requires the listener marshaller
        imports.pushOut("*.InvocationService_InvocationListener");
        imports.duplicateAndMunge("*Listener",
            "Service", "Marshaller",
            "Listener", "Marshaller",
            ".client.", ".data.");
        imports.popIn();

        // get rid of java.lang stuff and any remaining primitives
        imports.removeGlobals();

        if (imports.removeAll("[L*") > 0) {
            imports.add("com.threerings.io.TypedArray");
        }

        // get rid of remaining arrays
        imports.removeArrays();

        // remove imports in our own package
        imports.removeSamePackage(mpackage);

        ctx.put("imports", imports.toList());

        // now generate ActionScript versions of our marshaller
        try {
            // make sure our marshaller directory exists
            String mppath = mpackage.replace('.', File.separatorChar);
            new File(_asroot + File.separator + mppath).mkdirs();

            // generate an ActionScript version of our marshaller
            String ampath = _asroot + File.separator + mppath +
                File.separator + mname + ".as";
            StringWriter sw = new StringWriter();
            _velocity.mergeTemplate(AS_MARSHALLER_TMPL, "UTF-8", ctx, sw);
            writeFile(ampath, sw.toString());

            // ----------- Part III - as listener marshallers

            Class imlm = InvocationMarshaller.ListenerMarshaller.class;

            // now generate ActionScript versions of our listener marshallers
            // because those have to be in separate files
            for (ServiceListener listener : sdesc.listeners) {

                // start imports with just those used by listener methods
                imports = listener.imports.clone();

                // always need the super class and the listener class
                imports.add(imlm);
                imports.add(listener.listener);

                // replace '$' with '_' for action script naming convention
                imports.translateInnerClasses();

                // convert primitive java types to ooo util types
                imports.replace("long", "com.threerings.util.Long");

                // convert object arrays to typed arrays
                if (imports.removeAll("[L*") > 0) {
                    imports.add("com.threerings.io.TypedArray");
                }

                // get rid of remaining primitives and java.lang types
                imports.removeGlobals();

                // remove imports in our own package
                imports.removeSamePackage(mpackage);

                ctx.put("imports", imports.toList());
                ctx.put("listener", listener);
                sw = new StringWriter();
                _velocity.mergeTemplate(
                    AS_LISTENER_MARSHALLER_TMPL, "UTF-8", ctx, sw);
                String aslpath = _asroot + File.separator + mppath +
                    File.separator + mname + "_" +
                    listener.getName() + "Marshaller.as";
                writeFile(aslpath, sw.toString());
            }

        } catch (Exception e) {
            System.err.println("Failed processing template");
            e.printStackTrace(System.err);
        }

        // ----------- Part IV - as service

        // then make some changes to the context and generate ActionScript
        // versions of the service interface itself

        // start with the service methods' imports
        imports = sdesc.imports.clone();

        // add some things required by action script
        imports.add(Client.class);
        imports.add(InvocationService.class);

        // allow primitive types in service methods
        imports.replace("[B", "flash.utils.ByteArray");
        imports.replace("[I", "com.threerings.io.TypedArray");

        // convert java primitive types to ooo util types
        imports.replace("java.lang.Integer", "com.threerings.util.Integer");

        if (imports.removeAll("[L*") > 0) {
            imports.add("com.threerings.io.TypedArray");
        }

        // get rid of primitives and java.lang classes
        imports.removeGlobals();

        // get rid of remaining arrays
        imports.removeArrays();

        // change imports of Foo$Bar to Foo_Bar
        imports.translateInnerClasses();
{'fraction_non_alphanumeric': 0.10324610324610324, 'fraction_numerical': 0.0023595023595023596, 'mean_word_length': 2.1607909604519775, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4247571', 'n_tokens_mistral': 7176, 'n_tokens_neox': 7018, 'n_words': 2081}