text:
package com.intellij.codeInspection.dataFlow;

import com.intellij.codeInsight.AnnotationUtil;
import com.intellij.codeInsight.ConditionCheckManager;
import com.intellij.codeInsight.ConditionChecker;
import com.intellij.codeInsight.ExceptionUtil;
import com.intellij.codeInspection.dataFlow.instructions.*;
import com.intellij.codeInspection.dataFlow.value.*;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Condition;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.*;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.tree.IElementType;
import com.intellij.psi.util.*;
import com.intellij.util.IncorrectOperationException;
import com.intellij.util.SmartList;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.containers.Stack;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.*;

import static com.intellij.codeInsight.ConditionChecker.Type.*;
import static com.intellij.codeInspection.dataFlow.MethodContract.ValueConstraint;
import static com.intellij.psi.CommonClassNames.*;

class ControlFlowAnalyzer extends JavaElementVisitor {
  private static final Logger LOG = Logger.getInstance("#com.intellij.codeInspection.dataFlow.ControlFlowAnalyzer");
  private static final int NOT_FOUND = -10;
  private boolean myIgnoreAssertions;

  private static class CannotAnalyzeException extends RuntimeException { }

  private final DfaValueFactory myFactory;
  private ControlFlow myPass1Flow;
  private ControlFlow myCurrentFlow;
  private int myPassNumber;
  private Set<DfaVariableValue> myFields;
  private Stack<CatchDescriptor> myCatchStack;
  private DfaValue myRuntimeException;
  private DfaValue myError;
  private PsiType myNpe;

  ControlFlowAnalyzer(final DfaValueFactory valueFactory) {
    myFactory = valueFactory;
  }

  public ControlFlow buildControlFlow(@NotNull PsiElement codeFragment, boolean ignoreAssertions) {
    myIgnoreAssertions = ignoreAssertions;
    PsiManager manager = codeFragment.getManager();
    GlobalSearchScope scope = codeFragment.getResolveScope();
    myRuntimeException = myFactory.getNotNullFactory().create(PsiType.getJavaLangRuntimeException(manager, scope));
    myError = myFactory.getNotNullFactory().create(PsiType.getJavaLangError(manager, scope));
    myNpe = JavaPsiFacade.getElementFactory(manager.getProject()).createTypeByFQClassName(JAVA_LANG_NULL_POINTER_EXCEPTION, scope);
    myFields = new HashSet<DfaVariableValue>();
    myCatchStack = new Stack<CatchDescriptor>();
    myPassNumber = 1;
    myPass1Flow = new ControlFlow(myFactory);
    myCurrentFlow = myPass1Flow;

    try {
      codeFragment.accept(this);
    }
    catch (CannotAnalyzeException e) {
      return null;
    }

    myPassNumber = 2;
    final ControlFlow pass2Flow = new ControlFlow(myFactory);
    myCurrentFlow = pass2Flow;

    codeFragment.accept(this);

    pass2Flow.setFields(myFields.toArray(new DfaVariableValue[myFields.size()]));

    if (myPass1Flow.getInstructionCount() != pass2Flow.getInstructionCount()) {
      LOG.error(Arrays.toString(myPass1Flow.getInstructions()) + "!=\n" + Arrays.toString(pass2Flow.getInstructions()));
    }

    addInstruction(new ReturnInstruction());

    return pass2Flow;
  }

  private boolean myRecursionStopper = false;

  private void addInstruction(Instruction i) {
    ProgressManager.checkCanceled();

    if (!myRecursionStopper) {
      myRecursionStopper = true;
      try {
        // add extra conditional goto in order to handle possible runtime exceptions that could be caught by finally block
        if (i instanceof BranchingInstruction || i instanceof AssignInstruction || i instanceof MethodCallInstruction) {
          addConditionalRuntimeThrow();
        }
      }
      finally {
        myRecursionStopper = false;
      }
    }

    myCurrentFlow.addInstruction(i);
  }

  private int getEndOffset(PsiElement element) {
    return myPassNumber == 2 ? myPass1Flow.getEndOffset(element) : 0;
  }

  private int getStartOffset(PsiElement element) {
    return myPassNumber == 2 ? myPass1Flow.getStartOffset(element) : 0;
  }

  private void startElement(PsiElement element) {
    myCurrentFlow.startElement(element);
  }

  private void finishElement(PsiElement element) {
    myCurrentFlow.finishElement(element);
  }

  @Override
  public void visitErrorElement(PsiErrorElement element) {
    throw new CannotAnalyzeException();
  }

  @Override public void visitAssignmentExpression(PsiAssignmentExpression expression) {
    startElement(expression);

    try {
      PsiExpression lExpr = expression.getLExpression();
      PsiExpression rExpr = expression.getRExpression();

      if (rExpr == null) {
        pushUnknown();
        return;
      }

      lExpr.accept(this);

      IElementType op = expression.getOperationTokenType();
      PsiType type = expression.getType();
      boolean isBoolean = PsiType.BOOLEAN.equals(type);
      if (op == JavaTokenType.EQ) {
        rExpr.accept(this);
        generateBoxingUnboxingInstructionFor(rExpr, type);
      }
      else if (op == JavaTokenType.ANDEQ) {
        if (isBoolean) {
          generateNonLazyExpression(true, lExpr, rExpr, type);
        }
        else {
          generateDefaultBinOp(lExpr, rExpr, type);
        }
      }
      else if (op == JavaTokenType.OREQ) {
        if (isBoolean) {
          generateNonLazyExpression(false, lExpr, rExpr, type);
        }
        else {
          generateDefaultBinOp(lExpr, rExpr, type);
        }
      }
      else if (op == JavaTokenType.XOREQ) {
        if (isBoolean) {
          generateXorExpression(expression, new PsiExpression[]{lExpr, rExpr}, type);
        }
        else {
          generateDefaultBinOp(lExpr, rExpr, type);
        }
      }
      else if (op == JavaTokenType.PLUSEQ && type != null && type.equalsToText(JAVA_LANG_STRING)) {
        lExpr.accept(this);
        rExpr.accept(this);
        addInstruction(new BinopInstruction(JavaTokenType.PLUS, null, lExpr.getProject()));
      }
      else {
        generateDefaultBinOp(lExpr, rExpr, type);
      }

      addInstruction(new AssignInstruction(rExpr));
    }
    finally {
      finishElement(expression);
    }
  }

  private void generateDefaultBinOp(PsiExpression lExpr, PsiExpression rExpr, final PsiType exprType) {
    lExpr.accept(this);
    generateBoxingUnboxingInstructionFor(lExpr,exprType);
    rExpr.accept(this);
    generateBoxingUnboxingInstructionFor(rExpr,exprType);
    addInstruction(new BinopInstruction(null, null, lExpr.getProject()));
  }

  @Override public void visitAssertStatement(PsiAssertStatement statement) {
    if (myIgnoreAssertions) {
      return;
    }

    startElement(statement);
    final PsiExpression condition = statement.getAssertCondition();
    final PsiExpression description = statement.getAssertDescription();
    if (condition != null) {
      condition.accept(this);
      generateBoxingUnboxingInstructionFor(condition, PsiType.BOOLEAN);
      addInstruction(new ConditionalGotoInstruction(getEndOffset(statement), false, condition));
      if (description != null) {
        description.accept(this);
      }
      addInstruction(new ReturnInstruction());
    }
    finishElement(statement);
  }

  @Override public void visitDeclarationStatement(PsiDeclarationStatement statement) {
    startElement(statement);

    PsiElement[] elements = statement.getDeclaredElements();
    for (PsiElement element : elements) {
      if (element instanceof PsiClass) {
        addInstruction(new EmptyInstruction(element));
      }
      else if (element instanceof PsiVariable) {
        PsiVariable variable = (PsiVariable)element;
        PsiExpression initializer = variable.getInitializer();
        if (initializer != null) {
          initializeVariable(variable, initializer);
        }
      }
    }

    finishElement(statement);
  }

  @Override
  public void visitField(PsiField field) {
    PsiExpression initializer = field.getInitializer();
    if (initializer != null) {
      initializeVariable(field, initializer);
    }
  }

  private void initializeVariable(PsiVariable variable, PsiExpression initializer) {
    DfaVariableValue dfaVariable = myFactory.getVarFactory().createVariableValue(variable, false);
    addInstruction(new PushInstruction(dfaVariable, initializer));
    initializer.accept(this);
    generateBoxingUnboxingInstructionFor(initializer, variable.getType());
    addInstruction(new AssignInstruction(initializer));
    addInstruction(new PopInstruction());
  }

  @Override
  public void visitCodeFragment(JavaCodeFragment codeFragment) {
    startElement(codeFragment);
    if (codeFragment instanceof PsiExpressionCodeFragment) {
      PsiExpression expression = ((PsiExpressionCodeFragment)codeFragment).getExpression();
      if (expression != null) {
        expression.accept(this);
      }
    }
    finishElement(codeFragment);
  }

  @Override public void visitCodeBlock(PsiCodeBlock block) {
    startElement(block);

    for (PsiStatement statement : block.getStatements()) {
      if (statement instanceof PsiDeclarationStatement) {
        for (PsiElement declaration : ((PsiDeclarationStatement)statement).getDeclaredElements()) {
          if (declaration instanceof PsiVariable) {
            myCurrentFlow.removeVariable((PsiVariable)declaration);
          }
        }
      }
      statement.accept(this);
    }

    finishElement(block);
  }

  @Override public void visitBlockStatement(PsiBlockStatement statement) {
    startElement(statement);
    statement.getCodeBlock().accept(this);
    finishElement(statement);
  }

  @Override public void visitBreakStatement(PsiBreakStatement statement) {
    startElement(statement);

    PsiStatement exitedStatement = statement.findExitedStatement();

    if (exitedStatement != null) {
      int offset = myPass1Flow.getEndOffset(exitedStatement);
      if (offset == -1) offset = myPass1Flow.getInstructionCount();
      addInstruction(new GotoInstruction(offset));
    }

    finishElement(statement);
  }

  @Override public void visitContinueStatement(PsiContinueStatement statement) {
    startElement(statement);
    PsiStatement continuedStatement = statement.findContinuedStatement();
    if (continuedStatement != null) {
      int offset = -1;
      if (continuedStatement instanceof PsiForStatement) {
        PsiStatement body = ((PsiForStatement)continuedStatement).getBody();
        offset = myPass1Flow.getEndOffset(body);
      }
      else if (continuedStatement instanceof PsiWhileStatement) {
        PsiStatement body = ((PsiWhileStatement)continuedStatement).getBody();
        offset = myPass1Flow.getEndOffset(body);
      }
      else if (continuedStatement instanceof PsiDoWhileStatement) {
        PsiStatement body = ((PsiDoWhileStatement)continuedStatement).getBody();
        offset = myPass1Flow.getEndOffset(body);
      }
      else if (continuedStatement instanceof PsiForeachStatement) {
        PsiStatement body = ((PsiForeachStatement)continuedStatement).getBody();
        offset = myPass1Flow.getEndOffset(body);
      }
      Instruction instruction = offset == -1 ? new EmptyInstruction(null) : new GotoInstruction(offset);
      addInstruction(instruction);
    }
    finishElement(statement);
  }

  @Override public void visitDoWhileStatement(PsiDoWhileStatement statement) {
    startElement(statement);

    PsiStatement body = statement.getBody();
    if (body != null) {
      body.accept(this);
      PsiExpression condition = statement.getCondition();
      if (condition != null) {
        condition.accept(this);
        generateBoxingUnboxingInstructionFor(condition, PsiType.BOOLEAN);
        addInstruction(new ConditionalGotoInstruction(getStartOffset(statement), false, condition));
      }
    }

    finishElement(statement);
  }

  @Override public void visitEmptyStatement(PsiEmptyStatement statement) {
    startElement(statement);
    finishElement(statement);
  }

  @Override public void visitExpressionStatement(PsiExpressionStatement statement) {
    startElement(statement);
    final PsiExpression expr = statement.getExpression();
    expr.accept(this);
    addInstruction(new PopInstruction());
    finishElement(statement);
  }

