📄 proxybuilder.java
字号:
// out.print (" throw (" + excs[i].getName() + ")e.fillInStackTrace(); }\n"); if (exceptions[i].equals("build.lang.RuntimeException")) { catchedRuntimeException = true; } else if (exceptions[i].equals("org.ozoneDB.OzoneRemoteException")) { catchedOzoneRemoteException = true; } } } else { if (true || exceptions.length > 0) { out.print(" } catch (OzoneObjectException e) {\n"); out.print(" Throwable ee = e.getCause();\n"); out.print(" \n"); for (int i = 0; i < exceptions.length; i++) { out.print(" if (ee instanceof " + exceptions[i] + ") {\n"); out.print(" throw (" + exceptions[i] + ") ee;\n"); out.print(" }\n"); out.print(" \n"); } out.print(" // Hope that the compiler is not so smart and detect these statements as unreachable if these exceptions are already matched above\n"); out.print(" if (ee instanceof RuntimeException) {\n"); out.print(" throw (RuntimeException) ee;\n"); out.print(" }\n"); out.print(" \n"); out.print(" if (ee instanceof Error) {\n"); out.print(" throw (Error) ee;\n"); out.print(" }\n"); out.print(" \n"); out.print(" throw e; // Unhandled exception.\n"); } } if (!catchedOzoneRemoteException) { out.print(" } catch (OzoneRemoteException ore) {\n"); if (printStackTrace) { out.print(" ore.printStackTrace(System.out);\n"); } out.print(" ore.fillInStackTrace();\n"); out.print(" throw new UnexpectedException(ore.toString());\n"); } if (!catchedRuntimeException) { out.print(" } catch (RuntimeException 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"); out.print(" }\n"); } private Collection findRemoteInterfaces(String[] interfaces) throws ClassNotFoundException { Set remoteItfs = new HashSet(); for (int i = interfaces.length - 1; i >= 0; i--) { String itfName = interfaces[i]; Class itf = Class.forName(itfName); if (OzoneRemote.class.isAssignableFrom(itf)) { remoteItfs.add(itf); } } return remoteItfs; } public void init(MessageWriter msgWriter) { this.msgWriter = msgWriter; } public void beginClass(int modifer, String fullName, String superClass, String interfaces[]) throws BuilderException { msgWriter.startGeneration("Generating proxy for: " + fullName); try { out = new PrintWriter(osFactory.newInstance(fullName + postfix)); } catch (IOException e) { throw new BuilderException(e); } className = fullName; proxyClassName = fullName + postfix; try { implementationClass = Class.forName(fullName); sortedMethods = ReflectionHelper.methodsOfClass(implementationClass); remoteInterfaces = findRemoteInterfaces(interfaces); } catch (ClassNotFoundException e) { throw new BuilderException(e); } makeGlobalHeader(fullName); makeLocalHeader(fullName + postfix); makeCtrs(); } public void makeConstructor(int modifier, ClassBuilder.Parameter parameters[], String exceptions[]) throws BuilderException { makeCstr(parameters, exceptions); makeCreateMethod(parameters, exceptions); } public void makeMethod(int modifier, String name, ClassBuilder.Parameter parameters[], String returnType, String exceptions[], int lockLevel) throws BuilderException { // string buffer to build the signature of this method StringBuffer signaturBuf = new StringBuffer(""); String retType = returnType == null ? "void" : returnType; signaturBuf.append(" public " + retType + " " + name + "("); //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()); } signaturBuf.append(")"); String signaturStr = signaturBuf.toString(); /* I believe that this is not an issue any more //The getMethods() method returns methods twice, if they are //declared in different interfaces. So we have to check if this //signatur was already proceeded. if (doneMethodsIf.get(signaturStr) != null) { return; } */ out.print("\n\n"); out.print(signaturStr); //exceptions in declaration schreiben for (int i = 0; i < exceptions.length; i++) { out.print(i == 0 ? " throws " : ", "); out.print(exceptions[i]); } out.print(" {\n"); //implementation { boolean update = lockLevel != (Lock.LEVEL_READ); //System.out.println("looking at " + m.getName() + ", update is " + update + ", interface has method= " + updateMethodsIf.contains(m.getName())); //msgWriter.info(" " + className + "." + name + " (" + sig + ")"); // code to directly invoke the target method if (cache) { out.print(" Object target;\n"); out.print("\n"); out.print(" try {\n"); out.print(" target = link.fetch(this, " + (update ? "Lock.LEVEL_WRITE" : "Lock.LEVEL_READ") + ");\n"); 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"); out.print("\n"); out.print(" if (target!=null) {\n"); // convert arguments if needed for (int i = 0; i < parameters.length; i++) { if (!OPPHelper.isPrimitive(parameters[i].getType())) { out.print(" " + parameters[i].getName() + " = (" + parameters[i].getType() + ") ResultConverter.substituteOzoneCompatibles(" + parameters[i].getName() + ");\n"); } } String ozoneInterface = ((Class) remoteInterfaces.iterator().next()).getName(); msgWriter.debug("Interface = " + ozoneInterface); if (returnType != null) { if (!OPPHelper.isPrimitive(returnType)) { out.print(" return (" + returnType + ") ResultConverter.substituteOzoneCompatibles(((" + ozoneInterface + ") target)." + name + "("); } else { out.print(" return ((" + ozoneInterface + ") target)." + name + "("); } } else { out.print(" ((" + ozoneInterface + ") target)." + name + "("); } for (int i = 0; i < parameters.length; i++) { out.print(i > 0 ? ", " : ""); out.print("arg" + i); } if (returnType != null) { if (!OPPHelper.isPrimitive(returnType)) { out.print(")"); } } out.print(");\n"); out.print(" } else {\n"); } //array of arguments out.print(" try {\n"); 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()) + "(" + parameters[i].getName() + ")"); } else { out.print("arg" + i); } } out.print("};\n"); try { out.print(" Object result = link.invoke(this, " + getMethodIndex(name, parameters) + ", args, " + (update ? "Lock.LEVEL_WRITE" : "Lock.LEVEL_READ") + ");\n"); } catch (ClassNotFoundException e) { throw new BuilderException(e); } catch (NoSuchMethodException e) { throw new BuilderException(e); } // out.print (" result = link.invoke (this, \"" + m.getName() + "\", " + sig + ", args, " + (update ? "Lock.LEVEL_WRITE" : "Lock.LEVEL_READ") + ");\n"); // return value if (returnType != null) { if (OPPHelper.isPrimitive(returnType)) { out.print(" return " + OPPHelper.returncodeForPrimitive(returnType, "result") + ";\n"); } else { out.print(" return (" + returnType + ") result;\n"); } } // user defined exceptions boolean excAlreadyCatched = false; boolean rtAlreadyCatched = false; out.print(" } catch (OzoneObjectException e) {\n"); out.print(" Throwable ee = e.getCause();\n"); out.print(" \n"); for (int i = 0; i < exceptions.length; i++) { out.print(" if (ee instanceof " + exceptions[i] + ") {\n"); out.print(" throw (" + exceptions[i] + ") ee;\n"); out.print(" }\n"); out.print(" \n"); } out.print(" // Hope that the compiler is not so smart and detect these statements as unreachable if these exceptions are already matched above\n"); out.print(" if (ee instanceof RuntimeException) {\n"); out.print(" throw (RuntimeException) ee;\n"); out.print(" }\n"); out.print(" \n"); out.print(" if (ee instanceof Error) {\n"); out.print(" throw (Error) ee;\n"); out.print(" }\n"); out.print(" \n"); out.print(" throw e; // Unhandled exception.\n"); // runtime exceptions if (!rtAlreadyCatched && !excAlreadyCatched) { out.print(" } catch (RuntimeException e) {\n"); if (printStackTrace) { out.print(" e.printStackTrace(System.out);\n"); } out.print(" e.fillInStackTrace();\n"); out.print(" throw e;\n"); } // all exceptions left if (!excAlreadyCatched) { 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"); if (cache) { out.print(" }\n"); } } out.print(" }\n"); } public void endClass() throws BuilderException { out.println("}"); out.close(); msgWriter.endGeneration(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -