001/*
002 * Sonar, open source software quality management tool.
003 * Copyright (C) 2008-2012 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 */
020package org.sonar.java.signature;
021
022public class Parameter {
023
024  private final JvmJavaType jvmJavaType;
025  private final String className;
026  private final boolean isArray;
027
028  public Parameter(JvmJavaType jvmJavaType, boolean isArray) {
029    this(jvmJavaType, null, isArray);
030  }
031
032  public Parameter(String classCanonicalName, boolean isArray) {
033    this(JvmJavaType.L, classCanonicalName, isArray);
034  }
035
036  Parameter(JvmJavaType jvmJavaType, String classCanonicalName, boolean isArray) {
037    if (jvmJavaType == JvmJavaType.L && (classCanonicalName == null || "".equals(classCanonicalName))) {
038      throw new IllegalStateException("With an Object JavaType, this is mandatory to specify the canonical name of the class.");
039    }
040    this.jvmJavaType = jvmJavaType;
041    this.className = extractClassName(classCanonicalName);
042    this.isArray = isArray;
043  }
044
045  public Parameter(Parameter parameter) {
046    this(parameter.jvmJavaType, parameter.className, parameter.isArray);
047  }
048
049  public boolean isVoid() {
050    return jvmJavaType == JvmJavaType.V;
051  }
052
053  public JvmJavaType getJvmJavaType() {
054    return jvmJavaType;
055  }
056
057  public String getClassName() {
058    return className;
059  }
060
061  public boolean isArray() {
062    return isArray;
063  }
064
065  public boolean isOject() {
066    return jvmJavaType == JvmJavaType.L;
067  }
068
069  private String extractClassName(String classCanonicalName) {
070    if (classCanonicalName == null) {
071      return null;
072    }
073    int slashIndex = classCanonicalName.lastIndexOf('/');
074    int dollarIndex = classCanonicalName.lastIndexOf('$');
075    if (slashIndex != -1 || dollarIndex != -1) {
076      return classCanonicalName.substring(Math.max(slashIndex, dollarIndex) + 1);
077    }
078    return classCanonicalName;
079  }
080}