  @Override public void visitExpressionListStatement(PsiExpressionListStatement statement) {
    startElement(statement);
    PsiExpression[] expressions = statement.getExpressionList().getExpressions();
    for (PsiExpression expr : expressions) {
      expr.accept(this);
      addInstruction(new PopInstruction());
    }
    finishElement(statement);
  }

  @Override public void visitForeachStatement(PsiForeachStatement statement) {
    startElement(statement);
    final PsiParameter parameter = statement.getIterationParameter();
    final PsiExpression iteratedValue = statement.getIteratedValue();

    if (iteratedValue != null) {
      iteratedValue.accept(this);
      addInstruction(new FieldReferenceInstruction(iteratedValue, "Collection iterator or array.length"));
    }

    int offset = myCurrentFlow.getInstructionCount();
    DfaVariableValue dfaVariable = myFactory.getVarFactory().createVariableValue(parameter, false);
    addInstruction(new FlushVariableInstruction(dfaVariable));

    pushUnknown();
    addInstruction(new ConditionalGotoInstruction(getEndOffset(statement), true, null));

    final PsiStatement body = statement.getBody();
    if (body != null) {
      body.accept(this);
    }

    addInstruction(new GotoInstruction(offset));

    finishElement(statement);
    myCurrentFlow.removeVariable(parameter);
  }

  @Override public void visitForStatement(PsiForStatement statement) {
    startElement(statement);
    final ArrayList<PsiElement> declaredVariables = new ArrayList<PsiElement>();

    PsiStatement initialization = statement.getInitialization();
    if (initialization != null) {
      initialization.accept(this);
      initialization.accept(new JavaRecursiveElementWalkingVisitor() {
        @Override public void visitReferenceExpression(PsiReferenceExpression expression) {
          visitElement(expression);
        }

        @Override public void visitDeclarationStatement(PsiDeclarationStatement statement) {
          PsiElement[] declaredElements = statement.getDeclaredElements();
          for (PsiElement element : declaredElements) {
            if (element instanceof PsiVariable) {
              declaredVariables.add(element);
            }
          }
        }
      });
    }

    PsiExpression condition = statement.getCondition();
    if (condition != null) {
      condition.accept(this);
      generateBoxingUnboxingInstructionFor(condition, PsiType.BOOLEAN);
    }
    else {
      addInstruction(new PushInstruction(null, null));
    }
    addInstruction(new ConditionalGotoInstruction(getEndOffset(statement), true, condition));

    PsiStatement body = statement.getBody();
    if (body != null) {
      body.accept(this);
    }

    PsiStatement update = statement.getUpdate();
    if (update != null) {
      update.accept(this);
    }

    int offset = initialization != null
                 ? getEndOffset(initialization)
                 : getStartOffset(statement);

    addInstruction(new GotoInstruction(offset));
    finishElement(statement);

    for (PsiElement declaredVariable : declaredVariables) {
      PsiVariable psiVariable = (PsiVariable)declaredVariable;
      myCurrentFlow.removeVariable(psiVariable);
    }
  }

  @Override public void visitIfStatement(PsiIfStatement statement) {
    startElement(statement);

    PsiExpression condition = statement.getCondition();

    PsiStatement thenStatement = statement.getThenBranch();
    PsiStatement elseStatement = statement.getElseBranch();

    int offset = elseStatement != null
                 ? getStartOffset(elseStatement)
                 : getEndOffset(statement);

    if (condition != null) {
      condition.accept(this);
      generateBoxingUnboxingInstructionFor(condition, PsiType.BOOLEAN);
      addInstruction(new ConditionalGotoInstruction(offset, true, condition));
    }

    if (thenStatement != null) {
      thenStatement.accept(this);
    }

    if (elseStatement != null) {
      offset = getEndOffset(statement);
      Instruction instruction = new GotoInstruction(offset);
      addInstruction(instruction);
      elseStatement.accept(this);
    }

    finishElement(statement);
  }

  // in case of JspTemplateStatement
  @Override public void visitStatement(PsiStatement statement) {
    startElement(statement);
    finishElement(statement);
  }

  @Override public void visitLabeledStatement(PsiLabeledStatement statement) {
    startElement(statement);
    PsiStatement childStatement = statement.getStatement();
    if (childStatement != null) {
      childStatement.accept(this);
    }
    finishElement(statement);
  }

  @Override public void visitReturnStatement(PsiReturnStatement statement) {
    startElement(statement);

    PsiExpression returnValue = statement.getReturnValue();
    if (returnValue != null) {
      returnValue.accept(this);
      PsiMethod method = PsiTreeUtil.getParentOfType(statement, PsiMethod.class, true, PsiMember.class);
      if (method != null) {
        generateBoxingUnboxingInstructionFor(returnValue, method.getReturnType());
      }
      addInstruction(new CheckReturnValueInstruction(statement));
    }

    returnCheckingFinally();
    finishElement(statement);
  }

  private void returnCheckingFinally() {
    int finallyOffset = getFinallyOffset();
    if (finallyOffset != NOT_FOUND) {
      addInstruction(new GosubInstruction(finallyOffset));
    }
    addInstruction(new ReturnInstruction());
  }

  @Override public void visitSwitchLabelStatement(PsiSwitchLabelStatement statement) {
    startElement(statement);
    finishElement(statement);
  }

  @Override public void visitSwitchStatement(PsiSwitchStatement switchStmt) {
    startElement(switchStmt);
    PsiExpression caseExpression = switchStmt.getExpression();
    Set<PsiEnumConstant> enumValues = null;
    if (caseExpression != null /*&& !(caseExpression instanceof PsiReferenceExpression)*/) {
      caseExpression.accept(this);

      generateBoxingUnboxingInstructionFor(caseExpression, PsiType.INT);
      final PsiClass psiClass = PsiUtil.resolveClassInType(caseExpression.getType());
      if (psiClass != null && psiClass.isEnum()) {
        addInstruction(new FieldReferenceInstruction(caseExpression, "switch statement expression"));
        enumValues = new HashSet<PsiEnumConstant>();
        for (PsiField f : psiClass.getFields()) {
          if (f instanceof PsiEnumConstant) {
            enumValues.add((PsiEnumConstant)f);
          }
        }
      } else {
        addInstruction(new PopInstruction());
      }

    }

    PsiCodeBlock body = switchStmt.getBody();

    if (body != null) {
      PsiStatement[] statements = body.getStatements();
      PsiSwitchLabelStatement defaultLabel = null;
      for (PsiStatement statement : statements) {
        if (statement instanceof PsiSwitchLabelStatement) {
          PsiSwitchLabelStatement psiLabelStatement = (PsiSwitchLabelStatement)statement;
          if (psiLabelStatement.isDefaultCase()) {
            defaultLabel = psiLabelStatement;
          }
          else {
            try {
              int offset = getStartOffset(statement);
              PsiExpression caseValue = psiLabelStatement.getCaseValue();

              if (caseValue != null &&
                  caseExpression instanceof PsiReferenceExpression &&
                  ((PsiReferenceExpression)caseExpression).getQualifierExpression() == null &&
                  JavaPsiFacade.getInstance(body.getProject()).getConstantEvaluationHelper().computeConstantExpression(caseValue) != null) {

                addInstruction(new PushInstruction(getExpressionDfaValue((PsiReferenceExpression)caseExpression), caseExpression));
                caseValue.accept(this);
                addInstruction(new BinopInstruction(JavaTokenType.EQEQ, null, caseExpression.getProject()));
              }
              else {
                pushUnknown();
              }

              addInstruction(new ConditionalGotoInstruction(offset, false, statement));

              if (enumValues != null) {
                if (caseValue instanceof PsiReferenceExpression) {
                  //noinspection SuspiciousMethodCalls
                  enumValues.remove(((PsiReferenceExpression)caseValue).resolve());
                }
              }
            }
            catch (IncorrectOperationException e) {
              LOG.error(e);
            }
          }
        }
      }

      if (enumValues == null || !enumValues.isEmpty()) {
        int offset = defaultLabel != null ? getStartOffset(defaultLabel) : getEndOffset(body);
        addInstruction(new GotoInstruction(offset));
      }

      body.accept(this);
    }

    finishElement(switchStmt);
  }

  @Override public void visitSynchronizedStatement(PsiSynchronizedStatement statement) {
    startElement(statement);

    PsiExpression lock = statement.getLockExpression();
    if (lock != null) {
      lock.accept(this);
      addInstruction(new FieldReferenceInstruction(lock, "Synchronized value"));
    }

    addInstruction(new FlushVariableInstruction(null));

    PsiCodeBlock body = statement.getBody();
    if (body != null) {
      body.accept(this);
    }

    finishElement(statement);
  }

  @Override public void visitThrowStatement(PsiThrowStatement statement) {
    startElement(statement);

    PsiExpression exception = statement.getException();

    if (exception != null) {
      exception.accept(this);
      addInstruction(new DupInstruction());
      addInstruction(new PushInstruction(myFactory.getConstFactory().getNull(), null));
      addInstruction(new BinopInstruction(JavaTokenType.EQEQ, null, statement.getProject()));
      ConditionalGotoInstruction gotoInstruction = new ConditionalGotoInstruction(-1, true, null);
      addInstruction(gotoInstruction);
      addThrowCode(myNpe);
      gotoInstruction.setOffset(myCurrentFlow.getInstructionCount());
      addThrowCode(exception.getType());
    }

    finishElement(statement);
  }

  private void addConditionalRuntimeThrow() {
    for (int i = myCatchStack.size() - 1; i >= 0; i--) {
      CatchDescriptor cd = myCatchStack.get(i);
      if (cd.isFinally()) {
        addConditionalRuntimeThrow(cd, false);
        continue;
      }

      PsiType type = cd.getLubType();
      if (type instanceof PsiClassType && ExceptionUtil.isUncheckedExceptionOrSuperclass((PsiClassType)type)) {
        addConditionalRuntimeThrow(cd, true);
      }
    }
  }

  private void addConditionalRuntimeThrow(CatchDescriptor cd, boolean forCatch) {
    pushUnknown();
    final ConditionalGotoInstruction branch = new ConditionalGotoInstruction(-1, false, null);
    addInstruction(branch);
    addInstruction(new EmptyStackInstruction());
    if (forCatch) {
      PsiType type = cd.getLubType();
      boolean isRuntime = InheritanceUtil.isInheritor(type, JAVA_LANG_RUNTIME_EXCEPTION) || ExceptionUtil.isGeneralExceptionType(type);
      boolean isError = InheritanceUtil.isInheritor(type, JAVA_LANG_ERROR) || type.equalsToText(JAVA_LANG_THROWABLE);
      if (isRuntime != isError) {
        addInstruction(new PushInstruction(isRuntime ? myRuntimeException : myError, null));
        addGotoCatch(cd);
      } else {
        pushUnknown();
        final ConditionalGotoInstruction branch2 = new ConditionalGotoInstruction(-1, false, null);
        addInstruction(branch2);
        addInstruction(new PushInstruction(myError, null));
        addGotoCatch(cd);
        branch2.setOffset(myCurrentFlow.getInstructionCount());
        addInstruction(new PushInstruction(myRuntimeException, null));
        addGotoCatch(cd);
      }
    }
    else {
      addInstruction(new GosubInstruction(cd.getJumpOffset(this)));
      addInstruction(new ReturnInstruction());
    }
    branch.setOffset(myCurrentFlow.getInstructionCount());
  }

  private void addThrowCode(PsiType exceptionClass) {
    if (exceptionClass == null) return;
    for (int i = myCatchStack.size() - 1; i >= 0; i--) {
      CatchDescriptor cd = myCatchStack.get(i);
      if (cd.isFinally()) {
        addInstruction(new GosubInstruction(cd.getJumpOffset(this)));
      }
      else if (cd.getType().isAssignableFrom(exceptionClass)) { // Definite catch.
        addGotoCatch(cd);
        return;
      }
      else if (cd.getType().isConvertibleFrom(exceptionClass)) { // Probable catch
        addInstruction(new DupInstruction());
        pushUnknown();
        final ConditionalGotoInstruction branch = new ConditionalGotoInstruction(-1, false, null);
        addInstruction(branch);
        addGotoCatch(cd);
        branch.setOffset(myCurrentFlow.getInstructionCount());
      }
    }

    addInstruction(new ReturnInstruction());
  }

  /**
   * Exception is expected on the stack.
   */
  private void addGotoCatch(CatchDescriptor cd) {
    addInstruction(new PushInstruction(myFactory.getVarFactory().createVariableValue(cd.getParameter(), false), null));
    addInstruction(new SwapInstruction());
    myCurrentFlow.addInstruction(new AssignInstruction(null));
    addInstruction(new PopInstruction());
    addInstruction(new GotoInstruction(cd.getJumpOffset(this)));
  }

  private int getFinallyOffset() {
    for (int i = myCatchStack.size() - 1; i >= 0; i--) {
      CatchDescriptor cd = myCatchStack.get(i);
      if (cd.isFinally()) return cd.getJumpOffset(this);
    }

    return NOT_FOUND;
  }

  private static class ApplyNotNullInstruction extends Instruction {
    private PsiMethodCallExpression myCall;

    private ApplyNotNullInstruction(PsiMethodCallExpression call) {
      myCall = call;
    }

    @Override
    public DfaInstructionState[] accept(DataFlowRunner runner, DfaMemoryState state, InstructionVisitor visitor) {
      DfaValue value = state.pop();
      DfaValueFactory factory = runner.getFactory();
      if (state.applyCondition(
        factory.getRelationFactory().createRelation(value, factory.getConstFactory().getNull(), JavaTokenType.EQEQ, true))) {
        return nextInstruction(runner, state);
      }
      if (visitor instanceof StandardInstructionVisitor) {
        ((StandardInstructionVisitor)visitor).skipConstantConditionReporting(myCall);
      }
      return DfaInstructionState.EMPTY_ARRAY;
    }
  }

  private static class CatchDescriptor {
    private final PsiType myType;
    private final PsiParameter myParameter;
    private final PsiCodeBlock myBlock;
    private final boolean myIsFinally;

    public CatchDescriptor(PsiCodeBlock finallyBlock) {
      myType = null;
      myParameter = null;
      myBlock = finallyBlock;
      myIsFinally = true;
    }

    public CatchDescriptor(PsiParameter parameter, PsiCodeBlock catchBlock) {
      myType = parameter.getType();
      myParameter = parameter;
      myBlock = catchBlock;
      myIsFinally = false;
    }

    public PsiType getType() {
      return myType;
    }

    PsiType getLubType() {
      PsiType type = myType;
      if (type instanceof PsiDisjunctionType) {
        return ((PsiDisjunctionType)type).getLeastUpperBound();
      }
      return type;
    }

    public boolean isFinally() {
      return myIsFinally;
    }

    public int getJumpOffset(ControlFlowAnalyzer analyzer) {
      return analyzer.getStartOffset(myBlock);
    }

    public PsiParameter getParameter() {
      return myParameter;
    }
  }

  @Override
  public void visitTryStatement(PsiTryStatement statement) {
    startElement(statement);

    PsiResourceList resourceList = statement.getResourceList();
    PsiCodeBlock tryBlock = statement.getTryBlock();
    PsiCodeBlock finallyBlock = statement.getFinallyBlock();

    if (finallyBlock != null) {
      myCatchStack.push(new CatchDescriptor(finallyBlock));
    }

    int catchesPushCount = 0;
    PsiCatchSection[] sections = statement.getCatchSections();
    for (int i = sections.length - 1; i >= 0; i--) {
      PsiCatchSection section = sections[i];
      PsiCodeBlock catchBlock = section.getCatchBlock();
      PsiParameter parameter = section.getParameter();
      if (parameter != null && catchBlock != null) {
        PsiType type = parameter.getType();
        if (type instanceof PsiClassType || type instanceof PsiDisjunctionType) {
          myCatchStack.push(new CatchDescriptor(parameter, catchBlock));
          catchesPushCount++;
          continue;
        }
      }
      throw new CannotAnalyzeException();
    }

    int endOffset = finallyBlock == null ? getEndOffset(statement) : getStartOffset(finallyBlock) - 2;

    if (resourceList != null) {
      resourceList.accept(this);
    }

    if (tryBlock != null) {
      tryBlock.accept(this);
    }

    for (int i = 0; i < catchesPushCount; i++) {
      myCatchStack.pop();
    }

    addInstruction(new GotoInstruction(endOffset));

    for (PsiCatchSection section : sections) {
      section.accept(this);
      addInstruction(new GotoInstruction(endOffset));
    }

    if (finallyBlock != null) {
      myCatchStack.pop();
      addInstruction(new GosubInstruction(getStartOffset(finallyBlock)));
      addInstruction(new GotoInstruction(getEndOffset(statement)));
      finallyBlock.accept(this);
      addInstruction(new ReturnFromSubInstruction());
    }

    finishElement(statement);
  }

  @Override
  public void visitCatchSection(PsiCatchSection section) {
    PsiCodeBlock catchBlock = section.getCatchBlock();
    if (catchBlock != null) {
      catchBlock.accept(this);
    }
  }

  @Override
  public void visitResourceList(PsiResourceList resourceList) {
    for (PsiResourceVariable variable : resourceList.getResourceVariables()) {
      PsiExpression initializer = variable.getInitializer();
      if (initializer != null) {
        initializeVariable(variable, initializer);
      }
      PsiMethod closer = PsiUtil.getResourceCloserMethod(variable);
      if (closer != null) {
        addMethodThrows(closer);
      }
    }
  }

  @Override public void visitWhileStatement(PsiWhileStatement statement) {
    startElement(statement);

    PsiExpression condition = statement.getCondition();

    if (condition != null) {
      condition.accept(this);
      generateBoxingUnboxingInstructionFor(condition, PsiType.BOOLEAN);
      addInstruction(new ConditionalGotoInstruction(getEndOffset(statement), true, condition));
    }

    PsiStatement body = statement.getBody();
    if (body != null) {
      body.accept(this);
    }

    if (condition != null) {
      addInstruction(new GotoInstruction(getStartOffset(statement)));
    }

    finishElement(statement);
  }

  @Override public void visitExpressionList(PsiExpressionList list) {
    startElement(list);

    PsiExpression[] expressions = list.getExpressions();
    for (PsiExpression expression : expressions) {
      expression.accept(this);
    }

    finishElement(list);
  }

  @Override public void visitExpression(PsiExpression expression) {
    startElement(expression);
    DfaValue dfaValue = myFactory.createValue(expression);
    addInstruction(new PushInstruction(dfaValue, expression));
    finishElement(expression);
  }

  @Override
  public void visitArrayAccessExpression(PsiArrayAccessExpression expression) {
    startElement(expression);
    PsiExpression arrayExpression = expression.getArrayExpression();
    arrayExpression.accept(this);
    addInstruction(new FieldReferenceInstruction(expression, null));

    PsiExpression indexExpression = expression.getIndexExpression();
    if (indexExpression != null) {
      indexExpression.accept(this);
      generateBoxingUnboxingInstructionFor(indexExpression, PsiType.INT);
      addInstruction(new PopInstruction());
    }

    pushTypeOrUnknown(arrayExpression);
    finishElement(expression);
  }

  @Override
  public void visitArrayInitializerExpression(PsiArrayInitializerExpression expression) {
    startElement(expression);
    PsiType type = expression.getType();
    PsiExpression[] initializers = expression.getInitializers();
    for (PsiExpression initializer : initializers) {
      initializer.accept(this);
      if (type instanceof PsiArrayType) {
        generateBoxingUnboxingInstructionFor(initializer, ((PsiArrayType)type).getComponentType());
      }
      addInstruction(new PopInstruction());
    }
    pushUnknown();
    finishElement(expression);
  }

  @Override
  public void visitPolyadicExpression(PsiPolyadicExpression expression) {
    startElement(expression);

    try {
      DfaValue dfaValue = myFactory.createValue(expression);
      if (dfaValue != null) {
        addInstruction(new PushInstruction(dfaValue, expression));
        return;
      }
      IElementType op = expression.getOperationTokenType();

      PsiExpression[] operands = expression.getOperands();
      if (operands.length <= 1) {
        pushUnknown();
        return;
      }
      PsiType type = expression.getType();
      if (op == JavaTokenType.ANDAND) {
        generateAndExpression(operands, type);
      }
      else if (op == JavaTokenType.OROR) {
        generateOrExpression(operands, type);
      }
      else if (op == JavaTokenType.XOR && PsiType.BOOLEAN.equals(type)) {
        generateXorExpression(expression, operands, type);
      }
      else {
        generateOther(expression, op, operands, type);
      }
    }
    finally {
      finishElement(expression);
    }
  }

  private void generateOther(PsiPolyadicExpression expression, IElementType op, PsiExpression[] operands, PsiType type) {
    op = substituteBinaryOperation(op, type);

    PsiExpression lExpr = operands[0];
    lExpr.accept(this);
    PsiType lType = lExpr.getType();

    for (int i = 1; i < operands.length; i++) {
      PsiExpression rExpr = operands[i];
      PsiType rType = rExpr.getType();

      acceptBinaryRightOperand(op, type, lExpr, lType, rExpr, rType);
      addInstruction(new BinopInstruction(op, expression.isPhysical() ? expression : null, expression.getProject()));

      lExpr = rExpr;
      lType = rType;
    }
  }

  @Nullable
  private static IElementType substituteBinaryOperation(IElementType op, PsiType type) {
    if (JavaTokenType.PLUS == op && (type == null || !type.equalsToText(JAVA_LANG_STRING))) {
      return null;
    }
    return op;
  }

  private void acceptBinaryRightOperand(@Nullable IElementType op, PsiType type,
                                        PsiExpression lExpr, PsiType lType,
                                        PsiExpression rExpr, PsiType rType) {
    boolean comparing = op == JavaTokenType.EQEQ || op == JavaTokenType.NE;
    boolean comparingRef = comparing
                           && !TypeConversionUtil.isPrimitiveAndNotNull(lType)
                           && !TypeConversionUtil.isPrimitiveAndNotNull(rType);

    boolean comparingPrimitiveNumeric = comparing &&
                                        TypeConversionUtil.isPrimitiveAndNotNull(lType) &&
                                        TypeConversionUtil.isPrimitiveAndNotNull(rType) &&
                                        TypeConversionUtil.isNumericType(lType) &&
                                        TypeConversionUtil.isNumericType(rType);

    PsiType castType = comparingPrimitiveNumeric ? TypeConversionUtil.isFloatOrDoubleType(lType) ? PsiType.DOUBLE : PsiType.LONG : type;

    if (!comparingRef) {
      generateBoxingUnboxingInstructionFor(lExpr,castType);
    }

    rExpr.accept(this);
    if (!comparingRef) {
      generateBoxingUnboxingInstructionFor(rExpr, castType);
    }
  }

  private void generateBoxingUnboxingInstructionFor(PsiExpression expression, PsiType expectedType) {
    PsiType exprType = expression.getType();

    if (TypeConversionUtil.isPrimitiveAndNotNull(expectedType) && TypeConversionUtil.isPrimitiveWrapper(exprType)) {
      addInstruction(new MethodCallInstruction(expression, MethodCallInstruction.MethodType.UNBOXING, expectedType));
    }
    else if (TypeConversionUtil.isAssignableFromPrimitiveWrapper(expectedType) && TypeConversionUtil.isPrimitiveAndNotNull(exprType)) {
      addInstruction(new MethodCallInstruction(expression, MethodCallInstruction.MethodType.BOXING, expectedType));
    }
    else if (exprType != expectedType &&
             TypeConversionUtil.isPrimitiveAndNotNull(exprType) &&
             TypeConversionUtil.isPrimitiveAndNotNull(expectedType) &&
             TypeConversionUtil.isNumericType(exprType) &&
             TypeConversionUtil.isNumericType(expectedType)) {
      addInstruction(new MethodCallInstruction(expression, MethodCallInstruction.MethodType.CAST, expectedType) {
        @Override
        public DfaInstructionState[] accept(DataFlowRunner runner, DfaMemoryState stateBefore, InstructionVisitor visitor) {
          return visitor.visitCast(this, runner, stateBefore);
        }
      });
    }
  }

  private void generateXorExpression(PsiExpression expression, PsiExpression[] operands, final PsiType exprType) {
    PsiExpression operand = operands[0];
    operand.accept(this);
    generateBoxingUnboxingInstructionFor(operand, exprType);
    for (int i = 1; i < operands.length; i++) {
      operand = operands[i];
      operand.accept(this);
      generateBoxingUnboxingInstructionFor(operand, exprType);
      PsiElement psiAnchor = expression.isPhysical() ? expression : null;
      addInstruction(new BinopInstruction(JavaTokenType.NE, psiAnchor, expression.getProject()));
    }
  }

  private void generateOrExpression(PsiExpression[] operands, final PsiType exprType) {
    for (int i = 0; i < operands.length; i++) {
      PsiExpression operand = operands[i];
      operand.accept(this);
      generateBoxingUnboxingInstructionFor(operand, exprType);
      PsiExpression nextOperand = i == operands.length - 1 ? null : operands[i + 1];
      if (nextOperand != null) {
        addInstruction(new ConditionalGotoInstruction(getStartOffset(nextOperand), true, operand));
        addInstruction(new PushInstruction(myFactory.getConstFactory().getTrue(), null));
        addInstruction(new GotoInstruction(getEndOffset(operands[operands.length - 1])));
      }
    }
  }

  private void generateNonLazyExpression(boolean and, PsiExpression lExpression, PsiExpression rExpression, PsiType exprType) {
    rExpression.accept(this);
    generateBoxingUnboxingInstructionFor(rExpression, exprType);

    lExpression.accept(this);
    generateBoxingUnboxingInstructionFor(lExpression, exprType);

    ConditionalGotoInstruction toPopAndPushSuccess = new ConditionalGotoInstruction(-1, and, lExpression);
    addInstruction(toPopAndPushSuccess);
    GotoInstruction overPushSuccess = new GotoInstruction(-1);
    addInstruction(overPushSuccess);

    PopInstruction pop = new PopInstruction();
    addInstruction(pop);
    DfaConstValue constValue = and ? myFactory.getConstFactory().getFalse() : myFactory.getConstFactory().getTrue();
    PushInstruction pushSuccess = new PushInstruction(constValue, null);
    addInstruction(pushSuccess);

    toPopAndPushSuccess.setOffset(pop.getIndex());
    overPushSuccess.setOffset(pushSuccess.getIndex() + 1);
  }

  private void generateAndExpression(PsiExpression[] operands, final PsiType exprType) {
    List<ConditionalGotoInstruction> branchToFail = new ArrayList<ConditionalGotoInstruction>();
    for (PsiExpression operand : operands) {
      operand.accept(this);
      generateBoxingUnboxingInstructionFor(operand, exprType);

      ConditionalGotoInstruction onFail = new ConditionalGotoInstruction(-1, true, operand);
      branchToFail.add(onFail);
      addInstruction(onFail);
    }

    addInstruction(new PushInstruction(myFactory.getConstFactory().getTrue(), null));
    GotoInstruction toSuccess = new GotoInstruction(-1);
    addInstruction(toSuccess);
    PushInstruction pushFalse = new PushInstruction(myFactory.getConstFactory().getFalse(), null);
    addInstruction(pushFalse);
    for (ConditionalGotoInstruction toFail : branchToFail) {
      toFail.setOffset(pushFalse.getIndex());
    }
    toSuccess.setOffset(pushFalse.getIndex()+1);

  }

  @Override public void visitClassObjectAccessExpression(PsiClassObjectAccessExpression expression) {
    startElement(expression);
    PsiElement[] children = expression.getChildren();
    for (PsiElement child : children) {
      child.accept(this);
    }
    pushUnknown();
    finishElement(expression);
  }

  @Override public void visitConditionalExpression(PsiConditionalExpression expression) {
    startElement(expression);

    PsiExpression condition = expression.getCondition();

    PsiExpression thenExpression = expression.getThenExpression();
    PsiExpression elseExpression = expression.getElseExpression();

    final int elseOffset = elseExpression == null ? getEndOffset(expression) - 1 : getStartOffset(elseExpression);
    if (thenExpression != null) {
      condition.accept(this);
      generateBoxingUnboxingInstructionFor(condition, PsiType.BOOLEAN);
      PsiType type = expression.getType();
      addInstruction(new ConditionalGotoInstruction(elseOffset, true, condition));
      thenExpression.accept(this);
      generateBoxingUnboxingInstructionFor(thenExpression,type);

      addInstruction(new GotoInstruction(getEndOffset(expression)));

      if (elseExpression != null) {
        elseExpression.accept(this);
        generateBoxingUnboxingInstructionFor(elseExpression,type);
      }
      else {
        pushUnknown();
      }
    }
    else {
      pushUnknown();
    }

    finishElement(expression);
  }

  private void pushUnknown() {
    addInstruction(new PushInstruction(DfaUnknownValue.getInstance(), null));
  }

  @Override public void visitInstanceOfExpression(PsiInstanceOfExpression expression) {
    startElement(expression);
    PsiExpression operand = expression.getOperand();
    PsiTypeElement checkType = expression.getCheckType();
    if (checkType != null) {
      operand.accept(this);
      PsiType type = checkType.getType();
      if (type instanceof PsiClassType) {
        type = ((PsiClassType)type).rawType();
      }
      addInstruction(new PushInstruction(myFactory.getTypeFactory().create(type), null));
      addInstruction(new InstanceofInstruction(expression, expression.getProject(), operand, type));
    }
    else {
      pushUnknown();
    }

    finishElement(expression);
  }

  private void addMethodThrows(PsiMethod method) {
    if (method != null) {
      PsiClassType[] refs = method.getThrowsList().getReferencedTypes();
      for (PsiClassType ref : refs) {
        pushUnknown();
        ConditionalGotoInstruction cond = new ConditionalGotoInstruction(NOT_FOUND, false, null);
        addInstruction(cond);
        addInstruction(new EmptyStackInstruction());
        addInstruction(new PushInstruction(myFactory.getNotNullFactory().create(ref), null));
        addThrowCode(ref);
        cond.setOffset(myCurrentFlow.getInstructionCount());
      }
    }
  }

  @Override public void visitMethodCallExpression(PsiMethodCallExpression expression) {
    try {
      startElement(expression);

      if (handleContracts(expression, getCallContracts(expression))) {
        return;
      }

      PsiReferenceExpression methodExpression = expression.getMethodExpression();
      PsiExpression qualifierExpression = methodExpression.getQualifierExpression();

      if (qualifierExpression != null) {
        qualifierExpression.accept(this);
      }
      else {
        pushUnknown();
      }

      PsiExpression[] expressions = expression.getArgumentList().getExpressions();
      PsiElement method = methodExpression.resolve();
      PsiParameter[] parameters = method instanceof PsiMethod ? ((PsiMethod)method).getParameterList().getParameters() : null;
      for (int i = 0; i < expressions.length; i++) {
        PsiExpression paramExpr = expressions[i];
        paramExpr.accept(this);
        if (parameters != null && i < parameters.length) {
          generateBoxingUnboxingInstructionFor(paramExpr, parameters[i].getType());
        }
      }

      addInstruction(new MethodCallInstruction(expression, createChainedVariableValue(expression)));

      if (!myCatchStack.isEmpty()) {
        addMethodThrows(expression.resolveMethod());
      }

      if (expressions.length == 1 && method instanceof PsiMethod &&
          "equals".equals(((PsiMethod)method).getName()) && parameters.length == 1 &&
          parameters[0].getType().equalsToText(JAVA_LANG_OBJECT) &&
          PsiType.BOOLEAN.equals(((PsiMethod)method).getReturnType())) {
        addInstruction(new PushInstruction(myFactory.getConstFactory().getFalse(), null));
        addInstruction(new SwapInstruction());
        addInstruction(new ConditionalGotoInstruction(getEndOffset(expression), true, null));

        addInstruction(new PopInstruction());
        addInstruction(new PushInstruction(myFactory.getConstFactory().getTrue(), null));

        expressions[0].accept(this);
        addInstruction(new ApplyNotNullInstruction(expression));
      }
    }
    finally {
      finishElement(expression);
    }
  }

  private boolean handleContracts(PsiMethodCallExpression expression, List<MethodContract> _contracts) {
    if (_contracts.isEmpty()) {
      return false;
    }

    final PsiExpression[] args = expression.getArgumentList().getExpressions();
    List<MethodContract> contracts = ContainerUtil.findAll(_contracts, new Condition<MethodContract>() {
      @Override
      public boolean value(MethodContract contract) {
        return args.length == contract.arguments.length;
      }
    });
    if (contracts.isEmpty()) {
      return false;
    }

    for (PsiExpression arg : args) {
      arg.accept(this);
    }

    if (contracts.size() > 1) {
      addInstruction(new DupInstruction(args.length, contracts.size() - 1));
    }
    for (MethodContract contract : contracts) {
      handleContract(expression, contract);
    }
    pushUnknown(); // goto here if all contracts are false
    return true;
  }

  private void handleContract(PsiMethodCallExpression expression, MethodContract contract) {
    PsiExpression[] args = expression.getArgumentList().getExpressions();

    final int exitPoint = getEndOffset(expression);

    List<ConditionalGotoInstruction> gotoContractFalse = new SmartList<ConditionalGotoInstruction>();
    for (int i = args.length - 1; i >= 0; i--) {
      ValueConstraint arg = contract.arguments[i];
      if (arg == ValueConstraint.NULL_VALUE || arg == ValueConstraint.NOT_NULL_VALUE) {
        addInstruction(new PushInstruction(myFactory.getConstFactory().getNull(), null));
        addInstruction(new BinopInstruction(JavaTokenType.EQEQ, null, expression.getProject()));
      }
      else if (arg != ValueConstraint.TRUE_VALUE && arg != ValueConstraint.FALSE_VALUE) {
        addInstruction(new PopInstruction());
        continue;
      }

      boolean expectingTrueOnStack = arg == ValueConstraint.NULL_VALUE || arg == ValueConstraint.TRUE_VALUE;
      ConditionalGotoInstruction condGoto = new ConditionalGotoInstruction(-1, expectingTrueOnStack, null);
      gotoContractFalse.add(condGoto);
      addInstruction(condGoto);
    }

    // if contract is true
    switch (contract.returnValue) {
      case ANY_VALUE:
        pushUnknown();
        addInstruction(new GotoInstruction(exitPoint));
        break;
      case NULL_VALUE:
        addInstruction(new PushInstruction(myFactory.getConstFactory().getNull(), null));
        addInstruction(new GotoInstruction(exitPoint));
        break;
      case NOT_NULL_VALUE:
        PsiType type = expression.getType();
        Nullness nullability = DfaPsiUtil.getElementNullability(type, expression.resolveMethod());
        addInstruction(new PushInstruction(myFactory.createTypeValueWithNullability(type, nullability), null));
        addInstruction(new GotoInstruction(exitPoint));
        break;
      case TRUE_VALUE:
        addInstruction(new PushInstruction(myFactory.getConstFactory().getTrue(), null));
        addInstruction(new GotoInstruction(exitPoint));
        break;
      case FALSE_VALUE:
        addInstruction(new PushInstruction(myFactory.getConstFactory().getFalse(), null));
        addInstruction(new GotoInstruction(exitPoint));
        break;
      case THROW_EXCEPTION:
        int finallyOffset = getFinallyOffset();
        if (finallyOffset != NOT_FOUND) {
          addInstruction(new GosubInstruction(finallyOffset));
        }
        addInstruction(new ReturnInstruction());
        break;
      case SYSTEM_EXIT:
        addInstruction(new ReturnInstruction());
        break;
    }

    // if contract is false
    for (ConditionalGotoInstruction instruction : gotoContractFalse) {
      instruction.setOffset(myCurrentFlow.getInstructionCount());
    }
  }

