Search in sources :

Example 41 with Stack

use of java.util.Stack in project jop by jop-devel.

the class Segment method synchronizedSegment.

/**
	 * Create an interprocedural segment for a synchronized block. Currently we do
	 * not split basic blocks here, so either you are happy with basic block granularity,
	 * or you split the basic block while loading.
	 * @param targetBlock The block containing the monitorenter instruction
	 * @param monitorEnter The monitor enter instruction
	 * @param callString   The context for the method
	 * @param cfgProvider A control flow graph provider
	 * @param callStringLength Length of the callstrings
	 * @param infeasibles Information about infeasible edges (null if no information available)
	 *
	 * @return a segment representing executions of the synchronized block
	 */
public static Segment synchronizedSegment(ContextCFG ccfg, CFGNode entryNode, InstructionHandle monitorEnter, CFGProvider cfgProvider, int callStringLength, InfeasibleEdgeProvider infeasibles) {
    if (infeasibles == null) {
        infeasibles = InfeasibleEdgeProvider.NO_INFEASIBLES;
    }
    ControlFlowGraph cfg = ccfg.getCfg();
    SuperGraph superGraph = new SuperGraph(cfgProvider, cfg, ccfg.getCallString(), callStringLength, infeasibles);
    ContextCFG rootMethod = superGraph.getRootNode();
    /* lift entry edges */
    Set<SuperGraphEdge> entryEdges = Iterators.addAll(new HashSet<SuperGraphEdge>(), superGraph.liftCFGEdges(rootMethod, cfg.incomingEdgesOf(entryNode)));
    /* find exit blocks (might also be in the same block) */
    /* monitorenter followed bei monitorexit in same block => segment only contains this block */
    Set<CFGEdge> monitorExitEdges = new HashSet<CFGEdge>();
    CFGNode currentNode = entryNode;
    int currentNestingLevel = 1;
    Iterator<InstructionHandle> insIter = currentNode.getBasicBlock().getInstructions().iterator();
    while (insIter.hasNext()) {
        if (insIter.next() == monitorEnter)
            break;
    }
    Stack<Pair<CFGNode, Integer>> todo = new Stack<Pair<CFGNode, Integer>>();
    Set<CFGNode> visited = new HashSet<CFGNode>();
    do {
        boolean isExit = false;
        while (insIter.hasNext()) {
            InstructionHandle ih = insIter.next();
            if (ih.getInstruction() instanceof MONITOREXIT) {
                /* blocks outgoing edges terminate segment */
                currentNestingLevel--;
                if (currentNestingLevel == 0) {
                    isExit = true;
                    // If monitorexit is not implemented in Java, the outgoing edges of the
                    // basic block that contains monitorexit end the synchronized block.
                    // In order to avoid imprecision, it is advisable that monitorexit is the
                    // last statement in the basic block.
                    // We also handle the case when monitorexit is implemented in Java. In this case,
                    // currentNode will be a SpecialInvokeNode, urrentNode's only
                    // successor will be the corresponding ReturnNode, and the outgoing edges of
                    // the ReturnNode are the exit edges for the synchronized segment.
                    CFGNode onlySuccessor = Iterators.fromSingleton(cfg.getSuccessors(currentNode));
                    if (onlySuccessor != null && onlySuccessor instanceof ControlFlowGraph.ReturnNode) {
                        Iterators.addAll(monitorExitEdges, cfg.outgoingEdgesOf(onlySuccessor));
                    } else {
                        Iterators.addAll(monitorExitEdges, cfg.outgoingEdgesOf(currentNode));
                    }
                    break;
                }
            } else if (ih.getInstruction() instanceof MONITORENTER) {
                currentNestingLevel++;
            }
        }
        if (!isExit) {
            for (CFGNode node : cfg.getSuccessors(currentNode)) {
                todo.add(new Pair<CFGNode, Integer>(node, currentNestingLevel));
            }
        }
        currentNode = null;
        while (!todo.isEmpty()) {
            Pair<CFGNode, Integer> nextPair = todo.pop();
            CFGNode nextNode = nextPair.first();
            if (!visited.contains(nextNode)) {
                visited.add(nextNode);
                if (cfg.outgoingEdgesOf(nextNode).isEmpty()) {
                    throw new AssertionError("Found monitor-exit free path from monitorenter to the end of a function. In: " + cfg);
                } else if (nextNode.getBasicBlock() == null) {
                    for (CFGNode node : cfg.getSuccessors(nextNode)) {
                        todo.add(new Pair<CFGNode, Integer>(node, nextPair.second()));
                    }
                } else {
                    currentNode = nextNode;
                    currentNestingLevel = nextPair.second();
                    insIter = currentNode.getBasicBlock().getInstructions().iterator();
                    break;
                }
            }
        }
    } while (currentNode != null);
    Set<SuperGraphEdge> exitEdges = Iterators.addAll(new HashSet<SuperGraphEdge>(), superGraph.liftCFGEdges(rootMethod, monitorExitEdges));
    return new Segment(superGraph, entryEdges, exitEdges);
}
Also used : MONITORENTER(org.apache.bcel.generic.MONITORENTER) InstructionHandle(org.apache.bcel.generic.InstructionHandle) MONITOREXIT(org.apache.bcel.generic.MONITOREXIT) HashSet(java.util.HashSet) CFGEdge(com.jopdesign.common.code.ControlFlowGraph.CFGEdge) Pair(com.jopdesign.common.graphutils.Pair) CFGNode(com.jopdesign.common.code.ControlFlowGraph.CFGNode) Stack(java.util.Stack) ContextCFG(com.jopdesign.common.code.SuperGraph.ContextCFG) SuperGraphEdge(com.jopdesign.common.code.SuperGraph.SuperGraphEdge)

