001 /*
002 * Sonar, open source software quality management tool.
003 * Copyright (C) 2008-2012 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * Sonar is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 3 of the License, or (at your option) any later version.
010 *
011 * Sonar is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014 * Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public
017 * License along with Sonar; if not, write to the Free Software
018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
019 */
020 package org.sonar.java.bytecode.asm;
021
022 import org.objectweb.asm.FieldVisitor;
023 import org.objectweb.asm.MethodVisitor;
024 import org.objectweb.asm.commons.EmptyVisitor;
025 import org.sonar.java.bytecode.asm.AsmClassProvider.DETAIL_LEVEL;
026
027 public class AsmClassVisitor extends EmptyVisitor {
028
029 private AsmClassProvider asmClassProvider;
030 private DETAIL_LEVEL level;
031 private AsmClass asmClass;
032
033 public AsmClassVisitor(AsmClassProvider asmClassProvider, AsmClass asmClass, DETAIL_LEVEL level) {
034 this.asmClassProvider = asmClassProvider;
035 this.level = level;
036 this.asmClass = asmClass;
037 }
038
039 public void visit(int version, int accessFlags, String internalName, String signature, String superClass, String[] interfaces) {
040 if (asmClass.getDetailLevel() == DETAIL_LEVEL.NOTHING) {
041 asmClass.setAccessFlags(accessFlags);
042 if (asmClass.isInterface()) {
043 if (interfaces.length == 1) {
044 asmClass.setSuperClass(asmClassProvider.getClass(interfaces[0], DETAIL_LEVEL.STRUCTURE));
045 }
046 } else {
047 if (superClass != null) {
048 asmClass.setSuperClass(asmClassProvider.getClass(superClass, DETAIL_LEVEL.STRUCTURE));
049 }
050 for (String interfaceName : interfaces) {
051 asmClass.addInterface(asmClassProvider.getClass(interfaceName, DETAIL_LEVEL.STRUCTURE));
052 }
053 }
054 if (signature != null) {
055 String[] internalNames = AsmSignature.extractInternalNames(signature);
056 AsmClass[] asmClasses = internalNamesToAsmClasses(internalNames, DETAIL_LEVEL.NOTHING);
057 asmClass.addUsesOfClasses(asmClasses);
058 }
059 }
060 }
061
062 public FieldVisitor visitField(int access, String fieldName, String description, String signature, Object value) {
063 AsmField field = asmClass.getFieldOrCreateIt(fieldName);
064 field.setAccessFlags(access);
065 String[] internalNames = AsmSignature.extractInternalNames(description, signature);
066 AsmClass[] asmClasses = internalNamesToAsmClasses(internalNames, DETAIL_LEVEL.NOTHING);
067 field.addUsesOfClasses(asmClasses);
068 return null;
069 }
070
071 public MethodVisitor visitMethod(int access, String methodName, String description, String signature, String[] exceptions) {
072 AsmMethod method = asmClass.getMethodOrCreateIt(methodName + description);
073 if (isInheritedMethodSignature(method.getParent(), method.getKey())) {
074 method.setInherited(true);
075 }
076 method.setSignature(signature);
077 method.setBodyLoaded(true);
078 method.setAccessFlags(access);
079 String[] internalNames = AsmSignature.extractInternalNames(description, signature);
080 AsmClass[] asmClasses = internalNamesToAsmClasses(internalNames, DETAIL_LEVEL.NOTHING);
081 method.addUsesOfClasses(asmClasses);
082 AsmClass[] asmExceptionClasses = internalNamesToAsmClasses(exceptions, DETAIL_LEVEL.NOTHING);
083 method.addUsesOfClasses(asmExceptionClasses);
084 if (level == DETAIL_LEVEL.STRUCTURE_AND_CALLS) {
085 return new AsmMethodVisitor(method, asmClassProvider);
086 }
087 return null;
088 }
089
090 public void visitEnd() {
091 asmClass.setDetailLevel(level);
092 }
093
094 private AsmClass[] internalNamesToAsmClasses(String[] internalNames, DETAIL_LEVEL level) {
095 if (internalNames == null) {
096 return new AsmClass[0];
097 }
098 AsmClass[] asmClasses = new AsmClass[internalNames.length];
099 for (int i = 0; i < internalNames.length; i++) {
100 asmClasses[i] = asmClassProvider.getClass(internalNames[i], level);
101 }
102 return asmClasses;
103 }
104
105 private boolean isInheritedMethodSignature(AsmClass parent, String key) {
106 if (parent.getSuperClass() != null
107 && (parent.getSuperClass().getMethod(key) != null || isInheritedMethodSignature(parent.getSuperClass(), key))) {
108 return true;
109 }
110 for (AsmClass interfaceClass : parent.getInterfaces()) {
111 if (interfaceClass.getMethod(key) != null || isInheritedMethodSignature(interfaceClass, key)) {
112 return true;
113 }
114 }
115 return false;
116 }
117 }