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.ArrayList;
023    import java.util.List;
024    
025    import org.sonar.squid.api.SourceCodeEdgeUsage;
026    
027    public class AsmMethod extends AsmResource {
028    
029      private String name;
030      private String key;
031      private boolean inherited = false;
032      private boolean empty = false;
033      private boolean bodyLoaded = true;
034      private boolean accessor = false;
035    
036      public AsmMethod(AsmClass parent, String name, String descriptor) {
037        this.parent = parent;
038        this.name = name;
039        key = name + descriptor;
040      }
041    
042      public AsmMethod(AsmClass parent, String key) {
043        this.parent = parent;
044        this.key = key;
045        this.name = key.substring(0, key.indexOf('('));
046      }
047    
048      public String getName() {
049        return name;
050      }
051    
052      public String getKey() {
053        return key;
054      }
055    
056      public List<AsmField> getCallsToField() {
057        List<AsmField> callsToField = new ArrayList<AsmField>();
058        for (AsmEdge usage : getOutgoingEdges()) {
059          if (usage.getUsage() == SourceCodeEdgeUsage.CALLS_FIELD) {
060            callsToField.add((AsmField) usage.getTo());
061          }
062        }
063        return callsToField;
064      }
065    
066      public List<AsmMethod> getCallsToMethod() {
067        List<AsmMethod> callsToMethod = new ArrayList<AsmMethod>();
068        for (AsmEdge usage : getOutgoingEdges()) {
069          if (usage.getUsage() == SourceCodeEdgeUsage.CALLS_METHOD) {
070            callsToMethod.add((AsmMethod) usage.getTo());
071          }
072        }
073        return callsToMethod;
074      }
075    
076      @Override
077      public boolean equals(Object object) {
078        if (this == object) {
079          return true;
080        }
081        if (object instanceof AsmMethod) {
082          AsmMethod otherMethod = (AsmMethod) object;
083          return parent.equals(otherMethod.parent) && key.equals(otherMethod.key);
084        }
085        return false;
086      }
087    
088      @Override
089      public int hashCode() {
090        return parent.hashCode() + key.hashCode();
091      }
092    
093      public boolean isConstructor() {
094        return name.equals("<init>") || name.equals("<clinit>");
095      }
096    
097      void setInherited(boolean inherited) {
098        this.inherited = inherited;
099      }
100    
101      public boolean isInherited() {
102        return inherited;
103      }
104    
105      public boolean isEmpty() {
106        return empty;
107      }
108    
109      public boolean isBodyLoaded() {
110        return bodyLoaded;
111      }
112    
113      void setBodyLoaded(boolean bodyLoaded) {
114        this.bodyLoaded = bodyLoaded;
115      }
116    
117      void setEmpty(boolean empty) {
118        this.empty = empty;
119      }
120    
121      public boolean isAccessor() {
122        return accessor;
123      }
124    
125      public void setAccessor(boolean accessor) {
126        this.accessor = accessor;
127      }
128    
129      @Override
130      public String toString() {
131        return key;
132      }
133    }