001: /*
002: * Copyright (c) 2001-2007, Jean Tessier
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions
007: * are met:
008: *
009: * * Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: *
012: * * Redistributions in binary form must reproduce the above copyright
013: * notice, this list of conditions and the following disclaimer in the
014: * documentation and/or other materials provided with the distribution.
015: *
016: * * Neither the name of Jean Tessier nor the names of his contributors
017: * may be used to endorse or promote products derived from this software
018: * without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
021: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
022: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
023: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
024: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
027: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
028: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
029: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
030: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031: */
032:
033: package com.jeantessier.classreader;
034:
035: import org.apache.oro.text.perl.*;
036:
037: public class FeatureDependencyCollector extends CollectorBase {
038: private static final Perl5Util perl = new Perl5Util();
039:
040: private Class_info this Class;
041:
042: public void visitClassfile(Classfile classfile) {
043: this Class = classfile.getRawClass();
044:
045: classfile.getConstantPool().accept(this );
046: }
047:
048: public void visitFieldRef_info(FieldRef_info entry) {
049: if (entry.getRawClass() != this Class) {
050: add(entry.getClassName() + "."
051: + entry.getRawNameAndType().getName());
052: }
053: }
054:
055: public void visitMethodRef_info(MethodRef_info entry) {
056: if ((entry.getRawClass() != this Class)
057: && !perl.match("/<.*init>/", entry.getRawNameAndType()
058: .getName())) {
059: add(entry.getClassName() + "."
060: + entry.getRawNameAndType().getName());
061: }
062: }
063:
064: public void visitInterfaceMethodRef_info(
065: InterfaceMethodRef_info entry) {
066: if (entry.getRawClass() != this Class) {
067: add(entry.getClassName() + "."
068: + entry.getRawNameAndType().getName());
069: }
070: }
071:
072: public void visitMethod_info(Method_info entry) {
073: processSignature(entry.getDescriptor());
074:
075: super .visitMethod_info(entry);
076: }
077:
078: public void visitInstruction(Instruction helper) {
079: switch (helper.getOpcode()) {
080: case 0xb2: // getstatic
081: case 0xb3: // putstatic
082: case 0xb4: // getfield
083: case 0xb5: // putfield
084: case 0xb6: // invokevirtual
085: case 0xb7: // invokespecial
086: case 0xb8: // invokestatic
087: case 0xb9: // invokeinterface
088: helper.getIndexedConstantPoolEntry().accept(this );
089: break;
090: default:
091: // Do nothing
092: break;
093: }
094:
095: super .visitInstruction(helper);
096: }
097:
098: private void processSignature(String str) {
099: int currentPos = 0;
100: int startPos;
101: int endPos;
102:
103: while ((startPos = str.indexOf('L', currentPos)) != -1) {
104: if ((endPos = str.indexOf(';', startPos)) != -1) {
105: String candidate = str.substring(startPos + 1, endPos);
106: if (!this Class.getName().equals(candidate)) {
107: add(SignatureHelper.path2ClassName(candidate));
108: }
109: currentPos = endPos + 1;
110: } else {
111: currentPos = startPos + 1;
112: }
113: }
114: }
115: }
|