  private static List<MethodContract> getCallContracts(PsiMethodCallExpression expression) {
    PsiMethod resolved = expression.resolveMethod();
    if (resolved != null) {
      final PsiAnnotation contractAnno = AnnotationUtil.findAnnotation(resolved, "org.jetbrains.annotations.Contract");
      if (contractAnno != null) {
        final Project project = expression.getProject();
        return CachedValuesManager.getManager(project).getCachedValue(contractAnno, new CachedValueProvider<List<MethodContract>>() {
          @Nullable
          @Override
          public Result<List<MethodContract>> compute() {
            PsiAnnotationMemberValue value = contractAnno.findAttributeValue(null);
            Object text = JavaPsiFacade.getInstance(project).getConstantEvaluationHelper().computeConstantExpression(value);
            if (text instanceof String) {
              try {
                return Result.create(parseContract((String)text), contractAnno);
              }
              catch (Exception ignored) {
              }
            }
            return Result.create(Collections.<MethodContract>emptyList(), contractAnno);
          }
        });
      }

      @NonNls String methodName = resolved.getName();

      PsiExpression[] params = expression.getArgumentList().getExpressions();
      PsiClass owner = resolved.getContainingClass();
      if (owner != null) {
        final String className = owner.getQualifiedName();
        if ("java.lang.System".equals(className)) {
          if ("exit".equals(methodName)) {
            return Collections.singletonList(new MethodContract(getAnyArgConstraints(params), ValueConstraint.SYSTEM_EXIT));
          }
        }
        else if ("junit.framework.Assert".equals(className) || "org.junit.Assert".equals(className) ||
                 "junit.framework.TestCase".equals(className) || "org.testng.Assert".equals(className)) {
          boolean testng = "org.testng.Assert".equals(className);
          if ("fail".equals(methodName)) {
            return Collections.singletonList(new MethodContract(getAnyArgConstraints(params), ValueConstraint.THROW_EXCEPTION));
          }

          int checkedParam = testng ? 0 : params.length - 1;
          ValueConstraint[] constraints = getAnyArgConstraints(params);
          if ("assertTrue".equals(methodName)) {
            constraints[checkedParam] = ValueConstraint.FALSE_VALUE;
            return Collections.singletonList(new MethodContract(constraints, ValueConstraint.THROW_EXCEPTION));
          }
          if ("assertFalse".equals(methodName)) {
            constraints[checkedParam] = ValueConstraint.TRUE_VALUE;
            return Collections.singletonList(new MethodContract(constraints, ValueConstraint.THROW_EXCEPTION));
          }
          if ("assertNull".equals(methodName)) {
            constraints[checkedParam] = ValueConstraint.NOT_NULL_VALUE;
            return Collections.singletonList(new MethodContract(constraints, ValueConstraint.THROW_EXCEPTION));
          }
          if ("assertNotNull".equals(methodName)) {
            constraints[checkedParam] = ValueConstraint.NULL_VALUE;
            return Collections.singletonList(new MethodContract(constraints, ValueConstraint.THROW_EXCEPTION));
          }
          return Collections.emptyList();
        }
      }

      ConditionChecker checker = ConditionCheckManager.findConditionChecker(resolved);
      if (checker != null) {
        ValueConstraint[] constraints = getAnyArgConstraints(params);
        int checkedParam = checker.getCheckedParameterIndex();
        if (checkedParam >= constraints.length) {
          return Collections.emptyList();
        }

        ConditionChecker.Type type = checker.getConditionCheckType();
        if (type == ASSERT_IS_NULL_METHOD || type == ASSERT_IS_NOT_NULL_METHOD) {
          constraints[checkedParam] = type == ASSERT_IS_NOT_NULL_METHOD ? ValueConstraint.NULL_VALUE : ValueConstraint.NOT_NULL_VALUE;
          return Collections.singletonList(new MethodContract(constraints, ValueConstraint.THROW_EXCEPTION));
        } else if (type == IS_NULL_METHOD || type == IS_NOT_NULL_METHOD) {
          constraints[checkedParam] = type == IS_NULL_METHOD ? ValueConstraint.NOT_NULL_VALUE : ValueConstraint.NULL_VALUE;
          return Collections.singletonList(new MethodContract(constraints, ValueConstraint.FALSE_VALUE));
        } else { //assertTrue or assertFalse
          constraints[checkedParam] = type == ASSERT_FALSE_METHOD ? ValueConstraint.TRUE_VALUE : ValueConstraint.FALSE_VALUE;
          return Collections.singletonList(new MethodContract(constraints, ValueConstraint.THROW_EXCEPTION));
        }
      }
    }

    return Collections.emptyList();
  }

  private static List<MethodContract> parseContract(String text) throws ParseException {
    List<MethodContract> result = ContainerUtil.newArrayList();
    for (String clause : StringUtil.replace(text, " ", "").split(";")) {
      String arrow = "->";
      int arrowIndex = clause.indexOf(arrow);
      if (arrowIndex < 0) {
        throw new ParseException("A contract clause must be in form arg1, ..., argN -> return-value");
      }

      String[] argStrings = clause.substring(0, arrowIndex).split(",");
      ValueConstraint[] args = new ValueConstraint[argStrings.length];
      for (int i = 0; i < args.length; i++) {
        args[i] = parseConstraint(argStrings[i]);
      }
      result.add(new MethodContract(args, parseConstraint(clause.substring(arrowIndex + arrow.length()))));
    }
    return result;
  }

  private static ValueConstraint parseConstraint(String name) throws ParseException {
    if (StringUtil.isEmpty(name)) throw new ParseException("Constraint should not be empty");
    if ("null".equals(name)) return ValueConstraint.NULL_VALUE;
    if ("!null".equals(name)) return ValueConstraint.NOT_NULL_VALUE;
    if ("true".equals(name)) return ValueConstraint.TRUE_VALUE;
    if ("false".equals(name)) return ValueConstraint.FALSE_VALUE;
    if ("exit".equals(name)) return ValueConstraint.SYSTEM_EXIT;
    if ("fail".equals(name)) return ValueConstraint.THROW_EXCEPTION;
    if ("_".equals(name)) return ValueConstraint.ANY_VALUE;
    throw new ParseException("Constraint should be one of: null, !null, true, false, exit, fail, _. Found: " + name);
  }

  private static class ParseException extends Exception {
    private ParseException(String message) {
      super(message);
    }
  }

  private static ValueConstraint[] getAnyArgConstraints(PsiExpression[] params) {
    ValueConstraint[] args = new ValueConstraint[params.length];
    for (int i = 0; i < args.length; i++) {
      args[i] = ValueConstraint.ANY_VALUE;
    }
    return args;
  }

  private void pushTypeOrUnknown(PsiExpression expr) {
    PsiType type = expr.getType();

    final DfaValue dfaValue;
    if (type instanceof PsiClassType) {
      dfaValue = myFactory.getTypeFactory().create(type);
    }
    else {
      dfaValue = null;
    }

    addInstruction(new PushInstruction(dfaValue, null));
  }

  @Override public void visitNewExpression(PsiNewExpression expression) {
    startElement(expression);

    pushUnknown();

    if (expression.getType() instanceof PsiArrayType) {
      final PsiExpression[] dimensions = expression.getArrayDimensions();
      for (final PsiExpression dimension : dimensions) {
        dimension.accept(this);
      }
      for (PsiExpression ignored : dimensions) {
        addInstruction(new PopInstruction());
      }
      final PsiArrayInitializerExpression arrayInitializer = expression.getArrayInitializer();
      if (arrayInitializer != null) {
        for (final PsiExpression initializer : arrayInitializer.getInitializers()) {
          initializer.accept(this);
          addInstruction(new PopInstruction());
        }
      }
      addInstruction(new MethodCallInstruction(expression, null));
    }
    else {
      final PsiExpressionList args = expression.getArgumentList();
      PsiMethod ctr = expression.resolveConstructor();
      if (args != null) {
        PsiExpression[] params = args.getExpressions();
        PsiParameter[] parameters = ctr == null ? null : ctr.getParameterList().getParameters();
        for (int i = 0; i < params.length; i++) {
          PsiExpression param = params[i];
          param.accept(this);
          if (parameters != null && i < parameters.length) {
            generateBoxingUnboxingInstructionFor(param, parameters[i].getType());
          }
        }
      }

      addInstruction(new MethodCallInstruction(expression, null));

      if (!myCatchStack.isEmpty()) {
        addMethodThrows(ctr);
      }

    }

    finishElement(expression);
  }

  @Override public void visitParenthesizedExpression(PsiParenthesizedExpression expression) {
    startElement(expression);
    PsiExpression inner = expression.getExpression();
    if (inner != null) {
      inner.accept(this);
    }
    else {
      pushUnknown();
    }
    finishElement(expression);
  }

  @Override public void visitPostfixExpression(PsiPostfixExpression expression) {
    startElement(expression);

    PsiExpression operand = expression.getOperand();
    operand.accept(this);
    generateBoxingUnboxingInstructionFor(operand, PsiType.INT);

    addInstruction(new PopInstruction());
    pushUnknown();

    if (operand instanceof PsiReferenceExpression) {
      PsiVariable psiVariable = DfaValueFactory.resolveUnqualifiedVariable((PsiReferenceExpression)expression.getOperand());
      if (psiVariable != null) {
        DfaVariableValue dfaVariable = myFactory.getVarFactory().createVariableValue(psiVariable, false);
        addInstruction(new FlushVariableInstruction(dfaVariable));
      }
    }

    finishElement(expression);
  }

  @Override public void visitPrefixExpression(PsiPrefixExpression expression) {
    startElement(expression);

    DfaValue dfaValue = myFactory.createValue(expression);
    if (dfaValue == null) {
      PsiExpression operand = expression.getOperand();

      if (operand == null) {
        pushUnknown();
      }
      else {
        operand.accept(this);
        PsiType type = expression.getType();
        PsiPrimitiveType unboxed = PsiPrimitiveType.getUnboxedType(type);
        generateBoxingUnboxingInstructionFor(operand, unboxed == null ? type : unboxed);
        if (expression.getOperationTokenType() == JavaTokenType.EXCL) {
          addInstruction(new NotInstruction());
        }
        else {
          addInstruction(new PopInstruction());
          pushUnknown();

          if (operand instanceof PsiReferenceExpression) {
            PsiVariable psiVariable = DfaValueFactory.resolveUnqualifiedVariable((PsiReferenceExpression)operand);
            if (psiVariable != null) {
              DfaVariableValue dfaVariable = myFactory.getVarFactory().createVariableValue(psiVariable, false);
              addInstruction(new FlushVariableInstruction(dfaVariable));
            }
          }
        }
      }
    }
    else {
      addInstruction(new PushInstruction(dfaValue, expression));
    }

    finishElement(expression);
  }

  @Override public void visitReferenceExpression(PsiReferenceExpression expression) {
    startElement(expression);

    final PsiExpression qualifierExpression = expression.getQualifierExpression();
    if (qualifierExpression != null) {
      qualifierExpression.accept(this);
      addInstruction(expression.resolve() instanceof PsiField ? new FieldReferenceInstruction(expression, null) : new PopInstruction());
    }

    boolean referenceRead = PsiUtil.isAccessedForReading(expression) && !PsiUtil.isAccessedForWriting(expression);
    addInstruction(new PushInstruction(getExpressionDfaValue(expression), expression, referenceRead));

    finishElement(expression);
  }

  @Nullable
  private DfaValue getExpressionDfaValue(PsiReferenceExpression expression) {
    DfaValue dfaValue = myFactory.createReferenceValue(expression);
    if (dfaValue instanceof DfaVariableValue) {
      DfaVariableValue dfaVariable = (DfaVariableValue)dfaValue;
      if (dfaVariable.getPsiVariable() instanceof PsiField) {
        myFields.add(dfaVariable);
      }
    }
    if (dfaValue == null) {
      PsiElement resolved = expression.resolve();
      if (resolved instanceof PsiField) {
        dfaValue = createDfaValueForAnotherInstanceMemberAccess(expression, (PsiField)resolved);
      }
    }
    return dfaValue;
  }

