/testability-explorer/src/main/java/com/google/test/metric/method/Block.java

http://testability-explorer.googlecode.com/ · Java · 101 lines · 71 code · 15 blank · 15 comment · 9 complexity · b9fe6d3208726792e5e5ed4de56c5f82 MD5 · raw file

  1. /*
  2. * Copyright 2007 Google Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.google.test.metric.method;
  17. import static java.util.Collections.unmodifiableList;
  18. import java.util.ArrayList;
  19. import java.util.List;
  20. import com.google.test.metric.method.op.stack.Load;
  21. import com.google.test.metric.method.op.stack.StackOperation;
  22. public class Block {
  23. private final String id;
  24. private final List<Block> previousBlocks = new ArrayList<Block>();
  25. private final List<StackOperation> operations = new ArrayList<StackOperation>();
  26. private final List<Block> nextBlocks = new ArrayList<Block>();
  27. private final boolean isTerminal = false;
  28. private Constant exception;
  29. public Block(String id) {
  30. this.id = id;
  31. }
  32. public void addNextBlock(Block nextBlock) {
  33. if (!nextBlocks.contains(nextBlock) && nextBlock != this) {
  34. nextBlocks.add(nextBlock);
  35. nextBlock.previousBlocks.add(this);
  36. }
  37. }
  38. public void addOp(StackOperation operation) {
  39. operations.add(operation);
  40. }
  41. public List<StackOperation> getOperations() {
  42. return operations;
  43. }
  44. @Override
  45. public String toString() {
  46. StringBuilder buf = new StringBuilder();
  47. buf.append(exception == null ? "Block" : "ExceptionHandlerBlock");
  48. buf.append("[");
  49. buf.append(id);
  50. String sep = " -> ";
  51. for (Block next : nextBlocks) {
  52. buf.append(sep);
  53. buf.append(next.id);
  54. sep = ", ";
  55. }
  56. sep = " <- ";
  57. for (Block next : previousBlocks) {
  58. buf.append(sep);
  59. buf.append(next.id);
  60. sep = ", ";
  61. }
  62. buf.append("]{\n");
  63. for (StackOperation operation : operations) {
  64. buf.append(" ");
  65. buf.append(operation);
  66. buf.append("\n");
  67. }
  68. buf.append("}");
  69. return buf.toString();
  70. }
  71. public List<Block> getNextBlocks() {
  72. return unmodifiableList(nextBlocks);
  73. }
  74. public boolean isTerminal() {
  75. return isTerminal;
  76. }
  77. public String getId() {
  78. return id;
  79. }
  80. public void setExceptionHandler(int lineNumber, Constant exception) {
  81. if (this.exception == null) {
  82. operations.add(0, new Load(lineNumber, exception));
  83. }
  84. this.exception = exception;
  85. }
  86. }