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