/* BSD 2-Clause License - see OPAL/LICENSE for details. */ package org.opalj package tac package fpcf package analyses package cg package reflection import scala.util.boundary import scala.util.boundary.break import org.opalj.br.ClassType import org.opalj.br.fpcf.properties.Context import org.opalj.fpcf.Entity import org.opalj.fpcf.PropertyStore object StringUtil { /** * Returns Strings that a given expression may evaluate to. * Identifies local use of String constants. */ def getPossibleStrings[ContextType <: Context]( value: Expr[V], stmts: Array[Stmt[V]] ): Option[Set[String]] = boundary { Some(value.asVar.definedBy.map[Set[String]] { index => if (index >= 0) { getString(index, stmts) match { case Some(v) => Set(v) case _ => break(None); } } else { break(None); } }.flatten) } /** * Returns Strings that a given expression may evaluate to. * Identifies String constants. * Clients MUST handle dependencies where the depender is the given one and the dependee * provides allocation sites of Strings. */ def getPossibleStrings[ContextType <: Context]( value: V, context: ContextType, depender: Entity, stmts: Array[Stmt[V]], failure: () => Unit )( implicit typeIterator: TypeIterator, state: TypeIteratorState, ps: PropertyStore ): Set[String] = { var strings = Set.empty[String] AllocationsUtil.handleAllocations( value, context, depender, stmts, _ eq ClassType.String, failure ) { (_, defSite, _stmts) => getString(defSite, _stmts) match { case Some(v) => strings += v case _ => failure() } } strings } def getString(stringDefSite: Int, stmts: Array[Stmt[V]]): Option[String] = { val expr = stmts(stringDefSite).asAssignment.expr expr match { case StringConst(_, v) => Some(v) case _ => None } } }