📄 compile.java
字号:
/*Copyright (c) 2003-2005, Dennis M. SosnoskiAll rights reserved.Redistribution and use in source and binary forms, with or without modification,are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of JiBX nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" ANDANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIEDWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE AREDISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FORANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ONANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THISSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*/package org.jibx.binding;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.IOException;import java.net.URL;import java.net.URLClassLoader;import org.apache.bcel.classfile.Method;import org.apache.bcel.verifier.VerificationResult;import org.apache.bcel.verifier.Verifier;import org.apache.bcel.verifier.VerifierFactory;import org.jibx.binding.classes.BoundClass;import org.jibx.binding.classes.BranchWrapper;import org.jibx.binding.classes.ClassCache;import org.jibx.binding.classes.ClassFile;import org.jibx.binding.classes.MungedClass;import org.jibx.binding.def.BindingDefinition;import org.jibx.runtime.JiBXException;/** * Binding compiler. This version checks the modified and generated classes * by loading them and listing method information. * * @author Dennis M. Sosnoski * @version 1.0 */ public class Compile{ private boolean m_verbose; private boolean m_load; private boolean m_verify; private boolean m_trackBranches; private boolean m_errorOverride; private boolean m_skipValidate; /** * Default constructor. This just initializes all options disabled. */ public Compile() {} /** * Constructor with settings specified. * * @param verbose report binding details and results * @param load test load modified classes to validate * @param verify use BCEL validation of modified classes * @param track keep tracking information for source of branch generation * @param over override code generation error handling */ public Compile(boolean verbose, boolean load, boolean verify, boolean track, boolean over) { m_verbose = verbose; m_load = load; m_verify = verify; m_trackBranches = track; m_errorOverride = over; } /** * Verify generated and modified files using BCEL verifier. This provides a * more comprehensive listing of errors than just loading a class in the * JVM. * * @param file information for class to be verified * @return <code>true</code> if successfully verified, <code>false</code> if * problem found (automatically reported) */ private boolean verifyBCEL(ClassFile file) { try { // construct verifier for class file Verifier verifier = VerifierFactory.getVerifier(file.getName()); // run validation in stages with error handling for each stage boolean verified = false; VerificationResult vr = verifier.doPass1(); if (vr.getStatus() == VerificationResult.VERIFIED_OK) { vr = verifier.doPass2(); if (vr.getStatus() == VerificationResult.VERIFIED_OK) { Method[] methods = file.getRawClass().getMethods(); for (int j = 0; j < methods.length; j++) { vr = verifier.doPass3a(j); if (vr.getStatus() == VerificationResult.VERIFIED_OK) { vr = verifier.doPass3b(j); } if (vr.getStatus() == VerificationResult.VERIFIED_OK) { verified = true; } else { System.out.println ("Verification failure on method " + methods[j].getName() + " of class " + file.getName() + ":"); System.out.println(" " + vr.toString()); } } } else { System.out.println("Verification failure on class " + file.getName() + ":"); System.out.println(" " + vr.toString()); } } else { System.out.println("Verification failure on class " + file.getName() + ":"); System.out.println(" " + vr.toString()); } return verified; } catch (Exception ex) { // catch BCEL errors System.out.println("BCEL failure:"); ex.printStackTrace(); return false; } } /** * Output all generated and modified class files. Writes the actual files, * and optionally verifies and/or reports the changes. * * @param paths array of paths used for loading files * @exception JiBXException if error in processing the binding definition * @exception IOException if path cannot be accessed */ private void handleOutput(String[] paths) throws JiBXException, IOException { // output the modified class files ClassFile[][] lists = MungedClass.fixChanges(true); // report modified file results to user ClassFile[] files = lists[0]; if (m_verbose) { System.out.println("\nWrote " + files.length + " files"); } if (m_verbose || m_load) { // generate class paths as URLs if needed for test loading URL[] urls = null; if (m_load) { urls = new URL[paths.length]; for (int i = 0; i < urls.length; i++) { urls[i] = new File(paths[i]).toURL(); } } for (int i = 0; i < files.length; i++) { // write class file to bytes ClassFile file = files[i]; ByteArrayOutputStream bos = new ByteArrayOutputStream(); file.writeFile(bos); byte[] bytes = bos.toByteArray(); if(m_verbose){ System.out.println("\n " + file.getName() + " output file size is " + bytes.length + " bytes"); } // verify using BCEL verifier if (m_verify) { verifyBCEL(file); } // load to JVM and list method information from class if (m_load) { DirectLoader cloader = new DirectLoader(urls); Class clas = cloader.load(file.getName(), bytes); if (m_verbose) { java.lang.reflect.Method[] methods = clas.getDeclaredMethods(); System.out.println(" Found " + methods.length + " methods:"); for (int j = 0; j < methods.length; j++) { java.lang.reflect.Method method = methods[j]; System.out.println(" " + method.getReturnType().getName() + " " + method.getName()); } } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -