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

com.siyeh.ig.inheritance.RefusedBequestInspectionBase 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.inheritance;

import com.intellij.codeInsight.AnnotationUtil;
import com.intellij.openapi.util.InvalidDataException;
import com.intellij.openapi.util.WriteExternalException;
import com.intellij.psi.*;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.BaseInspection;
import com.siyeh.ig.BaseInspectionVisitor;
import com.siyeh.ig.psiutils.ClassUtils;
import com.siyeh.ig.psiutils.MethodUtils;
import com.siyeh.ig.ui.ExternalizableStringSet;
import org.jdom.Element;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class RefusedBequestInspectionBase extends BaseInspection {

  @SuppressWarnings("PublicField") public boolean ignoreEmptySuperMethods = false;

  @SuppressWarnings("PublicField") final ExternalizableStringSet annotations =
    new ExternalizableStringSet("javax.annotation.OverridingMethodsMustInvokeSuper");

  @SuppressWarnings("PublicField") boolean onlyReportWhenAnnotated = false;

  @Override
  public void writeSettings(@NotNull Element node) throws WriteExternalException {
    super.writeSettings(node);
    if (onlyReportWhenAnnotated) {
      node.addContent(new Element("option").setAttribute("name", "onlyReportWhenAnnotated").
        setAttribute("value", String.valueOf(onlyReportWhenAnnotated)));
    }
    if (!annotations.hasDefaultValues()) {
      final Element element = new Element("option").setAttribute("name", "annotations");
      final Element valueElement = new Element("value");
      annotations.writeExternal(valueElement);
      node.addContent(element.addContent(valueElement));
    }
  }

  @Override
  public void readSettings(@NotNull Element node) throws InvalidDataException {
    super.readSettings(node);
    for (Element option : node.getChildren("option")) {
      if ("onlyReportWhenAnnotated".equals(option.getAttributeValue("name"))) {
        onlyReportWhenAnnotated = Boolean.parseBoolean(option.getAttributeValue("value"));
      }
      else if ("annotations".equals(option.getAttributeValue("name"))) {
        final Element value = option.getChild("value");
        if (value != null) {
          annotations.readExternal(value);
        }
      }
    }
  }

  @Override
  @NotNull
  public String getDisplayName() {
    return InspectionGadgetsBundle.message("refused.bequest.display.name");
  }

  @Override
  @NotNull
  public String buildErrorString(Object... infos) {
    return InspectionGadgetsBundle.message("refused.bequest.problem.descriptor");
  }

  @Override
  public BaseInspectionVisitor buildVisitor() {
    return new RefusedBequestVisitor();
  }

  private class RefusedBequestVisitor extends BaseInspectionVisitor {

    @Override
    public void visitMethod(@NotNull PsiMethod method) {
      super.visitMethod(method);
      final PsiCodeBlock body = method.getBody();
      if (body == null) {
        return;
      }
      if (method.getNameIdentifier() == null) {
        return;
      }
      final PsiMethod leastConcreteSuperMethod = getLeastConcreteSuperMethod(method);
      if (leastConcreteSuperMethod == null) {
        return;
      }
      final PsiClass objectClass = ClassUtils.findObjectClass(method);
      final PsiMethod[] superMethods = method.findSuperMethods(objectClass);
      if (superMethods.length > 0) {
        return;
      }
      if (ignoreEmptySuperMethods) {
        final PsiMethod superMethod = (PsiMethod)leastConcreteSuperMethod.getNavigationElement();
        if (MethodUtils.isTrivial(superMethod, true)) {
          return;
        }
      }
      if (onlyReportWhenAnnotated) {
        if (!AnnotationUtil.isAnnotated(leastConcreteSuperMethod, annotations)) {
          return;
        }
      }
      if (containsSuperCall(body, leastConcreteSuperMethod)) {
        return;
      }
      registerMethodError(method);
    }

    @Nullable
    private PsiMethod getLeastConcreteSuperMethod(PsiMethod method) {
      final PsiMethod[] superMethods = method.findSuperMethods(true);
      for (final PsiMethod superMethod : superMethods) {
        final PsiClass containingClass = superMethod.getContainingClass();
        if (containingClass != null && !superMethod.hasModifierProperty(PsiModifier.ABSTRACT) && !containingClass.isInterface()) {
          return superMethod;
        }
      }
      return null;
    }

    private boolean containsSuperCall(@NotNull PsiElement context, @NotNull PsiMethod method) {
      final SuperCallVisitor visitor = new SuperCallVisitor(method);
      context.accept(visitor);
      return visitor.hasSuperCall();
    }
  }

  private static class SuperCallVisitor extends JavaRecursiveElementVisitor {

    private final PsiMethod methodToSearchFor;
    private boolean hasSuperCall = false;

    SuperCallVisitor(PsiMethod methodToSearchFor) {
      this.methodToSearchFor = methodToSearchFor;
    }

    @Override
    public void visitElement(@NotNull PsiElement element) {
      if (hasSuperCall) {
        return;
      }
      super.visitElement(element);
    }

    @Override
    public void visitMethodCallExpression(
      @NotNull PsiMethodCallExpression expression) {
      if (hasSuperCall) {
        return;
      }
      super.visitMethodCallExpression(expression);
      final PsiReferenceExpression methodExpression = expression.getMethodExpression();
      final PsiExpression qualifier = methodExpression.getQualifierExpression();
      if (qualifier == null) {
        return;
      }
      final String text = qualifier.getText();
      if (!PsiKeyword.SUPER.equals(text)) {
        return;
      }
      final PsiMethod method = expression.resolveMethod();
      if (method == null) {
        return;
      }
      if (method.equals(methodToSearchFor)) {
        hasSuperCall = true;
      }
    }

    public boolean hasSuperCall() {
      return hasSuperCall;
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy