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

org.checkerframework.checker.signature.SignatureTransfer Maven / Gradle / Ivy

Go to download

The Checker Framework enhances Java's type system to make it more powerful and useful. This lets software developers detect and prevent errors in their Java programs. The Checker Framework includes compiler plug-ins ("checkers") that find bugs or verify their absence. It also permits you to write your own compiler plug-ins.

There is a newer version: 3.44.0
Show newest version
package org.checkerframework.checker.signature;

import javax.lang.model.element.ExecutableElement;
import org.checkerframework.checker.signature.qual.CanonicalNameOrEmpty;
import org.checkerframework.dataflow.analysis.ConditionalTransferResult;
import org.checkerframework.dataflow.analysis.TransferInput;
import org.checkerframework.dataflow.analysis.TransferResult;
import org.checkerframework.dataflow.cfg.node.MethodAccessNode;
import org.checkerframework.dataflow.cfg.node.MethodInvocationNode;
import org.checkerframework.dataflow.cfg.node.Node;
import org.checkerframework.dataflow.expression.JavaExpression;
import org.checkerframework.framework.flow.CFAnalysis;
import org.checkerframework.framework.flow.CFStore;
import org.checkerframework.framework.flow.CFTransfer;
import org.checkerframework.framework.flow.CFValue;
import org.checkerframework.framework.type.AnnotatedTypeMirror;
import org.checkerframework.javacutil.ElementUtils;
import org.checkerframework.javacutil.TypesUtils;

/** The transfer function for the Signature Checker. */
public class SignatureTransfer extends CFTransfer {

  /** The annotated type factory for this transfer function. */
  private SignatureAnnotatedTypeFactory aTypeFactory;

  /**
   * Create a new SignatureTransfer.
   *
   * @param analysis the analysis
   */
  public SignatureTransfer(CFAnalysis analysis) {
    super(analysis);
    aTypeFactory = (SignatureAnnotatedTypeFactory) analysis.getTypeFactory();
  }

  @Override
  public TransferResult visitMethodInvocation(
      MethodInvocationNode n, TransferInput in) {
    TransferResult superResult = super.visitMethodInvocation(n, in);

    MethodAccessNode target = n.getTarget();
    ExecutableElement method = target.getMethod();
    Node receiver = target.getReceiver();
    if (TypesUtils.isString(receiver.getType()) && ElementUtils.matchesElement(method, "isEmpty")) {

      AnnotatedTypeMirror receiverAtm = aTypeFactory.getAnnotatedType(receiver.getTree());
      if (receiverAtm.hasAnnotation(CanonicalNameOrEmpty.class)) {

        CFStore thenStore = superResult.getRegularStore();
        CFStore elseStore = thenStore.copy();
        ConditionalTransferResult result =
            new ConditionalTransferResult<>(superResult.getResultValue(), thenStore, elseStore);
        // The refined expression is the receiver of the method call.
        JavaExpression refinedExpr = JavaExpression.fromNode(receiver);

        elseStore.insertValue(refinedExpr, aTypeFactory.CANONICAL_NAME);
        return result;
      }
    }
    return superResult;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy