All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.siyeh.ig.psiutils.CloneUtils Maven / Gradle / Ivy

Go to download

A packaging of the IntelliJ Community Edition java-analysis-impl library. This is release number 1 of trunk branch 142.

The newest version!
/*
 * Copyright 2003-2015 Dave Griffith, Bas Leijdekkers
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.siyeh.ig.psiutils;

import com.intellij.psi.*;
import com.intellij.psi.util.InheritanceUtil;
import com.intellij.psi.util.PsiUtil;
import com.siyeh.HardcodedMethodConstants;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class CloneUtils {

  private CloneUtils() {}

  public static boolean isCloneable(@Nullable PsiClass aClass) {
    return InheritanceUtil.isInheritor(aClass, CommonClassNames.JAVA_LANG_CLONEABLE);
  }

  public static boolean isDirectlyCloneable(@NotNull PsiClass aClass) {
    final PsiClass[] interfaces = aClass.getInterfaces();
    for (PsiClass anInterface : interfaces) {
      if (anInterface == null) {
        continue;
      }
      final String qualifiedName = anInterface.getQualifiedName();
      if (CommonClassNames.JAVA_LANG_CLONEABLE.equals(qualifiedName)) {
        return true;
      }
    }
    return false;
  }

  public static boolean isClone(@Nullable PsiMethod method) {
    if (method == null) {
      return false;
    }
    final PsiClassType javaLangObject;
    if (!PsiUtil.isLanguageLevel5OrHigher(method)) {
      javaLangObject = TypeUtils.getObjectType(method);
    }
    else {
      // for 1.5 and after, clone may be covariant
      javaLangObject = null;
    }
    return MethodUtils.methodMatches(method, null, javaLangObject,
                                     HardcodedMethodConstants.CLONE, PsiType.EMPTY_ARRAY);
  }

  public static boolean onlyThrowsException(@NotNull PsiMethod method) {
    if (!method.hasModifierProperty(PsiModifier.FINAL)) {
      final PsiClass aClass = method.getContainingClass();
      if (aClass == null || !aClass.hasModifierProperty(PsiModifier.FINAL)) {
        return false;
      }
    }
    final PsiCodeBlock body = method.getBody();
    if (body == null) {
      return false;
    }
    final PsiStatement[] statements = body.getStatements();
    if (statements.length == 0) {
      return false;
    }
    final PsiStatement statement = statements[statements.length - 1];
    return statement instanceof PsiThrowStatement;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy