001 /**
002 * www.jcoverage.com
003 * Copyright (C)2003 jcoverage ltd.
004 *
005 * This file is part of jcoverage.
006 *
007 * jcoverage is free software; you can redistribute it and/or modify
008 * it under the terms of the GNU General Public License as published
009 * by the Free Software Foundation; either version 2 of the License,
010 * or (at your option) any later version.
011 *
012 * jcoverage is distributed in the hope that it will be useful, but
013 * WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * General Public License for more details.
016 *
017 * You should have received a copy of the GNU General Public License
018 * along with jcoverage; if not, write to the Free Software
019 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
020 * USA
021 *
022 */
023 package com.jcoverage.util;
024
025 import java.io.FileWriter;
026 import java.io.InputStream;
027 import java.io.IOException;
028 import java.io.PrintWriter;
029
030 import org.apache.bcel.Repository;
031
032 import org.apache.bcel.classfile.JavaClass;
033
034 import org.apache.bcel.verifier.VerificationResult;
035 import org.apache.bcel.verifier.Verifier;
036 import org.apache.bcel.verifier.VerifierFactory;
037
038 import org.apache.log4j.Logger;
039
040 public class JavaClassHelper {
041 static Logger logger=Logger.getLogger(JavaClassHelper.class);
042
043 public static JavaClass newJavaClass(InputStream clazz,String name) throws IOException {
044 return ClassParserHelper.newClassParser(clazz,name).parse();
045 }
046
047 public static JavaClass newJavaClass(Class cl) throws IOException {
048 return ClassParserHelper.newClassParser(cl).parse();
049 }
050
051 public static void dump(JavaClass javaClass) throws IOException {
052 FileWriter fw=null;
053 try {
054 logger.debug("dumping: "+javaClass.getClassName()+" to: "+ClassHelper.getBaseName(javaClass.getClassName())+".code");
055
056 fw=new FileWriter(ClassHelper.getBaseName(javaClass.getClassName())+".code");
057 PrintWriter pw=new PrintWriter(fw);
058
059 pw.println(javaClass);
060 pw.println(javaClass.getConstantPool());
061 for(int i=0;i<javaClass.getMethods().length;i++) {
062 pw.println(javaClass.getMethods()[i]);
063 if(javaClass.getMethods()[i].getCode()!=null) {
064 pw.println(javaClass.getMethods()[i].getCode().toString(true));
065 }
066 }
067
068 pw.close();
069 } finally {
070 if(fw!=null) {
071 try {
072 fw.close();
073 } catch(IOException ex) {
074 if(logger.isDebugEnabled()) {
075 logger.debug(ex);
076 }
077 }
078 fw=null;
079 }
080 }
081 }
082 }