public static <T> T first(final Iterable<T> collection) { if (collection instanceof List<?>) { return first((List<T>) collection); } final Iterator<T> it = VerifyArgument.notNull(collection, "collection").iterator(); if (it.hasNext()) { return it.next(); } throw Error.sequenceHasNoElements(); }
public static <T> T get(final Iterable<T> collection, final int index) { if (VerifyArgument.notNull(collection, "collection") instanceof List<?>) { return get((List<T>) collection, index); } int i = 0; for (final T item : collection) { if (i++ == index) { return item; } } throw Error.indexOutOfRange(index); }
public static <T> int indexOfByIdentity(final Iterable<?> collection, final T item) { VerifyArgument.notNull(collection, "collection"); if (collection instanceof List<?>) { return indexOfByIdentity((List<?>) collection, item); } int i = -1; for (final Object o : collection) { ++i; if (o == item) { return i; } } return -1; }
private boolean isSingleStoreBoolean(final Variable variable) { if (_singleStoreVariables.contains(variable)) { final List<ExpressionToInfer> assignments = _assignmentExpressions.get(variable); final ExpressionToInfer e = single(assignments); return matchBooleanConstant(last(e.expression.getArguments())) != null; } return false; }
public static <T> boolean sequenceDeepEquals(final Iterable<? extends T> first, final Iterable<? extends T> second) { VerifyArgument.notNull(first, "first"); VerifyArgument.notNull(second, "second"); if (first == second) { return true; } if (first instanceof List<?> && second instanceof List<?>) { return sequenceDeepEquals((List<?>) first, (List<?>) second); } final Iterator<? extends T> firstIterator = first.iterator(); final Iterator<? extends T> secondIterator = second.iterator(); while (firstIterator.hasNext()) { if (!secondIterator.hasNext()) { return false; } if (!sequenceDeepEqualsCore(firstIterator.next(), secondIterator.next())) { return false; } } return !secondIterator.hasNext(); }
final InvocationExpression hashCodeCall = first(m2.<InvocationExpression>get("hashCodeCall")); final MemberReference hashCodeMethod = hashCodeCall.getUserData(Keys.MEMBER_REFERENCE); tableSwitchInput = first(m3.<IdentifierExpression>get("tableSwitchInput")); assert tableSwitchInput != null; final List<PrimitiveExpression> stringValues = toList(m3.<PrimitiveExpression>get("stringValue")); final List<PrimitiveExpression> tableSwitchCaseValues = toList(m3.<PrimitiveExpression>get("tableSwitchCaseValue")); final boolean allCasesFound = all( tableSwitch.getSwitchSections(), new Predicate<SwitchSection>() { final AstNode newInput = first(m2.<AstNode>get("target"));
public static <T> T last(final Iterable<T> collection) { VerifyArgument.notNull(collection, "collection"); if (collection instanceof List<?>) { return last((List<T>) collection); } final Iterator<T> iterator = collection.iterator(); final boolean hasAny = iterator.hasNext(); if (!hasAny) { throw Error.sequenceHasNoElements(); } T last; do { last = iterator.next(); } while (iterator.hasNext()); return last; }
@Override public final boolean equals(final Object o) { if (this == o) { return true; } if (!(o instanceof Frame)) { return false; } final Frame frame = (Frame) o; return frame._frameType == _frameType && CollectionUtilities.sequenceDeepEquals(frame._localValues, _localValues) && CollectionUtilities.sequenceDeepEquals(frame._stackValues, _stackValues); }
final Node lastInTry = lastOrDefault(tryAst, NOT_A_LABEL_OR_NOP); final Expression firstExpression = firstOrDefault( catchBlock.getSelfAndChildrenRecursive(Expression.class), new Predicate<Expression>() { nestedHandlers.add(e); final int nestedEndIndex = firstIndexWhere( body, new Predicate<ByteCode>() { final Node lastInHandler = lastOrDefault(handlerAst, NOT_A_LABEL_OR_NOP); final Expression finallyStart = firstOrDefault(finallyBlock.getSelfAndChildrenRecursive(Expression.class)); final Expression last; first = firstOrDefault(tryCatchBlock.getTryBlock().getSelfAndChildrenRecursive(Expression.class)); final CatchBlock lastCatch = lastOrDefault(tryCatchBlock.getCatchBlocks()); if (lastCatch == null) { last = null; last = lastOrDefault(lastCatch.getSelfAndChildrenRecursive(Expression.class)); last = lastOrDefault(finallyBlock.getSelfAndChildrenRecursive(Expression.class));
public static <T> T single(final Iterable<T> collection) { if (collection instanceof List<?>) { return single((List<T>) collection); } final Iterator<T> it = VerifyArgument.notNull(collection, "collection").iterator(); if (it.hasNext()) { final T result = it.next(); if (it.hasNext()) { throw Error.sequenceHasMultipleElements(); } return result; } throw Error.sequenceHasNoElements(); }
final Node next = getOrDefault(body, position + 1); final Variable v = variable.get(); final Expression e = inlinedExpression.get(); final Expression nestedValue = single(e.getArguments()); current.getArguments().set(0, single(e.getArguments()));
@Override public Void visitMethodDeclaration(final MethodDeclaration node, final Void p) { final MethodDefinition methodDefinition = node.getUserData(Keys.METHOD_DEFINITION); if (isSwitchMapMethod(methodDefinition)) { final Match m = SWITCH_TABLE_METHOD_BODY.match(node.getBody()); if (m.success()) { final MemberReferenceExpression fieldAccess = first(m.<MemberReferenceExpression>get("fieldAccess")); final FieldReference field = (FieldReference) fieldAccess.getUserData(Keys.MEMBER_REFERENCE); final List<MemberReferenceExpression> enumValues = toList(m.<MemberReferenceExpression>get("enumValue")); final List<PrimitiveExpression> tableValues = toList(m.<PrimitiveExpression>get("tableValue")); assert field != null && tableValues.size() == enumValues.size(); final String key = makeKey(field); SwitchMapInfo info = _switchMaps.get(key); if (info == null) { _switchMaps.put(key, info = new SwitchMapInfo(field)); } info.switchMapMethodDeclaration = node; for (int i = 0; i < enumValues.size(); i++) { final MemberReferenceExpression memberReference = enumValues.get(i); final IdentifierExpression identifier = new IdentifierExpression( Expression.MYSTERY_OFFSET, memberReference.getMemberName()); identifier.putUserData(Keys.MEMBER_REFERENCE, memberReference.getUserData(Keys.MEMBER_REFERENCE)); info.mappings.put((Integer) tableValues.get(i).getValue(), identifier); } } } return super.visitMethodDeclaration(node, p); }
public static boolean matchLoadAny(final Node node, final Iterable<Variable> expectedVariables) { return any( expectedVariables, new Predicate<Variable>() { @Override public boolean test(final Variable variable) { return matchLoad(node, variable); } } ); }
assert CollectionUtilities.contains(targetStatement.getAncestors(), parent);
@Override public boolean test(final SwitchSection s) { return !s.getCaseLabels().isEmpty() && all( s.getCaseLabels(), new Predicate<CaseLabel>() { @Override public boolean test(final CaseLabel c) { return c.getExpression().isNull() || (c.getExpression() instanceof PrimitiveExpression && ((PrimitiveExpression) c.getExpression()).getValue() instanceof Integer && tableInputMap.containsKey(((PrimitiveExpression) c.getExpression()).getValue())); } } ); } }
private boolean isSingleStoreBoolean(final Variable variable) { if (_singleStoreVariables.contains(variable)) { final List<ExpressionToInfer> assignments = _assignmentExpressions.get(variable); final ExpressionToInfer e = single(assignments); return matchBooleanConstant(last(e.expression.getArguments())) != null; } return false; }
final InvocationExpression hashCodeCall = first(m2.<InvocationExpression>get("hashCodeCall")); final MemberReference hashCodeMethod = hashCodeCall.getUserData(Keys.MEMBER_REFERENCE); tableSwitchInput = first(m3.<IdentifierExpression>get("tableSwitchInput")); assert tableSwitchInput != null; final List<PrimitiveExpression> stringValues = toList(m3.<PrimitiveExpression>get("stringValue")); final List<PrimitiveExpression> tableSwitchCaseValues = toList(m3.<PrimitiveExpression>get("tableSwitchCaseValue")); final boolean allCasesFound = all( tableSwitch.getSwitchSections(), new Predicate<SwitchSection>() { final AstNode newInput = first(m2.<AstNode>get("target"));