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

proguard.analysis.cpa.defaults.Cfa Maven / Gradle / Ivy

Go to download

ProGuardCORE is a free library to read, analyze, modify, and write Java class files.

There is a newer version: 9.1.7
Show newest version
/*
 * ProGuardCORE -- library to process Java bytecode.
 *
 * Copyright (c) 2002-2022 Guardsquare NV
 *
 * 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 proguard.analysis.cpa.defaults;

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import proguard.classfile.Signature;
import proguard.analysis.cpa.interfaces.CfaEdge;
import proguard.analysis.cpa.interfaces.CfaNode;

/**
 * A {@link Cfa} is a control flow automaton with nodes {@code } and edges {@code }. It can be used for different programming languages with functions identified by {@code
 * }.
 *
 * @author Carlo Alberto Pozzoli
 */
public abstract class Cfa, CfaEdgeT extends CfaEdge, SignatureT extends Signature>
{

    protected final           Map> functionNodes = new HashMap<>();
    protected final transient Set                           allNodes      = new HashSet<>();

    /**
     * Returns true if there are no nodes in the CFA, false otherwise.
     */
    public boolean isEmpty()
    {
        return functionNodes.isEmpty();
    }

    /**
     * Returns a collection of all the nodes present in the graph.
     */
    public Collection getAllNodes()
    {
        return allNodes;
    }

    /**
     * Returns a collection of the entry nodes (with offset 0) of all the functions present in the graph, returns an empty collection if the graph is empty.
     */
    public Collection getFunctionEntryNodes()
    {
        return functionNodes.keySet()
                            .stream()
                            .map(x -> functionNodes.get(x).getOrDefault(0, null))
                            .filter(x -> x != null)
                            .collect(Collectors.toSet());
    }

    /**
     * Returns the entry node of a specific function (with offset 0), returns null if the function or its entry node are not in the graph.
     *
     * @param signature The signature of the function.
     */
    public CfaNodeT getFunctionEntryNode(SignatureT signature)
    {
        return functionNodes.getOrDefault(signature, Collections.emptyMap()).getOrDefault(0, null);
    }

    /**
     * Returns all the nodes of a specific function, returns an empty collection if the function is not in the graph or if it has no nodes.
     *
     * @param signature The signature of the function.
     */
    public Collection getFunctionNodes(SignatureT signature)
    {
        return functionNodes.getOrDefault(signature, Collections.emptyMap()).values();
    }

    /**
     * Returns the node of a function at a specific code offset, returns null if the function or the specific node are not in the graph.
     *
     * @param signature The signature of the function.
     * @param offset    The offset of the code location represented by the node.
     */
    public CfaNodeT getFunctionNode(SignatureT signature, int offset)
    {
        return functionNodes.getOrDefault(signature, Collections.emptyMap()).getOrDefault(offset, null);
    }

    /**
     * Add an entry node to the graph for a specified function (with offset 0).
     *
     * @param signature The signature of the function,.
     * @param node      The entry node to add.
     */
    public void addFunctionEntryNode(SignatureT signature, CfaNodeT node)
    {
        addFunctionNode(signature, node, 0);
    }

    /**
     * Add a node to the graph for a specified function.
     *
     * @param signature The signature of the function.
     * @param node      The node to add.
     */
    public void addFunctionNode(SignatureT signature, CfaNodeT node, int offset)
    {
        functionNodes.computeIfAbsent(signature, x -> new HashMap()).put(offset, node);
        allNodes.add(node);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy