001: /*
002: * FindBugs - Find Bugs in Java programs
003: * Copyright (C) 2006, University of Maryland
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: */
019:
020: package edu.umd.cs.findbugs.asm;
021:
022: import org.objectweb.asm.Attribute;
023: import org.objectweb.asm.ClassAdapter;
024: import org.objectweb.asm.ClassReader;
025: import org.objectweb.asm.ClassVisitor;
026: import org.objectweb.asm.Label;
027: import org.objectweb.asm.MethodAdapter;
028: import org.objectweb.asm.MethodVisitor;
029:
030: public class FBClassReader extends ClassReader {
031:
032: // boolean needOffsets; // optional optimisation (not thread safe)
033:
034: public FBClassReader(byte[] b) {
035: super (b);
036: }
037:
038: public FBClassReader(byte[] b, int off, int len) {
039: super (b, off, len);
040: }
041:
042: @Override
043: public void accept(ClassVisitor cv, Attribute[] attrs, int flags) {
044: super .accept(new MyClassAdapter(cv), attrs, flags);
045: }
046:
047: @Override
048: protected Label readLabel(int offset, Label[] labels) {
049: // if (!needOffsets) return super.readLabel(offset, labels);
050: if (labels[offset] == null) {
051: for (int i = 0; i < labels.length; ++i) {
052: labels[i] = new MyLabel(i);
053: }
054: }
055: ((MyLabel) labels[offset]).realLabel = true;
056: return labels[offset];
057: }
058:
059: private static class MyClassAdapter extends ClassAdapter {
060:
061: public MyClassAdapter(ClassVisitor cv) {
062: super (cv);
063: }
064:
065: @Override
066: public MethodVisitor visitMethod(int access, String name,
067: String desc, String signature, String[] exceptions) {
068: MethodVisitor mv = super .visitMethod(access, name, desc,
069: signature, exceptions);
070: // needOffsets = mv instanceof MyMethodVisitor;
071: if (mv instanceof FBMethodVisitor) {
072: mv = new MyMethodAdapter((FBMethodVisitor) mv);
073: }
074: return mv;
075: }
076: }
077:
078: private static class MyMethodAdapter extends MethodAdapter {
079:
080: public MyMethodAdapter(FBMethodVisitor mv) {
081: super (mv);
082: }
083:
084: @Override
085: public void visitLabel(Label label) {
086: assert label instanceof MyLabel;
087: MyLabel l = (MyLabel) label;
088: ((FBMethodVisitor) mv).visitOffset(l.originalOffset);
089: if (l.realLabel) {
090: mv.visitLabel(label);
091: }
092: }
093: }
094:
095: private static class MyLabel extends Label {
096:
097: final int originalOffset;
098:
099: boolean realLabel;
100:
101: MyLabel(int originalOffset) {
102: this.originalOffset = originalOffset;
103: }
104: }
105: }
|