001    /*
002     * Sonar, open source software quality management tool.
003     * Copyright (C) 2008-2011 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.api;
021    
022    import org.apache.commons.lang.StringUtils;
023    import org.sonar.api.resources.*;
024    
025    /**
026     * @since 2.6
027     */
028    public final class JavaMethod extends Method {
029    
030      public static final String QUALIFIER = Qualifiers.METHOD;
031    
032      public static final int UNKNOWN_LINE = -1;
033      private static final String CLASS_SEPARATOR = "#";
034    
035      private String signature;
036      private String className;
037      private int fromLine;
038      private int toLine;
039      private boolean isAccessor = false;
040    
041      private JavaMethod(String className, String signature) {
042        super(toKey(className, signature), QUALIFIER, Java.INSTANCE);
043        this.className = className;
044        this.signature = signature;
045      }
046    
047      private JavaMethod(String className, String signature, int fromLine, int toLine, boolean isAccessor) {
048        this(className, signature);
049        this.fromLine = fromLine;
050        this.toLine = toLine;
051        this.isAccessor = isAccessor;
052      }
053    
054      public int getFromLine() {
055        return fromLine;
056      }
057    
058      public int getToLine() {
059        return toLine;
060      }
061    
062      public String getSignature() {
063        return signature;
064      }
065    
066      public String getClassName() {
067        return className;
068      }
069    
070      public boolean isAccessor() {
071        return isAccessor;
072      }
073    
074      @Override
075      public String getName() {
076        return signature;
077      }
078    
079      @Override
080      public String getLongName() {
081        return getKey();
082      }
083    
084      @Override
085      public String getDescription() {
086        return null;
087      }
088    
089      @Override
090      public Resource getParent() {
091        return null;
092      }
093    
094      @Override
095      public String toString() {
096        return getKey();
097      }
098    
099      public static JavaMethod createRef(String key) {
100        String[] parts = splitClassAndMethodFromKey(key);
101        return new JavaMethod(parts[0], parts[1]);
102      }
103    
104      private static String[] splitClassAndMethodFromKey(String key) {
105        String[] parts = StringUtils.split(key, CLASS_SEPARATOR);
106        if (parts.length!=2) {
107          throw new IllegalArgumentException("Java method does not respect the format: org.foo.Bar#methodName(LString;)V. Got: " + key);
108        }
109        return parts;
110      }
111    
112      public static JavaMethod createRef(JavaClass javaClass, String signature) {
113        return new JavaMethod(javaClass.getName(), signature);
114      }
115    
116      static String toKey(JavaClass javaClass, String signature) {
117        return toKey(javaClass.getName(), signature);
118      }
119    
120      static String toKey(String className, String signature) {
121        return new StringBuilder().append(className).append(CLASS_SEPARATOR).append(signature).toString();
122      }
123    
124      public static class Builder {
125        private String className;
126        private String signature;
127        private int fromLine = UNKNOWN_LINE;
128        private int toLine = UNKNOWN_LINE;
129        private boolean isAccessor = false;
130    
131        public Builder setKey(String key) {
132          String[] parts = splitClassAndMethodFromKey(key);
133          this.className = parts[0];
134          this.signature = parts[1];
135          return this;
136        }
137    
138        public Builder setClass(String className) {
139          this.className = className;
140          return this;
141        }
142    
143        public Builder setClass(JavaClass javaClass) {
144          this.className = javaClass.getName();
145          return this;
146        }
147    
148        public Builder setSignature(String signature) {
149          this.signature = signature;
150          return this;
151        }
152    
153        public Builder setFromLine(int fromLine) {
154          this.fromLine = Math.max(UNKNOWN_LINE, fromLine);
155          return this;
156        }
157    
158        public Builder setToLine(int toLine) {
159          this.toLine = Math.max(UNKNOWN_LINE, toLine);
160          return this;
161        }
162    
163        public Builder setAccessor(boolean accessor) {
164          isAccessor = accessor;
165          return this;
166        }
167    
168        public JavaMethod create() {
169          return new JavaMethod(className, signature, fromLine, toLine, isAccessor);
170        }
171      }
172    }