  @NotNull
  private DfaValue createDfaValueForAnotherInstanceMemberAccess(PsiReferenceExpression expression, PsiField field) {
    DfaValue dfaValue = null;
    if (expression.getQualifierExpression() != null) {
      dfaValue = createChainedVariableValue(expression);
    }
    if (dfaValue == null) {
      PsiType type = expression.getType();
      return myFactory.createTypeValueWithNullability(type, DfaPsiUtil.getElementNullability(type, field));
    }
    return dfaValue;
  }

  @Nullable
  private DfaVariableValue createChainedVariableValue(@Nullable PsiExpression expression) {
    if (expression instanceof PsiParenthesizedExpression) {
      return createChainedVariableValue(((PsiParenthesizedExpression)expression).getExpression());
    }

    PsiReferenceExpression refExpr;
    if (expression instanceof PsiMethodCallExpression) {
      refExpr = ((PsiMethodCallExpression)expression).getMethodExpression();
    }
    else if (expression instanceof PsiReferenceExpression) {
      refExpr = (PsiReferenceExpression)expression;
    }
    else {
      return null;
    }

    PsiElement target = refExpr.resolve();
    PsiVariable var = getAccessedVariable(target);
    if (var == null) {
      return null;
    }

    PsiMethod accessMethod = target instanceof PsiMethod ? (PsiMethod)target : null;
    PsiExpression qualifier = refExpr.getQualifierExpression();
    if (qualifier == null) {
      DfaVariableValue result = myFactory.getVarFactory().createVariableValue(var, refExpr.getType(), false, null, accessMethod);
      if (var instanceof PsiField) {
        myFields.add(result);
      }
      return result;
    }

    if (DfaPsiUtil.isFinalField(var) || DfaPsiUtil.isPlainMutableField(var)) {
      DfaVariableValue qualifierValue = createChainedVariableValue(qualifier);
      if (qualifierValue != null) {
        return myFactory.getVarFactory().createVariableValue(var, refExpr.getType(), false, qualifierValue, accessMethod);
      }
    }
    return null;
  }

  @Nullable
  private static PsiVariable getAccessedVariable(final PsiElement target) {
    if (target instanceof PsiVariable) {
      return (PsiVariable)target;
    }
    if (target instanceof PsiMethod) {
      PsiMethod method = (PsiMethod)target;
      if (PropertyUtil.isSimpleGetter(method)) {
        return PropertyUtil.getSimplyReturnedField(method, PropertyUtil.getSingleReturnValue(method));
      }
    }
    return null;
  }

  @Override public void visitSuperExpression(PsiSuperExpression expression) {
    startElement(expression);
    addInstruction(new PushInstruction(myFactory.getNotNullFactory().create(expression.getType()), null));
    finishElement(expression);
  }

  @Override public void visitThisExpression(PsiThisExpression expression) {
    startElement(expression);
    addInstruction(new PushInstruction(myFactory.getNotNullFactory().create(expression.getType()), null));
    finishElement(expression);
  }

  @Override public void visitLiteralExpression(PsiLiteralExpression expression) {
    startElement(expression);

    DfaValue dfaValue = myFactory.createLiteralValue(expression);
    addInstruction(new PushInstruction(dfaValue, expression));

    finishElement(expression);
  }

  @Override public void visitTypeCastExpression(PsiTypeCastExpression castExpression) {
    startElement(castExpression);
    PsiExpression operand = castExpression.getOperand();

    if (operand != null) {
      operand.accept(this);
      generateBoxingUnboxingInstructionFor(operand, castExpression.getType());
    }
    else {
      pushTypeOrUnknown(castExpression);
    }

    final PsiTypeElement typeElement = castExpression.getCastType();
    if (typeElement != null && operand != null) {
      addInstruction(new TypeCastInstruction(castExpression, operand, typeElement.getType()));
    }
    finishElement(castExpression);
  }

  @Override public void visitClass(PsiClass aClass) {
  }

}

class MethodContract {
  public final ValueConstraint[] arguments;
  public final ValueConstraint returnValue;

  public MethodContract(ValueConstraint[] arguments, ValueConstraint returnValue) {
    this.arguments = arguments;
    this.returnValue = returnValue;
  }

  public enum ValueConstraint {
    ANY_VALUE, NULL_VALUE, NOT_NULL_VALUE, TRUE_VALUE, FALSE_VALUE, THROW_EXCEPTION, SYSTEM_EXIT
  }
}


text:
ScalaJS.impls.scala_math_Numeric$LongIsIntegral$class__plus__Lscala_math_Numeric$LongIsIntegral__J__J__J = (function($$this, x, y) {
  return x.$$plus__Lscala_scalajs_runtime_Long__Lscala_scalajs_runtime_Long(y)
});
ScalaJS.impls.scala_math_Numeric$LongIsIntegral$class__minus__Lscala_math_Numeric$LongIsIntegral__J__J__J = (function($$this, x, y) {
  return x.$$minus__Lscala_scalajs_runtime_Long__Lscala_scalajs_runtime_Long(y)
});
ScalaJS.impls.scala_math_Numeric$LongIsIntegral$class__times__Lscala_math_Numeric$LongIsIntegral__J__J__J = (function($$this, x, y) {
  return x.$$times__Lscala_scalajs_runtime_Long__Lscala_scalajs_runtime_Long(y)
});
ScalaJS.impls.scala_math_Numeric$LongIsIntegral$class__quot__Lscala_math_Numeric$LongIsIntegral__J__J__J = (function($$this, x, y) {
  return x.$$div__Lscala_scalajs_runtime_Long__Lscala_scalajs_runtime_Long(y)
});
ScalaJS.impls.scala_math_Numeric$LongIsIntegral$class__rem__Lscala_math_Numeric$LongIsIntegral__J__J__J = (function($$this, x, y) {
  return x.$$percent__Lscala_scalajs_runtime_Long__Lscala_scalajs_runtime_Long(y)
});
ScalaJS.impls.scala_math_Numeric$LongIsIntegral$class__negate__Lscala_math_Numeric$LongIsIntegral__J__J = (function($$this, x) {
  return x.unary$und$minus__Lscala_scalajs_runtime_Long()
});
ScalaJS.impls.scala_math_Numeric$LongIsIntegral$class__fromInt__Lscala_math_Numeric$LongIsIntegral__I__J = (function($$this, x) {
  return ScalaJS.modules.scala_scalajs_runtime_Long().fromInt__I__Lscala_scalajs_runtime_Long(x)
});
ScalaJS.impls.scala_math_Numeric$LongIsIntegral$class__toInt__Lscala_math_Numeric$LongIsIntegral__J__I = (function($$this, x) {
  return x.toInt__I()
});
ScalaJS.impls.scala_math_Numeric$LongIsIntegral$class__toLong__Lscala_math_Numeric$LongIsIntegral__J__J = (function($$this, x) {
  return x
});
ScalaJS.impls.scala_math_Numeric$LongIsIntegral$class__toFloat__Lscala_math_Numeric$LongIsIntegral__J__F = (function($$this, x) {
  return x.toFloat__F()
});
ScalaJS.impls.scala_math_Numeric$LongIsIntegral$class__toDouble__Lscala_math_Numeric$LongIsIntegral__J__D = (function($$this, x) {
  return x.toDouble__D()
});
ScalaJS.impls.scala_math_Numeric$LongIsIntegral$class__$init$__Lscala_math_Numeric$LongIsIntegral__V = (function($$this) {
  /*<skip>*/
});
//@ sourceMappingURL=Numeric$LongIsIntegral$class.js.map

redpajama_set_name: RedPajamaGithub


text:
import os
import sys

if __name__ == "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "sweetheart.settings")

    from django.core.management import execute_from_command_line

    execute_from_command_line(sys.argv)


text:
package nz.ubermouse.rsbot.actions

import com.epicbot.api.rs3.methods.widget.Camera
import com.epicbot.api.rs3.wrappers.Locatable

class RotateCamera(location: Locatable) extends Action {
  override def run: Boolean = {
    Camera.turnTo(location, 15)
    true
  }
}


text:
require "openssl"
require "zlib"
require "active_support/core_ext/numeric"

module ActiveRecord
  module Encryption
    # An encryptor exposes the encryption API that +ActiveRecord::Encryption::EncryptedAttributeType+
    # uses for encrypting and decrypting attribute values.
    #
    # It interacts with a +KeyProvider+ for getting the keys, and delegate to
    # +ActiveRecord::Encryption::Cipher+ the actual encryption algorithm.
    class Encryptor
      # Encrypts +clean_text+ and returns the encrypted result
      #
      # Internally, it will:
      #
      # 1. Create a new +ActiveRecord::Encryption::Message+
      # 2. Compress and encrypt +clean_text+ as the message payload
      # 3. Serialize it with +ActiveRecord::Encryption.message_serializer+ (+ActiveRecord::Encryption::SafeMarshal+
      #    by default)
      # 4. Encode the result with Base 64
      #
      # === Options
      #
      # [:key_provider]
      #   Key provider to use for the encryption operation. It will default to
      #   +ActiveRecord::Encryption.key_provider+ when not provided
      #
      # [:cipher_options]
      #   +Cipher+-specific options that will be passed to the Cipher configured in
      #   +ActiveRecord::Encryption.cipher+
      def encrypt(clear_text, key_provider: default_key_provider, cipher_options: {})
        validate_payload_type(clear_text)
        serialize_message build_encrypted_message(clear_text, key_provider: key_provider, cipher_options: cipher_options)
      end

      # Decrypts a +clean_text+ and returns the result as clean text
      #
      # === Options
      #
      # [:key_provider]
      #   Key provider to use for the encryption operation. It will default to
      #   +ActiveRecord::Encryption.key_provider+ when not provided
      #
      # [:cipher_options]
      #   +Cipher+-specific options that will be passed to the Cipher configured in
      #   +ActiveRecord::Encryption.cipher+
      def decrypt(encrypted_text, key_provider: default_key_provider, cipher_options: {})
        message = deserialize_message(encrypted_text)
        keys = key_provider.decryption_keys(message)
        raise Errors::Decryption unless keys.present?
        uncompress_if_needed(cipher.decrypt(message, key: keys.collect(&:secret), **cipher_options), message.headers.compressed)
      rescue *(ENCODING_ERRORS + DECRYPT_ERRORS)
        raise Errors::Decryption
      end

      # Returns whether the text is encrypted or not
      def encrypted?(text)
        deserialize_message(text)
        true
      rescue Errors::Encoding, *DECRYPT_ERRORS
        false
      end

      private
        DECRYPT_ERRORS = [OpenSSL::Cipher::CipherError, Errors::EncryptedContentIntegrity, Errors::Decryption]
        ENCODING_ERRORS = [EncodingError, Errors::Encoding]
        THRESHOLD_TO_JUSTIFY_COMPRESSION = 140.bytes

        def default_key_provider
          ActiveRecord::Encryption.key_provider
        end

        def validate_payload_type(clear_text)
          unless clear_text.is_a?(String)
            raise ActiveRecord::Encryption::Errors::ForbiddenClass, "The encryptor can only encrypt string values (#{clear_text.class})"
          end
        end

        def cipher
          ActiveRecord::Encryption.cipher
        end

        def build_encrypted_message(clear_text, key_provider:, cipher_options:)
          key = key_provider.encryption_key

          clear_text, was_compressed = compress_if_worth_it(clear_text)
          cipher.encrypt(clear_text, key: key.secret, **cipher_options).tap do |message|
            message.headers.add(key.public_tags)
            message.headers.compressed = true if was_compressed
          end
        end

        def serialize_message(message)
          serializer.dump(message)
        end

        def deserialize_message(message)
          raise Errors::Encoding unless message.is_a?(String)
          serializer.load message
        rescue ArgumentError, TypeError, Errors::ForbiddenClass
          raise Errors::Encoding
        end

        def serializer
          ActiveRecord::Encryption.message_serializer
        end

        # Under certain threshold, ZIP compression is actually worse that not compressing
        def compress_if_worth_it(string)
          if string.bytesize > THRESHOLD_TO_JUSTIFY_COMPRESSION
            [compress(string), true]
          else
            [string, false]
          end
        end

        def compress(data)
          Zlib::Deflate.deflate(data).tap do |compressed_data|
            compressed_data.force_encoding(data.encoding)
          end
        end

        def uncompress_if_needed(data, compressed)
          if compressed
            uncompress(data)
          else
            data
          end
        end

        def uncompress(data)
          Zlib::Inflate.inflate(data).tap do |uncompressed_data|
            uncompressed_data.force_encoding(data.encoding)
          end
        end
    end
  end
end


text:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Text.RegularExpressions;
using NUnit.Framework;
using Foundation.ObjectHydrator.Generators;
using Foundation.ObjectHydrator.Tests.POCOs;

namespace Foundation.ObjectHydrator.Tests.HydratorTests
{
    [TestFixture]
    public class Hydrator_SimpleCustomer_Tests
    {
        [Test]
        public void CanGetSingleSimpleCustomer()
        {
            var hydrator = new Hydrator<SimpleCustomer>();
            var customer = hydrator.GetSingle();

            Assert.IsTrue(!String.IsNullOrEmpty(customer.Description), "Customer Description should exist.");

            DumpSimpleCustomer(customer);
        }

        [Test]
        public void CanGetList()
        {
            var listCount = 50;
            var hydrator = new Hydrator<SimpleCustomer>();
            var customers = hydrator.GetList(listCount);

            Assert.IsTrue(customers.Count == listCount, "Customer count is wrong.");

            //DumpCustomers(customers);
        }

        [Test]
        public void CanGetDescription()
        {
            var hydrator = new Hydrator<SimpleCustomer>();
            var customer = hydrator.GetSingle();

            Assert.IsTrue(!String.IsNullOrEmpty(customer.Description), "Customer Description should exist.");

            DumpSimpleCustomer(customer);
        }

        [Test]
        public void CanConstrainIntegers()
        {
            var hydrator = new Hydrator<SimpleCustomer>()
                .WithInteger(x => x.Locations, 5, 10);

            var customer = hydrator.GetSingle();

            Assert.IsTrue(customer.Locations >= 5 && customer.Locations <= 10,
                String.Format("Customer Locations [{0}] is outside expected range.", customer.Locations));

            DumpSimpleCustomer(customer);
        }

        [Test]
        public void CanDefaultString()
        {
            var defaultValue = "Testing123";

            var hydrator = new Hydrator<SimpleCustomer>()
                .With(x => x.Description, defaultValue);

            var customer = hydrator.GetSingle();

            Assert.IsTrue(defaultValue == customer.Description, String.Format("Default value is not as expected[{0}]", defaultValue));

            DumpSimpleCustomer(customer);
        }

        [Test]
        public void CanOverrideGenerator()
        {
            var hydrator = new Hydrator<SimpleCustomer>()
                .With(x => x.FirstName, "Bob");
            var customer = hydrator.GetSingle();

            Assert.IsNotNull(customer);
            Assert.IsTrue(customer.FirstName == "Bob");
        }

        [Test]
        public void CanGetInteger()
        {
            var hydrator = new Hydrator<SimpleCustomer>();

            var customer = hydrator.GetSingle();

            Assert.IsTrue(customer.Locations >= 0, String.Format("Customer Locations is expected."));

            DumpSimpleCustomer(customer);
        }

        [Test]
        public void CanGetDouble()
        {
            var hydrator = new Hydrator<SimpleCustomer>();

            var customer = hydrator.GetSingle();

            Assert.IsTrue(customer.Revenue >= 0, String.Format("Customer Revenue is expected."));

            DumpSimpleCustomer(customer);
        }

        [Test]
        public void CanConstrainDoubleDecimalPlaces()
        {
            var decimalPlaces = 3;

            var hydrator = new Hydrator<SimpleCustomer>()
                .WithDouble(x => x.Revenue, decimalPlaces);
            var customer = hydrator.GetSingle();

            var decimalPart = customer.Revenue - (int) customer.Revenue;
            Assert.IsTrue(decimalPart >= 0, String.Format("Customer Revenue decimal part is expected."));

            DumpSimpleCustomer(customer);
        }

        [Test]
        public void CanConstrainDoubleRange()
        {
            var minimum = 15.76;
            var maximum = 76.43;

            var hydrator = new Hydrator<SimpleCustomer>()
                .WithDouble(x => x.Revenue, minimum, maximum);

            var customer = hydrator.GetSingle();
            Assert.That(customer.Revenue, Is.InRange(minimum, maximum));
            DumpSimpleCustomer(customer);
        }

        [Test]
        public void CanConstrainDoubleRangeAndDecimals()
        {
            var minimum = 15.76;
            var maximum = 76.43;
            var decimalPlaces = 3;

            var hydrator = new Hydrator<SimpleCustomer>()
                .WithDouble(x => x.Revenue, minimum, maximum, decimalPlaces);

            var customer = hydrator.GetSingle();
            var decimalPart = customer.Revenue - (int) customer.Revenue;

            Assert.That(customer.Revenue, Is.InRange(minimum, maximum));
            Assert.IsTrue(decimalPart >= 0, String.Format("Customer Revenue decimal part is expected."));

            DumpSimpleCustomer(customer);
        }

        [Test]
        public void CanDefaultInteger()
        {
            var defaultValue = 73;

            var hydrator = new Hydrator<SimpleCustomer>()
                .With(x => x.Locations, defaultValue);

            var customer = hydrator.GetSingle();

            Assert.IsTrue(defaultValue == customer.Locations, String.Format("Default value is not as expected[{0}]", defaultValue));

            DumpSimpleCustomer(customer);
        }

        [Test]
        public void CanDefaultDateTime()
        {
            var defaultValue = new DateTime(2009, 01, 01);

            var hydrator = new Hydrator<SimpleCustomer>()
                .With(x => x.IncorporatedOn, defaultValue);

            var customer = hydrator.GetSingle();

            Assert.IsTrue(defaultValue == customer.IncorporatedOn, String.Format("Default value is not as expected[{0}]", defaultValue));

            DumpSimpleCustomer(customer);
        }

        [Test]
        public void CanChainWithDefaultDescription()
        {
            var defaultValue = "Testing123";
            var minimumValue = 65;
            var maximumValue = 75;

            var hydrator = new Hydrator<SimpleCustomer>()
                .With(x => x.Description, defaultValue)
                .With(x => x.Locations, new IntegerGenerator(minimumValue, maximumValue));

            var customer = hydrator.GetSingle();

            Assert.IsTrue(defaultValue == customer.Description, String.Format("Default value is not as expected[{0}]", defaultValue));

            Assert.That(customer.Locations, Is.InRange(minimumValue, maximumValue),
                String.Format("Customer Locations [{0}] is outside expected range [{1},{2}].", customer.Locations, minimumValue,
                    maximumValue));
            DumpSimpleCustomer(customer);
        }

        [Test]
        public void CanConstrainWithDates()
        {
            var minimumValue = new DateTime(2009, 01, 01);
            var maximumValue = new DateTime(2009, 01, 10);

            var hydrator = new Hydrator<SimpleCustomer>()
                .WithDate(x => x.IncorporatedOn, minimumValue, maximumValue);

            var customer = hydrator.GetSingle();

            Assert.That(customer.IncorporatedOn, Is.InRange(minimumValue, maximumValue),
                String.Format("Customer IncorporatedOn [{0}] is outside expected range [{1}, {2}].", customer.IncorporatedOn,
                    minimumValue, maximumValue));
            DumpSimpleCustomer(customer);
        }

        [Test]
        public void CanConstrainDates()
        {
            var minimumValue = new DateTime(2009, 01, 01);
            var maximumValue = new DateTime(2009, 01, 10);

            var hydrator = new Hydrator<SimpleCustomer>()
                .With(x => x.IncorporatedOn, new DateTimeGenerator(minimumValue, maximumValue));

            var customer = hydrator.GetSingle();

            Assert.That(customer.IncorporatedOn, Is.InRange(minimumValue, maximumValue),
                String.Format("Customer IncorporatedOn [{0}] is outside expected range [{1}, {2}].", customer.IncorporatedOn,
                    minimumValue, maximumValue));
            DumpSimpleCustomer(customer);
        }

        [Test]
        public void CanGetByteArray()
        {
            var length = 10;
            var hydrator = new Hydrator<SimpleCustomer>()
                .WithByteArray(x => x.Version, length);

            var customer = hydrator.GetSingle();

            Assert.IsTrue(customer.Version.Length == length, String.Format("Customer Version Length is expected to be {0}.", length));

            DumpSimpleCustomer(customer);
        }

        [Test]
        public void CanGetCreditCardNumber()
        {
            var length = 16;

            var hydrator = new Hydrator<SimpleCustomer>()
                .WithCreditCardNumber(x => x.CreditCardNumber, length);

            var customer = hydrator.GetSingle();

            Assert.IsNotEmpty(customer.CreditCardNumber, String.Format("Credit Card Number is expected."));
            Assert.IsTrue(customer.CreditCardNumber.Length == length,
                String.Format("Credit Card Number [{0}] should be {1} long.", customer.CreditCardNumber,
                    length));

            DumpSimpleCustomer(customer);
        }

        private bool IsWebsiteAddressValid(string webaddy)
        {
            var testpattern = new Regex("((mailto\\:|(news|(ht|f)tp(s?))\\://){1}\\S+)");
            return testpattern.IsMatch(webaddy);
        }

        [Test]
        public void TestWebsiteAddress()
        {
            var hydrator = new Hydrator<SimpleCustomer>();
            var customer = hydrator.GetSingle();
            Assert.IsNotNull(customer.homepage);
            Assert.IsTrue(IsWebsiteAddressValid(customer.homepage));
        }

        public bool IsValidIPAddress(string ipaddress)
        {
            var testpattern =
                new Regex(
                    "^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$");
            return testpattern.IsMatch(ipaddress);
        }

        [Test]
        public void IPAddressTest()
        {
            var hydrator = new Hydrator<SimpleCustomer>();
            var customer = hydrator.GetSingle();
            Assert.IsNotNull(customer.ipaddress);
            Assert.IsTrue(IsValidIPAddress(customer.ipaddress));
        }

        [Test]
        public void WithIPAddressTest()
        {
            var hydrator = new Hydrator<SimpleCustomer>()
                .WithIPAddress(x => x.placeholderstring);
            var customer = hydrator.GetSingle();
            Assert.IsNotNull(customer.placeholderstring);
            Assert.IsTrue(IsValidIPAddress(customer.placeholderstring));
        }

        [Test]
        public void GenderTest()
        {
            var hydrator = new Hydrator<SimpleCustomer>();
            var customer = hydrator.GetSingle();
            Assert.IsNotNull(customer.gender);
        }

        [Test]
        public void WithGenderTest()
        {
            var hydrator = new Hydrator<SimpleCustomer>()
                .WithGender(x => x.placeholderstring);

            var customer = hydrator.GetSingle();
            Assert.IsNotNull(customer.placeholderstring);
            Assert.IsTrue(customer.placeholderstring.ToLower().Contains("male") ||
                          customer.placeholderstring.ToLower().Contains("female"));
        }

        [Test]
        public void CreditCardTypeTest()
        {
            var hydrator = new Hydrator<SimpleCustomer>();
            var customer = hydrator.GetSingle();
            Assert.IsNotNull(customer.creditcardtype);
        }

        [Test]
        public void CountryCodeTest()
        {
            var hydrator = new Hydrator<SimpleCustomer>();
            var customer = hydrator.GetSingle();
            Assert.IsNotNull(customer.Country);
            Assert.IsTrue(customer.Country.Length == 2);
        }

        private bool IsEmailAddressValid(string emailaddress)
        {
            var emailpattern =
                new Regex(
                    "^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$");
            return emailpattern.IsMatch(emailaddress);
        }

        [Test]
        public void EmailAddressTest()
        {
            var hydrator = new Hydrator<SimpleCustomer>();
            var customer = hydrator.GetSingle();
            Assert.IsNotNull(customer.EmailAddress);
            Assert.IsTrue(IsEmailAddressValid(customer.EmailAddress));
        }

