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