001    /*
002     * Sonar, open source software quality management tool.
003     * Copyright (C) 2009 SonarSource SA
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.squid.bytecode.asm;
021    
022    import org.objectweb.asm.Type;
023    import org.objectweb.asm.signature.SignatureReader;
024    
025    public class AsmMethod extends AsmUnit {
026    
027      String    name;
028      String    key;
029      String[]  exceptions;
030      AsmType   returnType;
031      AsmType[] arguments;
032      String[]  signatureClasses;
033    
034      public AsmMethod(int access, String name, String descriptor, String descriptorWithGenerics, String[] exceptions) {
035        super(access);
036        this.name = name;
037        key = name + descriptor;
038        this.exceptions = exceptions;
039        initReturnType(descriptor);
040        initArguments(descriptor);
041        if (descriptorWithGenerics == null) {
042          extractClasses(descriptor);
043        } else {
044          extractClasses(descriptorWithGenerics);
045        }
046      }
047    
048      private void initArguments(String descriptor) {
049        Type[] types = Type.getArgumentTypes(descriptor);
050        arguments = new AsmType[types.length];
051        for (int i = 0; i < arguments.length; i++) {
052          arguments[i] = new AsmType(types[i]);
053        }
054      }
055    
056      private void extractClasses(String signature) {
057        AsmSignatureVisitor signatureVisitor = new AsmSignatureVisitor();
058        new SignatureReader(signature).accept(signatureVisitor);
059        signatureClasses = signatureVisitor.getInternalNames().toArray(new String[0]);
060      }
061    
062      private void initReturnType(String descriptor) {
063        returnType = new AsmType(Type.getReturnType(descriptor));
064      }
065    
066      public String getName() {
067        return name;
068      }
069    
070      public String getKey() {
071        return key;
072      }
073    
074      public boolean hasReturnType() {
075        return !returnType.isVoid();
076      }
077    
078      public AsmType getReturnType() {
079        return returnType;
080      }
081    
082      public boolean hasArguments() {
083        return arguments.length != 0;
084      }
085    
086      public AsmType[] getArguments() {
087        return arguments;
088      }
089    
090      public String[] getExceptions() {
091        return exceptions;
092      }
093      
094      public String[] getSignatureClasses(){
095        return signatureClasses;
096      }
097    
098      public String getReturnTypePath() {
099        return returnType.getPath();
100      }
101    }