Example 42 with Stack

use of java.util.Stack in project k-9 by k9mail.

the class MessageHelper method isCompletePartAvailable.

public static boolean isCompletePartAvailable(Part part) {
    Stack<Part> partsToCheck = new Stack<Part>();
    partsToCheck.push(part);
    while (!partsToCheck.isEmpty()) {
        Part currentPart = partsToCheck.pop();
        Body body = currentPart.getBody();
        boolean isBodyMissing = body == null;
        if (isBodyMissing) {
            return false;
        }
        if (body instanceof Multipart) {
            Multipart multipart = (Multipart) body;
            for (BodyPart bodyPart : multipart.getBodyParts()) {
                partsToCheck.push(bodyPart);
            }
        }
    }
    return true;
}
Also used : BodyPart(com.fsck.k9.mail.BodyPart) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart) Multipart(com.fsck.k9.mail.Multipart) BodyPart(com.fsck.k9.mail.BodyPart) Part(com.fsck.k9.mail.Part) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart) Body(com.fsck.k9.mail.Body) Stack(java.util.Stack)

Example 43 with Stack

use of java.util.Stack in project MaterialDrawer by mikepenz.

the class AccountHeaderBuilder method calculateProfiles.

/**
     * helper method to calculate the order of the profiles
     */
protected void calculateProfiles() {
    if (mProfiles == null) {
        mProfiles = new ArrayList<>();
    }
    if (mCurrentProfile == null) {
        int setCount = 0;
        for (int i = 0; i < mProfiles.size(); i++) {
            if (mProfiles.size() > i && mProfiles.get(i).isSelectable()) {
                if (setCount == 0 && (mCurrentProfile == null)) {
                    mCurrentProfile = mProfiles.get(i);
                } else if (setCount == 1 && (mProfileFirst == null)) {
                    mProfileFirst = mProfiles.get(i);
                } else if (setCount == 2 && (mProfileSecond == null)) {
                    mProfileSecond = mProfiles.get(i);
                } else if (setCount == 3 && (mProfileThird == null)) {
                    mProfileThird = mProfiles.get(i);
                }
                setCount++;
            }
        }
        return;
    }
    IProfile[] previousActiveProfiles = new IProfile[] { mCurrentProfile, mProfileFirst, mProfileSecond, mProfileThird };
    IProfile[] newActiveProfiles = new IProfile[4];
    Stack<IProfile> unusedProfiles = new Stack<>();
    // try to keep existing active profiles in the same positions
    for (int i = 0; i < mProfiles.size(); i++) {
        IProfile p = mProfiles.get(i);
        if (p.isSelectable()) {
            boolean used = false;
            for (int j = 0; j < 4; j++) {
                if (previousActiveProfiles[j] == p) {
                    newActiveProfiles[j] = p;
                    used = true;
                    break;
                }
            }
            if (!used) {
                unusedProfiles.push(p);
            }
        }
    }
    Stack<IProfile> activeProfiles = new Stack<>();
    // try to fill the gaps with new available profiles
    for (int i = 0; i < 4; i++) {
        if (newActiveProfiles[i] != null) {
            activeProfiles.push(newActiveProfiles[i]);
        } else if (!unusedProfiles.isEmpty()) {
            activeProfiles.push(unusedProfiles.pop());
        }
    }
    Stack<IProfile> reversedActiveProfiles = new Stack<>();
    while (!activeProfiles.empty()) {
        reversedActiveProfiles.push(activeProfiles.pop());
    }
    // reassign active profiles
    if (reversedActiveProfiles.isEmpty()) {
        mCurrentProfile = null;
    } else {
        mCurrentProfile = reversedActiveProfiles.pop();
    }
    if (reversedActiveProfiles.isEmpty()) {
        mProfileFirst = null;
    } else {
        mProfileFirst = reversedActiveProfiles.pop();
    }
    if (reversedActiveProfiles.isEmpty()) {
        mProfileSecond = null;
    } else {
        mProfileSecond = reversedActiveProfiles.pop();
    }
    if (reversedActiveProfiles.isEmpty()) {
        mProfileThird = null;
    } else {
        mProfileThird = reversedActiveProfiles.pop();
    }
}
Also used : IProfile(com.mikepenz.materialdrawer.model.interfaces.IProfile) Stack(java.util.Stack)

