@Override public boolean visit(JDeclaredType type, Context ctx) { assert referencedTypes.contains(type); Predicate<JNode> notReferenced = Predicates.not(Predicates.in(referencedNonTypes)); removeFields(notReferenced, type); removeMethods(notReferenced, type); for (JMethod method : type.getMethods()) { accept(method); } return false; }
public static boolean isRepresentedAsNative(String className) { return FluentIterable.from(Arrays.asList(DispatchType.values())) .transform( new Function<DispatchType, String>() { @Override public String apply(DispatchType dispatchType) { return dispatchType.getclassName(); } }) .filter(Predicates.notNull()) .toSet() .contains(className); }
/** * Creates a multi expression from a list of expressions, removing expressions that do not have * side effects if possible. */ public static JExpression createOptimizedMultiExpression( boolean ignoringResult, List<JExpression> expressions) { int numberOfExpressions = expressions.size(); JExpression result = expressions.get(numberOfExpressions - 1); numberOfExpressions = expressions.size(); if (numberOfExpressions == 0) { return new JMultiExpression(SourceOrigin.UNKNOWN); } expressions = Lists.newArrayList( Collections2.filter( expressions.subList(0, numberOfExpressions - 1), Predicates.and( Predicates.notNull(), new Predicate<JExpression>() { @Override public boolean apply(JExpression expression) { return expression.hasSideEffects(); } }))); if (result != null && (!ignoringResult || result.hasSideEffects())) { expressions.add(result); } if (expressions.size() == 1) { // Do not create a multi expression if it consists only of the result. return expressions.iterator().next(); } SourceInfo info = expressions.size() > 0 ? expressions.get(0).getSourceInfo() : SourceOrigin.UNKNOWN; return new JMultiExpression(info, expressions); }
// Arguments for pruned parameters will be pushed right into a multiexpression that will be // evaluated with the next arg, e.g. m(arg1, (prunnedArg2, prunnedArg3, arg4)). private void maybeReplaceForPrunedParameters(JMethodCall x, Context ctx) { if (!priorParametersByMethod.containsKey(x.getTarget())) { // No parameter was pruned. return; } JMethodCall replacementCall = x.cloneWithoutParameters(); assert !x.getTarget().canBePolymorphic(); List<JParameter> originalParams = priorParametersByMethod.get(x.getTarget()); // The method and the call agree in the number of parameters. assert originalParams.size() == x.getArgs().size(); // Traverse the call arguments left to right. SourceInfo sourceInfo = x.getSourceInfo(); JMultiExpression unevaluatedArgumentsForPrunedParameters = new JMultiExpression(sourceInfo); List<JExpression> args = x.getArgs(); for (int currentArgumentIndex = 0; currentArgumentIndex < args.size(); ++currentArgumentIndex) { JExpression arg = args.get(currentArgumentIndex); // If the parameter was not pruned . if (referencedNonTypes.contains(originalParams.get(currentArgumentIndex))) { // Add the current argument to the list of unevaluated arguments and pass the multi // expression to the call. unevaluatedArgumentsForPrunedParameters.addExpressions(arg); replacementCall.addArg(unevaluatedArgumentsForPrunedParameters); // Reset the accumulating multi expression. unevaluatedArgumentsForPrunedParameters = new JMultiExpression(sourceInfo); } else if (arg.hasSideEffects()) { // If the argument was pruned and has sideffects accumulate it; otherwise discard. unevaluatedArgumentsForPrunedParameters.addExpressions(arg); } } if (unevaluatedArgumentsForPrunedParameters.isEmpty()) { // We are done, all (side effectful) parameters have been evaluated. ctx.replaceMe(replacementCall); return; } // If the last few parameters where pruned, we need to evaluate the (side effectful) arguments // for those parameters. if (replacementCall.getArgs().isEmpty()) { // All parameters have been pruned, replace by (prunedArg1, ..., prunedArgn, m()). unevaluatedArgumentsForPrunedParameters.addExpressions(replacementCall); ctx.replaceMe(unevaluatedArgumentsForPrunedParameters); return; } // Some parameters have been pruned from the end, replace by // m(arg1,..., (lastArg = lastUnprunedArg, remainingArgs, lastArg)) JExpression lastArg = Iterables.getLast(replacementCall.getArgs()); JLocal tempVar = createTempLocal( sourceInfo, Iterables.getLast(Iterables.filter(originalParams, Predicates.in(referencedNonTypes))) .getType()); unevaluatedArgumentsForPrunedParameters.addExpressions( 0, JProgram.createAssignment( lastArg.getSourceInfo(), new JLocalRef(sourceInfo, tempVar), lastArg)); unevaluatedArgumentsForPrunedParameters.addExpressions(new JLocalRef(sourceInfo, tempVar)); replacementCall.setArg( replacementCall.getArgs().size() - 1, unevaluatedArgumentsForPrunedParameters); ctx.replaceMe(replacementCall); }