Android Open Source - anode Stub Generator






From Project

Back to project page anode.

License

The source code is released under:

Apache License

If you think the Android project anode listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright 2011-2012 Paddy Byers//from  w  w w. j  a  v  a 2  s . c o m
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */

package org.meshpoint.anode.stub;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.lang.reflect.Modifier;
import java.util.HashSet;

import org.meshpoint.anode.idl.IDLInterface;
import org.meshpoint.anode.idl.InterfaceManager;
import org.meshpoint.anode.idl.StubUtil;
import org.meshpoint.anode.idl.Types;
import org.meshpoint.anode.idl.IDLInterface.Operation;

public abstract class StubGenerator {

  /********************
   * private state
   ********************/
  
  protected InterfaceManager mgr;
  protected IDLInterface iface;
  protected File destination;
  private HashSet<String> memberNames = new HashSet<String>();
  
  /********************
   * public API
   ********************/
  
  public static class GeneratorException extends Exception {
    private static final long serialVersionUID = 5134576967433238034L;
    GeneratorException(String msg, Exception cause) {
      super(msg, cause);
    }
  }
  
  public StubGenerator(InterfaceManager mgr, IDLInterface iface, File destination) {
    this.mgr = mgr;
    this.iface = iface;
    this.destination = destination;
  }
  
  public abstract void generate() throws IOException, GeneratorException;

  /********************
   * private API
   ********************/
  
  protected void registerName(String memberName) throws GeneratorException {
    if(memberNames.contains(memberName))
      throw new GeneratorException("StubGenerator: overloaded operation or attribute name (" + memberName + ")", null);
    memberNames.add(memberName);
  }
  
  protected void writePreamble(ClassWriter lw, String className, String superclassName, String implementsName, int mode) {
    lw.writeln("/* This file has been automatically generated; do not edit */");
    lw.writeln();
    lw.writeln("package " + StubUtil.getStubPackage(mode) + ';');
    lw.writeln();
    StringBuffer header = new StringBuffer().append("public class ").append(className);
    if(superclassName != null) header.append(" extends ").append(superclassName);
    if(implementsName != null) header.append(" implements ").append(implementsName);
    lw.openScope(header.toString());
    lw.writeln();
  }
  
  protected static String getModifiers(int modifiers) {
    StringBuffer buf = new StringBuffer();
    if((modifiers & Modifier.PUBLIC) != 0)
      buf.append("public");
    if((modifiers & Modifier.STATIC) != 0) {
      if(buf.length() > 0) buf.append(' ');
      buf.append("static");
    }
    if((modifiers & Modifier.FINAL) != 0) {
      if(buf.length() > 0) buf.append(' ');
      buf.append("final");
    }
    return buf.toString();
  }

  protected String getType(int type) throws GeneratorException {
    /* array types */
    if((type & Types.TYPE_SEQUENCE) > 0)
      return getType(type & ~Types.TYPE_SEQUENCE) + "[]";
    
    if((type & Types.TYPE_ARRAY) > 0) {
      int componentType = type & ~Types.TYPE_ARRAY;
      if(componentType < Types.TYPE_STRING)
        return "org.w3c.dom." + getType(componentType|Types.TYPE_OBJECT) + "Array";
      return "org.w3c.dom.ObjectArray<" + getType(componentType) + ">";
    }
    
    if((type & Types.TYPE_MAP) > 0) {
      int componentType = type & ~Types.TYPE_MAP;
      return "java.util.HashMap<String, " + getType(componentType) + ">";
    }
    
    /* interface types */
    if((type & Types.TYPE_INTERFACE) > 0) {
      IDLInterface typeIface = mgr.getById(Types.type2classid(type));
      if(typeIface == null)
        throw new GeneratorException("Internal error: referenced class not found (id = " + Types.type2classid(type) + ")", null);
      return typeIface.getName();
    }
    
    /* others */
    String result;
    switch(type) {
    default:
      throw new GeneratorException("Illegal type encountered (type = " + type + ")", null);
    case Types.TYPE_UNDEFINED:
      result = "void";
      break;
    case Types.TYPE_BOOL:
      result = "boolean";
      break;
    case Types.TYPE_BYTE:
      result = "byte";
      break;
    case Types.TYPE_INT:
      result = "int";
      break;
    case Types.TYPE_LONG:
      result = "long";
      break;
    case Types.TYPE_DOUBLE:
      result = "double";
      break;
    case Types.TYPE_STRING:
      result = "String";
      break;
    case Types.TYPE_DATE:
      result = "java.util.Date";
      break;
    case Types.TYPE_OBJECT:
      result = "Object";
      break;
    case Types.TYPE_OBJECT|Types.TYPE_BOOL:
      result = "Boolean";
      break;
    case Types.TYPE_OBJECT|Types.TYPE_BYTE:
      result = "Byte";
      break;
    case Types.TYPE_OBJECT|Types.TYPE_INT:
      result = "Integer";
      break;
    case Types.TYPE_OBJECT|Types.TYPE_LONG:
      result = "Long";
      break;
    case Types.TYPE_OBJECT|Types.TYPE_DOUBLE:
      result = "Double";
      break;
    case Types.TYPE_OBJECT|Types.TYPE_STRING:
      result = "String";
      break;
    }
    return result;
  }
  
  protected static String getArgName(int argIdx) {
    return "arg" + argIdx;
  }

  protected static String getterName(String attrName) {
    return "get_" + StubUtil.uclName(attrName);
  }

  protected static String setterName(String attrName) {
    return "set_" + StubUtil.uclName(attrName);
  }

  protected static String getArgToObjectExpression(int type, String subExpr) throws GeneratorException {
    String result = subExpr;

    /* void */
    if(type == Types.TYPE_UNDEFINED)
      return result;

    /* array + interface types */
    if((type & (Types.TYPE_SEQUENCE|Types.TYPE_ARRAY|Types.TYPE_MAP|Types.TYPE_INTERFACE)) > 0)
      return result;
    
    /* others */
    switch(type) {
    default:
      throw new GeneratorException("Illegal type encountered (type = " + type + ")", null);
    case Types.TYPE_BOOL:
      result = "org.meshpoint.anode.js.JSValue.asJSBoolean(" + result + ")";
      break;
    case Types.TYPE_INT:
      result = "org.meshpoint.anode.js.JSValue.asJSNumber((long)" + result + ")";
      break;
    case Types.TYPE_LONG:
    case Types.TYPE_DOUBLE:
      result = "org.meshpoint.anode.js.JSValue.asJSNumber(" + result + ")";
      break;
    case Types.TYPE_STRING:
    case Types.TYPE_DATE:
    case Types.TYPE_OBJECT:
    case Types.TYPE_OBJECT|Types.TYPE_BOOL:
    case Types.TYPE_OBJECT|Types.TYPE_BYTE:
    case Types.TYPE_OBJECT|Types.TYPE_INT:
    case Types.TYPE_OBJECT|Types.TYPE_LONG:
    case Types.TYPE_OBJECT|Types.TYPE_DOUBLE:
    case Types.TYPE_OBJECT|Types.TYPE_STRING:
    }
    return result;
  }
  
  protected String getCastExpression(int type) throws GeneratorException {
    return "(" + getType(type) + ")";
  }
  
  protected String getObjectToArgExpression(int type, String subExpr) throws GeneratorException {
    String result;
    String jsCastExpr = "((org.meshpoint.anode.js.JSValue)" + subExpr + ')';
    switch(type) {
    default:
      result = getCastExpression(type) + subExpr;
      break;
    case Types.TYPE_BOOL:
      result = jsCastExpr + ".getBooleanValue()";
      break;
    case Types.TYPE_INT:
      result = "(int)" + jsCastExpr + ".longValue";
      break;
    case Types.TYPE_LONG:
      result = jsCastExpr + ".longValue";
      break;
    case Types.TYPE_DOUBLE:
      result = jsCastExpr + ".dblValue";
      break;
    case Types.TYPE_OBJECT:
      result = subExpr;
    }
    return result;
  }

  protected void emitMaxargsArray(ClassWriter lw, IDLInterface iface, boolean includeGetter) {
    Operation[] operations = iface.getOperations();
    int maxArgCount = 0;
    for(Operation op : operations) {
      int thisArgCount = op.args.length;
      if(thisArgCount > maxArgCount)
        maxArgCount = thisArgCount;
    }
    emitArgsArray(lw, maxArgCount, includeGetter);
  }

  protected void emitArgsArray(ClassWriter lw, int len, boolean includeGetter) {
    lw.writeln("private static Object[] __args = new Object[" + len + "];");
    lw.writeln();
    if(includeGetter) {
      lw.writeln("public static Object[] __getArgs() { return __args; }");
      lw.writeln();
    }
  }

  protected class ClassWriter {
    /**
     * Create a ClassWriter instance for the specified stub class
     * @param className class name, without package component
     * @param mode stub mode
     * @throws IOException
     */
    public ClassWriter(String className, int mode) throws IOException {
      String stubPackage = StubUtil.getStubPackage(mode).replace('.', '/');
      File packageDir = new File(destination.toString() + '/' + stubPackage);
      packageDir.mkdirs();
      if(!packageDir.exists())
        throw new IOException("Unable to create package directory (" + packageDir.toString() + ")");
      
      String classFilename = className + ".java";
      File classFile = new File(packageDir, classFilename);
      FileOutputStream fos = new FileOutputStream(classFile);
      this.ps = new PrintStream(fos);
    }
    public void writeln() { ps.println(); }
    public void writeln(String s) { ps.append(indentChars, 0, indent); ps.println(s); }
    public void writeln(String s, int indentDelta) { indent += indentDelta; writeln(s); indent -= indentDelta; }
    public void openScope(String s) { writelnStart(s); openScope(); }
    public void closeScope() { --indent; writeln("}"); }
    public void close() { ps.flush(); ps.close(); }
    private void writelnStart(String s) { ps.append(indentChars, 0, indent); ps.print(s); }
    private void writelnEnd(String s) { ps.println(s); }
    private void openScope() { writelnEnd(" {"); indent++; }
    private PrintStream ps;
    private int indent;
    private CharSequence indentChars = "\t\t\t\t\t";
  }
}




Java Source Code List

org.meshpoint.anode.AndroidContext.java
org.meshpoint.anode.AnodeActivity.java
org.meshpoint.anode.AnodeReceiver.java
org.meshpoint.anode.AnodeService.java
org.meshpoint.anode.Constants.java
org.meshpoint.anode.Isolate.java
org.meshpoint.anode.RuntimeNative.java
org.meshpoint.anode.Runtime.java
org.meshpoint.anode.bridge.BridgeNative.java
org.meshpoint.anode.bridge.Env.java
org.meshpoint.anode.bridge.Env.java
org.meshpoint.anode.bridge.FinalizeQueue.java
org.meshpoint.anode.bridge.ModuleClassLoader.java
org.meshpoint.anode.bridge.ModuleContext.java
org.meshpoint.anode.bridge.SynchronousOperation.java
org.meshpoint.anode.error.GeneralError.java
org.meshpoint.anode.error.InternalError.java
org.meshpoint.anode.error.ReferenceError.java
org.meshpoint.anode.error.ScriptError.java
org.meshpoint.anode.error.TypeError.java
org.meshpoint.anode.idl.BoundInterface.java
org.meshpoint.anode.idl.Callback.java
org.meshpoint.anode.idl.Dictionary.java
org.meshpoint.anode.idl.Dictionary.java
org.meshpoint.anode.idl.IDLInterface.java
org.meshpoint.anode.idl.IDLInterface.java
org.meshpoint.anode.idl.InterfaceManager.java
org.meshpoint.anode.idl.InterfaceManager.java
org.meshpoint.anode.idl.StubUtil.java
org.meshpoint.anode.idl.Types.java
org.meshpoint.anode.java.Array.java
org.meshpoint.anode.java.Base.java
org.meshpoint.anode.java.Base.java
org.meshpoint.anode.java.ByteArray.java
org.meshpoint.anode.java.DoubleArray.java
org.meshpoint.anode.java.IntegerArray.java
org.meshpoint.anode.java.LongArray.java
org.meshpoint.anode.java.ObjectArray.java
org.meshpoint.anode.js.JSArray.java
org.meshpoint.anode.js.JSByteArray.java
org.meshpoint.anode.js.JSDoubleArray.java
org.meshpoint.anode.js.JSFunction.java
org.meshpoint.anode.js.JSIntegerArray.java
org.meshpoint.anode.js.JSInterface.java
org.meshpoint.anode.js.JSLongArray.java
org.meshpoint.anode.js.JSObjectArray.java
org.meshpoint.anode.js.JSObject.java
org.meshpoint.anode.js.JSValue.java
org.meshpoint.anode.module.IModuleContext.java
org.meshpoint.anode.module.IModuleContext.java
org.meshpoint.anode.module.IModule.java
org.meshpoint.anode.module.IModule.java
org.meshpoint.anode.stub.DictionaryStubGenerator.java
org.meshpoint.anode.stub.PlatformStubGenerator.java
org.meshpoint.anode.stub.StubGenerator.java
org.meshpoint.anode.stub.UserStubGenerator.java
org.meshpoint.anode.stub.util.DirectoryClassLoader.java
org.meshpoint.anode.stub.util.StubGen.java
org.meshpoint.anode.type.ICollection.java
org.meshpoint.anode.type.IFunction.java
org.meshpoint.anode.type.IIndexedCollection.java
org.meshpoint.anode.util.AndroidLog.java
org.meshpoint.anode.util.ArgProcessor.java
org.meshpoint.anode.util.Log.java
org.meshpoint.anode.util.ModuleUtils.java
org.meshpoint.anode.util.PrintStreamLog.java
org.meshpoint.anode.util.TarExtractor.java
org.meshpoint.anode.util.ZipExtractor.java
org.w3c.dom.Array.java
org.w3c.dom.Array.java
org.w3c.dom.ByteArray.java
org.w3c.dom.ByteArray.java
org.w3c.dom.DoubleArray.java
org.w3c.dom.DoubleArray.java
org.w3c.dom.IntegerArray.java
org.w3c.dom.IntegerArray.java
org.w3c.dom.LongArray.java
org.w3c.dom.LongArray.java
org.w3c.dom.ObjectArray.java
org.w3c.dom.ObjectArray.java
org.w3c.dom.PrimitiveArray.java
org.w3c.dom.PrimitiveArray.java