📄 proxybuilder.java
字号:
// You can redistribute this software and/or modify it under the terms of// the Ozone Library License version 1 published by ozone-db.org.//// The original code and portions created by SMB are// Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.//// $Id$package org.ozoneDB.tools.OPP.srcgen.builder;import org.ozoneDB.tools.OPP.OPPHelper;import org.ozoneDB.tools.OPP.srcgen.ClassBuilder;import org.ozoneDB.tools.OPP.srcgen.BuilderException;import org.ozoneDB.tools.OPP.srcgen.streamfactory.OutputStreamFactory;import org.ozoneDB.tools.OPP.message.MessageWriter;import org.ozoneDB.OzoneRemote;import org.ozoneDB.core.helper.ReflectionHelper;import org.ozoneDB.core.Lock;import java.io.*;import java.util.*;import java.lang.reflect.Method;/** * Builds an ozone Proxy. The code is lifted out of the proxy generator * and adapted to the builder director pattern. * * A lot oof code changes has to do with the absence of reflection dependency in the implementation of the builder * director pattern implementation. The proxy builder still depends heavily on reflection, something that could * probably be dealt with. The changes we have now is a lot of String lookup functionality for classes. * * We'd like to support static inner classes as OzoneObjects, but where should the proxy classes be defined? * Within the the proxy class of the enclosing class? If so, what if the enclosing class is not OzoneCompatible? * * @author <a href="http://www.softwarebuero.de/">SMB</a> * @author <a href="http://www.medium.net/">Medium.net</a> * @author Per Nyfelt * @author Joakim Ohlrogge */public class ProxyBuilder implements ClassBuilder { private PrintWriter out; private OutputStreamFactory osFactory; private String postfix; private Collection remoteInterfaces = new HashSet(); private boolean printStackTrace; private boolean cache; private MessageWriter msgWriter; private String className; private String proxyClassName; private Class implementationClass; private Method sortedMethods[]; private int getMethodIndex(String method, ClassBuilder.Parameter parameters[]) throws ClassNotFoundException, NoSuchMethodException { Method m = implementationClass.getMethod(method, parametersToClasses(parameters)); return ReflectionHelper.methodArrayIndex(sortedMethods, m); } private Class[] parametersToClasses(ClassBuilder.Parameter parameters[]) throws ClassNotFoundException { Class params[] = new Class[parameters.length]; for (int i = 0; i < parameters.length; i++) { ClassBuilder.Parameter parameter = parameters[i]; params[i] = getClassForType(parameter.getOrigTypeName()); } return params; } private Class getClassForType(String type) throws ClassNotFoundException { Class clType = OPPHelper.classForPrimitive(type); if (clType == null) { Class cl = Class.forName(type); return cl; } else return clType; } public ProxyBuilder(OutputStreamFactory osFactory, String postfix, boolean printStackTrace, boolean cache, MessageWriter listener) { this.osFactory = osFactory; this.postfix = postfix; this.printStackTrace = printStackTrace; this.cache = cache; this.msgWriter = listener; } private String[] getTypes(ClassBuilder.Parameter parameters[]) { String types[] = new String[parameters.length]; for (int i = 0; i < parameters.length; i++) { ClassBuilder.Parameter parameter = parameters[i]; types[i] = parameter.getType(); } return types; } private void makeCtrs() { out.print("\n"); out.print(" public " + OPPHelper.simpleClassName(proxyClassName) + "() {\n"); out.print(" super();\n"); // out.print (" System.out.println (\"Ctor...\");\n"); // out.print (" new Exception().fillInStackTrace().printStackTrace();\n"); out.print(" }\n"); out.print("\n\n"); // create default ctor that is responsible to create a pure proxy // without interacting with any database out.print(" public " + OPPHelper.simpleClassName(proxyClassName) + " (ObjectID oid,OzoneInterface link) {\n"); out.print(" super(oid,link);\n"); out.print(" }\n"); } private void makeGlobalHeader(String implementationClass) { out.print("// Proxy class generated by ozone's OPP ($Revision$).\n"); out.print("// DO NOT EDIT!\n"); out.print("\n"); if (!OPPHelper.packageName(implementationClass).equals("")) { out.print("package " + OPPHelper.packageName(implementationClass) + ";\n"); out.print("\n"); } out.print("import org.ozoneDB.*;\n"); out.print("import org.ozoneDB.core.ObjectID;\n"); out.print("import org.ozoneDB.core.Lock;\n"); out.print("import org.ozoneDB.core.ResultConverter;\n"); } private void makeLocalHeader(String proxyClassName) { out.print("\n"); out.print("/**\n"); out.print(" * This class was automatically generated by ozone's OPP.\n"); out.print(" * Do not instantiate or use this class directly.\n"); out.print(" */\n"); out.print("public final class " + OPPHelper.simpleClassName(proxyClassName) + " extends OzoneProxy"); boolean first = true; for (Iterator iter = remoteInterfaces.iterator(); iter.hasNext();) { // filter methods that are not inherited from a OzoneRemote // interface if (first) { first = false; out.print(" implements "); } else { out.print(", "); } out.print(((Class) iter.next()).getName()); } out.print(" {\n"); out.print("\n static final long serialVersionUID = 1L;\n"); } private void makeCstr(ClassBuilder.Parameter[] parameters, String[] exceptions) { // string buffer to build the signatur of this method StringBuffer signaturBuf = new StringBuffer(""); signaturBuf.append(" public " + OPPHelper.simpleClassName(proxyClassName) + " ("); // write the arguments in the declaration // skip default ctor if (parameters.length == 0) { return; } for (int i = 0; i < parameters.length; i++) { if (i != 0) { signaturBuf.append(", "); } signaturBuf.append(parameters[i].getType() + " " + parameters[i].getName()); } signaturBuf.append(")"); out.print("\n\n"); out.print(signaturBuf.toString()); //Write the exceptions in the declaration for (int i = 0; i < exceptions.length; i++) { out.print(i == 0 ? " throws " : ", "); out.print(exceptions[i]); } out.print(" {\n"); // the actual code of the method out.print(" try {\n"); out.print(" link = ExternalDatabase.forThread (Thread.currentThread());\n"); out.print(" if (link == null)\n"); out.print(" throw new TransactionException (\"Thread has not yet joined a transaction.\");\n"); out.print(" \n"); //array of arguments out.print(" Object[] args = {"); for (int i = 0; i < parameters.length; i++) { out.print(i > 0 ? ", " : ""); if (OPPHelper.isPrimitive(parameters[i].getType())) { out.print("new " + OPPHelper.wrappercodeForPrimitive(parameters[i].getType()) + "(arg" + i + ")"); } else { out.print("arg" + i); } } out.print("};\n"); String sig = ReflectionHelper.signature(getTypes(parameters)); out.print(" OzoneProxy proxy = link.createObject (\"" + className + "_Impl\", OzoneInterface.Public, null, " + sig + ", args);\n"); out.print(" remoteID = proxy.remoteID();\n"); out.print(" }\n"); //exceptions boolean alreadyCatched = false; for (int i = 0; i < exceptions.length; i++) { out.print(" catch (" + exceptions[i] + " e) {\n"); out.print(" e.fillInStackTrace();\n"); out.print(" throw e; }\n"); // out.print (" throw (" + excs[i].getName() + ")e.fillInStackTrace(); }\n"); if (exceptions[i].equals("build.lang.Exception")) { alreadyCatched = true; } } if (!alreadyCatched) { out.print(" catch (Exception e) {\n"); if (printStackTrace) { out.print(" e.printStackTrace (System.out);\n"); } out.print(" e.fillInStackTrace();\n"); out.print(" throw new UnexpectedException(e.toString()); }\n"); } out.print(" }\n"); } private void makeCreateMethod(ClassBuilder.Parameter[] parameters, String[] exceptions) { // string buffer to build the signature of this method StringBuffer signaturBuf = new StringBuffer(""); signaturBuf.append(" public static "); Class firstItf = (Class) remoteInterfaces.iterator().next(); String proxyInterface = firstItf.getName(); signaturBuf.append(proxyInterface); signaturBuf.append(" createObject("); // argumente in declaration schreiben for (int i = 0; i < parameters.length; i++) { if (i != 0) { signaturBuf.append(", "); } signaturBuf.append(parameters[i].getType() + " " + parameters[i].getName()); } if (parameters.length != 0) { signaturBuf.append(","); } signaturBuf.append("OzoneInterface link)"); out.print("\n\n"); out.print(signaturBuf.toString()); //exceptions in declaration schreiben for (int i = 0; i < exceptions.length; i++) { out.print(i == 0 ? " throws " : ", "); out.print(exceptions[i]); } out.print(" {\n"); // the actual code of the method out.print(" try {\n"); out.print(" /*\n"); out.print(" if (link == null)\n"); out.print(" throw new TransactionException (\"Thread has not yet joined a transaction.\");\n"); out.print(" */\n"); out.print(" \n"); //array of arguments out.print(" Object[] args = {"); for (int i = 0; i < parameters.length; i++) { out.print(i > 0 ? ", " : ""); if (OPPHelper.isPrimitive(parameters[i].getType())) { out.print("new " + OPPHelper.wrappercodeForPrimitive(parameters[i].getType()) + "(arg" + i + ")"); } else { out.print("arg" + i); } } out.print("};\n"); String sig = ReflectionHelper.signature(getTypes(parameters)); out.print(" OzoneProxy proxy = link.createObject (\"" + className + "\", OzoneInterface.Public, null, " + sig + ", args);\n"); out.print(" \n"); out.print(" return (" + proxyInterface + ") proxy;\n"); //exceptions boolean catchedRuntimeException = false; boolean catchedOzoneRemoteException = false; if (false) { for (int i = 0; i < exceptions.length; i++) { out.print(" } catch (" + exceptions[i] + " e) {\n"); out.print(" e.fillInStackTrace();\n"); out.print(" throw e;\n");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -