001: /***
002: * ASM Guide
003: * Copyright (c) 2007 Eric Bruneton
004: * All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: * 1. Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: * 2. Redistributions in binary form must reproduce the above copyright
012: * notice, this list of conditions and the following disclaimer in the
013: * documentation and/or other materials provided with the distribution.
014: * 3. Neither the name of the copyright holders nor the names of its
015: * contributors may be used to endorse or promote products derived from
016: * this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
022: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
023: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
024: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
025: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
026: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
027: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
028: * THE POSSIBILITY OF SUCH DAMAGE.
029: */package ch7.sec2;
030:
031: import static org.objectweb.asm.Opcodes.*;
032:
033: import java.util.Iterator;
034:
035: import org.objectweb.asm.ClassVisitor;
036: import org.objectweb.asm.tree.ClassNode;
037: import org.objectweb.asm.tree.FieldInsnNode;
038: import org.objectweb.asm.tree.FrameNode;
039: import org.objectweb.asm.tree.InsnList;
040: import org.objectweb.asm.tree.InsnNode;
041: import org.objectweb.asm.tree.JumpInsnNode;
042: import org.objectweb.asm.tree.LabelNode;
043: import org.objectweb.asm.tree.MethodInsnNode;
044: import org.objectweb.asm.tree.MethodNode;
045: import org.objectweb.asm.tree.TypeInsnNode;
046: import org.objectweb.asm.tree.VarInsnNode;
047: import org.objectweb.asm.tree.analysis.AnalyzerException;
048:
049: import util.AbstractTestCase;
050:
051: /**
052: * ASM Guide example test class.
053: *
054: * @author Eric Bruneton
055: */
056: public class CyclomaticComplexityTest extends AbstractTestCase {
057:
058: public void test() throws AnalyzerException {
059: MethodNode mn = new MethodNode(ACC_PUBLIC, "checkAndSetF",
060: "(I)V", null, null);
061: InsnList il = mn.instructions;
062: il.add(new VarInsnNode(ILOAD, 1));
063: LabelNode label = new LabelNode();
064: il.add(new JumpInsnNode(IFLT, label));
065: il.add(new VarInsnNode(ALOAD, 0));
066: il.add(new VarInsnNode(ILOAD, 1));
067: il.add(new FieldInsnNode(PUTFIELD, "pkg/Bean", "f", "I"));
068: LabelNode end = new LabelNode();
069: il.add(new JumpInsnNode(GOTO, end));
070: il.add(label);
071: il.add(new FrameNode(F_SAME, 0, null, 0, null));
072: il.add(new TypeInsnNode(NEW,
073: "java/lang/IllegalArgumentException"));
074: il.add(new InsnNode(DUP));
075: il.add(new MethodInsnNode(INVOKESPECIAL,
076: "java/lang/IllegalArgumentException", "<init>", "()V"));
077: il.add(new InsnNode(ATHROW));
078: il.add(end);
079: il.add(new FrameNode(F_SAME, 0, null, 0, null));
080: il.add(new InsnNode(RETURN));
081: mn.maxStack = 2;
082: mn.maxLocals = 2;
083: CyclomaticComplexity cc = new CyclomaticComplexity();
084: assert (cc.getCyclomaticComplexity("pkg/Bean", mn) == 1);
085: }
086:
087: @Override
088: protected ClassVisitor getClassAdapter(final ClassVisitor cv) {
089: return new ClassNode() {
090: public void visitEnd() {
091: Iterator i = methods.iterator();
092: while (i.hasNext()) {
093: MethodNode mn = (MethodNode) i.next();
094: try {
095: CyclomaticComplexity cc = new CyclomaticComplexity();
096: assert (cc.getCyclomaticComplexity(name, mn) > 0);
097: } catch (AnalyzerException ignored) {
098: }
099: }
100: accept(cv);
101: }
102: };
103: }
104: }
|