        [Test]
        public void BooleanGenerator()
        {
            var hydrator = new Hydrator<SimpleCustomer>();
            var customer = hydrator.GetSingle();
            Assert.IsNotNull(customer.IsActive);
            Assert.IsInstanceOfType(typeof (bool), customer.IsActive);
            DumpSimpleCustomer(customer);
        }

        [Test]
        public void CanGetTrackingNumberByInference()
        {
            var hydrator = new Hydrator<SimpleCustomer>();
            var customer = hydrator.GetSingle();
            Assert.IsNotNull(customer.TrackingNumber);
        }

        [Test]
        public void CanGetTrackingNumberBySpecification()
        {
            var hydrator = new Hydrator<SimpleCustomer>()
                .WithTrackingNumber(x => x.TrackingNumber, "usps");
            var customer = hydrator.GetSingle();
            Assert.IsNotNull(customer.TrackingNumber);
        }

        [Test]
        public void CanGetCCV()
        {
            var hydrator = new Hydrator<SimpleCustomer>()
                .WithCCV(x => x.CCV, "visa");
            var customer = hydrator.GetSingle();
            Assert.IsNotNull(customer.CCV);
            Assert.IsTrue(customer.CCV.Length == 3);
        }

        private bool CheckPhone(string phonetocheck)
        {
            var phonepattern =
                new Regex(
                    "^[\\(]{0,1}([0-9]){3}[\\)]{0,1}[ ]?([^0-1]){1}([0-9]){2}[ ]?[-]?[ ]?([0-9]){4}[ ]*((x){0,1}([0-9]){1,5}){0,1}$");
            var boolval = phonepattern.IsMatch(phonetocheck);
            return boolval;
        }

        [Test]
        public void WithPhoneTest()
        {
            var hydrator = new Hydrator<SimpleCustomer>()
                .WithAmericanPhone(x => x.placeholderstring);
            var customer = hydrator.GetSingle();
            Assert.IsNotNull(customer.placeholderstring);
            Assert.IsTrue(CheckPhone(customer.placeholderstring));
        }

        private bool IsAmericanPostalCodeValid(string postalcode)
        {
            var postalcodepattern = new Regex("^\\d{5}$|^\\d{5}-\\d{4}$");
            return postalcodepattern.IsMatch(postalcode);
        }

        [Test]
        public void WithPostalCodeTest()
        {
            var hydrator = new Hydrator<SimpleCustomer>()
                .WithAmericanPostalCode(x => x.placeholderstring, 1);
            var customer = hydrator.GetSingle();
            Assert.IsNotNull(customer.placeholderstring);
            Assert.IsTrue(IsAmericanPostalCodeValid(customer.placeholderstring));
        }

        [Test]
        public void FromListTest()
        {
            IList<string> mylist = new List<string>() {"red", "green", "blue", "orange"};
            var hydrator = new Hydrator<SimpleCustomer>()
                .FromList(x => x.placeholderstring, mylist);
            var customer = hydrator.GetSingle();
            Assert.IsNotNull(customer.placeholderstring);
            Assert.IsTrue(mylist.Contains(customer.placeholderstring));
        }

        [Test]
        public void WithAmericanAddressTest()
        {
            var hydrator = new Hydrator<SimpleCustomer>()
                .WithAmericanAddress(x => x.placeholderstring);
            var customer = hydrator.GetSingle();
            Assert.IsNotNull(customer.placeholderstring);
        }

        [Test]
        public void WithAmericanCityTest()
        {
            var hydrator = new Hydrator<SimpleCustomer>()
                .WithAmericanCity(x => x.placeholderstring);
            var customer = hydrator.GetSingle();
            Assert.IsNotNull(customer.placeholderstring);
        }

        [Test]
        public void WithAmericanState()
        {
            var hydrator = new Hydrator<SimpleCustomer>()
                .WithAmericanState(x => x.placeholderstring);
            var customer = hydrator.GetSingle();
            Assert.IsNotNull(customer.placeholderstring);
        }

        [Test]
        public void WithCompanyName()
        {
            var hydrator = new Hydrator<SimpleCustomer>()
                .WithCompanyName(x => x.placeholderstring);
            var customer = hydrator.GetSingle();
            Assert.IsNotNull(customer.placeholderstring);
        }

        [Test]
        public void WithCreditCardType()
        {
            var hydrator = new Hydrator<SimpleCustomer>()
                .WithCreditCardType(x => x.placeholderstring);
            var customer = hydrator.GetSingle();
            Assert.IsNotNull(customer.placeholderstring);
        }

        [Test]
        public void WithEmailAddress()
        {
            var hydrator = new Hydrator<SimpleCustomer>()
                .WithEmailAddress(x => x.placeholderstring);
            var customer = hydrator.GetSingle();
            Assert.IsNotNull(customer.placeholderstring);
        }

        [Test]
        public void WithFirstName()
        {
            var hydrator = new Hydrator<SimpleCustomer>()
                .WithFirstName(x => x.placeholderstring);
            var customer = hydrator.GetSingle();
            Assert.IsNotNull(customer.placeholderstring);
        }

        [Test]
        public void WithLastName()
        {
            var hydrator = new Hydrator<SimpleCustomer>()
                .WithLastName(x => x.placeholderstring);
            var customer = hydrator.GetSingle();
            Assert.IsNotNull(customer.placeholderstring);
        }

        [Test]
        public void WithWebsite()
        {
            var hydrator = new Hydrator<SimpleCustomer>()
                .WithWebsite(x => x.placeholderstring);
            var customer = hydrator.GetSingle();
            Assert.IsNotNull(customer.placeholderstring);
        }

        [Test]
        public void PasswordWithInferenceDefaultLength()
        {
            var hydrator = new Hydrator<SimpleCustomer>();
            var customer = hydrator.GetSingle();
            Assert.IsNotNull(customer.Password);
            Assert.AreEqual(10, customer.Password.Length);
        }

        [Test]
        public void PasswordUsingWithAndDefaultLength()
        {
            var hydrator = new Hydrator<SimpleCustomer>()
                .WithPassword(x => x.placeholderstring);
            var customer = hydrator.GetSingle();
            Assert.IsNotNull(customer.placeholderstring);
            Assert.AreEqual(10, customer.placeholderstring.Length);
        }

        [Test]
        public void PasswordUsingWithAndCustomLength()
        {
            var pwlen = 15;
            var hydrator = new Hydrator<SimpleCustomer>()
                .WithPassword(x => x.placeholderstring, pwlen);
            var customer = hydrator.GetSingle();
            Assert.IsNotNull(customer.placeholderstring);
            Assert.AreEqual(pwlen, customer.placeholderstring.Length);
        }

        [Test]
        public void AlphaNumericWithLength()
        {
            var hydrator = new Hydrator<SimpleCustomer>()
                .WithAlphaNumeric(x => x.placeholderstring, 10);
            var customer = hydrator.GetSingle();
            Assert.IsNotNull(customer.placeholderstring);
            Assert.AreEqual(10, customer.placeholderstring.Length);
        }

        [Test]
        public void WithUnitedKingdomCityTest()
        {
            var hydrator = new Hydrator<SimpleCustomer>()
                .WithUnitedKingdomCity(x => x.placeholderstring);
            var customer = hydrator.GetSingle();
            Assert.IsNotNull(customer.placeholderstring);
        }

        [Test]
        public void WithUnitedKingdomCountyTest()
        {
            var hydrator = new Hydrator<SimpleCustomer>()
                .WithUnitedKingdomCounty(x => x.placeholderstring);
            var customer = hydrator.GetSingle();
            Assert.IsNotNull(customer.placeholderstring);
        }

        [Test]
        public void WithUnitedKingdomPostCodeTest()
        {
            var hydrator = new Hydrator<SimpleCustomer>()
                .WithUnitedKingdomPostCode(x => x.placeholderstring);
            var customer = hydrator.GetSingle();
            Assert.IsNotNull(customer.placeholderstring);
        }

        private void DumpCustomers(IList<SimpleCustomer> customers)
        {
            foreach (SimpleCustomer customer in customers)
            {
                DumpSimpleCustomer(customer);
            }
        }

        private void DumpSimpleCustomer(Object theObject)
        {
            Trace.WriteLine("");
            foreach (PropertyInfo propertyInfo in theObject.GetType().GetProperties())
            {
                Trace.WriteLine(String.Format("{0} [{1}]", propertyInfo.Name, propertyInfo.GetValue(theObject, null)));

                if (propertyInfo.PropertyType == typeof (byte[]))
                {
                    var theArray = propertyInfo.GetValue(theObject, null) as byte[];
                    if (theArray != null)
                    {
                        Trace.Write("  byte[] ");
                        for (var i = 0; i < theArray.Length; i++)
                        {
                            Trace.Write(String.Format("[{0}]", theArray[i]));
                        }
                        Trace.WriteLine(String.Empty);
                    }
                }
            }
        }
    }
}


text:
The Rtsp package provides the classes for implementing RTSP client applications.,
see RFC RTSP 2326 for details.


text:
{% extends 'dreams/index.html'%}

{% block content-main %}

    <form action="{% url 'add' %}" method="post">{% csrf_token %}

Title: <input type="text" name="title" value={{ dream.title }}>

Date: <input type="date" name="date", value={{ dream.date }}>

Content: <input type="text" name="content" value={{ dream.content }}>

Feedback: <input type="text" name="feedback" value={{ dream.feedback }}>

        <button type="submit" value="Submit">Save</button>

    </form>

{% endblock %}


text:
import action from "./actions"
import basicNode from "./basicNode"
import  buttonAction from "./buttonAction"
import flow from "./flow"
import galleryItemActions from "./galleryItemActions"
import galleryItemImage from "./galleryItemImage"
import galleryItemText from "./galleryItemText"
import galleryMessage from "./galleryMessage"
import getStartedNode from "./getStartedNode"
import greetingTextNode from "./greetingTextNode"
import imageMessage from "./imageMessage"
import initialNode from "./initialNode"
import inputNode from "./inputNode"
import linkAction from "./linkAction"
import messages from "./messages"
import persistentMenuNode from "./persistentMenuNode"
import plugin from "./plugin"
import quickTextAction from "./quickTextAction"
import shareAction from "./shareAction"
import textMessage from "./textMessage"

export const Schemas = {
    action: action,
    basicNode: basicNode,
    buttonAction : buttonAction,
    flow: flow,
    galleryItemActions: galleryItemActions,
    galleryItemImage: galleryItemImage,
    galleryItemText: galleryItemText,
    galleryMessage: galleryMessage,
    getStartedNode: getStartedNode,
    greetingTextNode: greetingTextNode,
    imageMessage: imageMessage,
    initialNode: initialNode,
    inputNode: inputNode,
    linkAction: linkAction,
    messages: messages,
    persistentMenuNode: persistentMenuNode,
    plugin: plugin,
    quickTextAction: quickTextAction,
    shareAction: shareAction,
    textMessage: textMessage
}


text:
"""
#  Licensed to the Apache Software Foundation (ASF) under one   *
#  or more contributor license agreements.  See the NOTICE file *
#  distributed with this work for additional information        *
#  regarding copyright ownership.  The ASF licenses this file   *
#  to you 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.
"""
from __future__ import absolute_import
from .Pool import *
from ...util.TodoManager import *
from ...util.Todo import *

__all__ = ['QueuedPool']

class _Todo(Todo):

    def __init__(self, runnable):
        super(_Todo,self).__init__()
        self.__runnable = runnable

    def doit(self, mgr):
        self.__runnable.run()

    def exception(self, mgr, e):
        self.__runnable.exception(e)

class QueuedPool(Pool):
    """
    A queued implementation of pool
    """

    def __init__(self, mgr=None):
        """
        Constructs the queued pool with a specified todo manager.

        @param mgr - if mgr is not specified or None, use the default manager
        @raises Exception
        """
        if mgr == None:
            mgr = TodoManager.getTodoManager()

        self.__mgr = mgr

    def run(self, runnable):
        self.__mgr.add(_Todo(runnable))