Example 44 with Stack

use of java.util.Stack in project graphdb by neo4j-attic.

the class OldTraverserWrapper method toExpander.

private static RelationshipExpander toExpander(Object[] relationshipTypesAndDirections) {
    Stack<Object[]> entries = new Stack<Object[]>();
    for (int i = 0; i < relationshipTypesAndDirections.length; i += 2) {
        Object relType = relationshipTypesAndDirections[i];
        if (relType == null) {
            throw new IllegalArgumentException("Null relationship type at " + i);
        }
        if (!(relType instanceof RelationshipType)) {
            throw new IllegalArgumentException("Expected RelationshipType at var args pos " + i + ", found " + relType);
        }
        Object direction = relationshipTypesAndDirections[i + 1];
        if (direction == null) {
            throw new IllegalArgumentException("Null direction at " + (i + 1));
        }
        if (!(direction instanceof Direction)) {
            throw new IllegalArgumentException("Expected Direction at var args pos " + (i + 1) + ", found " + direction);
        }
        entries.push(new Object[] { relType, direction });
    }
    OrderedByTypeExpander expander = new OrderedByTypeExpander();
    while (!entries.isEmpty()) {
        Object[] entry = entries.pop();
        expander = (OrderedByTypeExpander) expander.add((RelationshipType) entry[0], (Direction) entry[1]);
    }
    return expander;
}
Also used : RelationshipType(org.neo4j.graphdb.RelationshipType) OrderedByTypeExpander(org.neo4j.kernel.OrderedByTypeExpander) Direction(org.neo4j.graphdb.Direction) Stack(java.util.Stack)

Example 45 with Stack

use of java.util.Stack in project neo4j-mobile-android by neo4j-contrib.

the class OldTraverserWrapper method toExpander.

private static RelationshipExpander toExpander(Object[] relationshipTypesAndDirections) {
    Stack<Object[]> entries = new Stack<Object[]>();
    for (int i = 0; i < relationshipTypesAndDirections.length; i += 2) {
        Object relType = relationshipTypesAndDirections[i];
        if (relType == null) {
            throw new IllegalArgumentException("Null relationship type at " + i);
        }
        if (!(relType instanceof RelationshipType)) {
            throw new IllegalArgumentException("Expected RelationshipType at var args pos " + i + ", found " + relType);
        }
        Object direction = relationshipTypesAndDirections[i + 1];
        if (direction == null) {
            throw new IllegalArgumentException("Null direction at " + (i + 1));
        }
        if (!(direction instanceof Direction)) {
            throw new IllegalArgumentException("Expected Direction at var args pos " + (i + 1) + ", found " + direction);
        }
        entries.push(new Object[] { relType, direction });
    }
    OrderedByTypeExpander expander = new OrderedByTypeExpander();
    while (!entries.isEmpty()) {
        Object[] entry = entries.pop();
        expander = (OrderedByTypeExpander) expander.add((RelationshipType) entry[0], (Direction) entry[1]);
    }
    return expander;
}
Also used : RelationshipType(org.neo4j.graphdb.RelationshipType) OrderedByTypeExpander(org.neo4j.kernel.OrderedByTypeExpander) Direction(org.neo4j.graphdb.Direction) Stack(java.util.Stack)

Aggregations

Stack (java.util.Stack)245 HashSet (java.util.HashSet)42 ArrayList (java.util.ArrayList)37 File (java.io.File)23 IOException (java.io.IOException)22 View (android.view.View)18 Test (org.junit.Test)18 ViewGroup (android.view.ViewGroup)14 HashMap (java.util.HashMap)14 Set (java.util.Set)12 LinkedList (java.util.LinkedList)11 List (java.util.List)11 ImageView (android.widget.ImageView)10 Map (java.util.Map)10 Document (org.w3c.dom.Document)10 TestInputHandler (org.apache.maven.plugins.repository.testutil.TestInputHandler)9 PostfixMathCommandI (org.nfunk.jep.function.PostfixMathCommandI)9 DocumentBuilder (javax.xml.parsers.DocumentBuilder)8 NotificationPanelView (com.android.systemui.statusbar.phone.NotificationPanelView)7 TreeSet (java.util.TreeSet)7