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

org.checkerframework.checker.nullness.KeyForTransfer 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.43.0
Show newest version
package org.checkerframework.checker.nullness;

import java.util.LinkedHashSet;
import java.util.Set;
import javax.lang.model.element.AnnotationMirror;
import org.checkerframework.checker.nullness.qual.KeyFor;
import org.checkerframework.dataflow.analysis.FlowExpressions;
import org.checkerframework.dataflow.analysis.FlowExpressions.Receiver;
import org.checkerframework.dataflow.analysis.TransferInput;
import org.checkerframework.dataflow.analysis.TransferResult;
import org.checkerframework.dataflow.cfg.node.MethodInvocationNode;
import org.checkerframework.dataflow.cfg.node.Node;
import org.checkerframework.framework.flow.CFAbstractTransfer;
import org.checkerframework.javacutil.AnnotationUtils;

/*
 * KeyForTransfer ensures that java.util.Map.put and containsKey
 * cause the appropriate @KeyFor annotation to be added to the key.
 */
public class KeyForTransfer extends CFAbstractTransfer {

    public KeyForTransfer(KeyForAnalysis analysis) {
        super(analysis);
    }

    /*
     * Provided that m is of a type that implements interface java.util.Map:
     * 
    *
  • Given a call m.containsKey(k), ensures that k is @KeyFor("m") in the thenStore of the transfer result. *
  • Given a call m.put(k, ...), ensures that k is @KeyFor("m") in the thenStore and elseStore of the transfer result. *
*/ @Override public TransferResult visitMethodInvocation( MethodInvocationNode node, TransferInput in) { TransferResult result = super.visitMethodInvocation(node, in); KeyForAnnotatedTypeFactory factory = (KeyForAnnotatedTypeFactory) analysis.getTypeFactory(); if (factory.isInvocationOfMapMethod(node, "containsKey") || factory.isInvocationOfMapMethod(node, "put")) { Node receiver = node.getTarget().getReceiver(); Receiver internalReceiver = FlowExpressions.internalReprOf(factory, receiver); String mapName = internalReceiver.toString(); Receiver keyReceiver = FlowExpressions.internalReprOf(factory, node.getArgument(0)); LinkedHashSet keyForMaps = new LinkedHashSet<>(); keyForMaps.add(mapName); final KeyForValue previousKeyValue = in.getValueOfSubNode(node.getArgument(0)); if (previousKeyValue != null) { for (AnnotationMirror prevAm : previousKeyValue.getAnnotations()) { if (prevAm != null && AnnotationUtils.areSameByClass(prevAm, KeyFor.class)) { keyForMaps.addAll(getKeys(prevAm)); } } } AnnotationMirror am = factory.createKeyForAnnotationMirrorWithValue(keyForMaps); if (factory.getMethodName(node).equals("containsKey")) { result.getThenStore().insertValue(keyReceiver, am); } else { // method name is "put" result.getThenStore().insertValue(keyReceiver, am); result.getElseStore().insertValue(keyReceiver, am); } } return result; } /** @return the String value of a KeyFor, this will throw an exception */ private Set getKeys(final AnnotationMirror keyFor) { if (keyFor.getElementValues().size() == 0) { return new LinkedHashSet<>(); } return new LinkedHashSet<>( AnnotationUtils.getElementValueArray(keyFor, "value", String.class, true)); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy