1 16 17 package org.springframework.core; 18 19 import java.io.PrintWriter ; 20 import java.io.StringWriter ; 21 22 import org.springframework.util.Assert; 23 24 35 public abstract class ControlFlowFactory { 36 37 41 public static ControlFlow createControlFlow() { 42 return JdkVersion.isAtLeastJava14() ? 43 (ControlFlow) new Jdk14ControlFlow() : 44 (ControlFlow) new Jdk13ControlFlow(); 45 } 46 47 48 56 static class Jdk14ControlFlow implements ControlFlow { 57 58 private StackTraceElement [] stack; 59 60 public Jdk14ControlFlow() { 61 this.stack = new Throwable ().getStackTrace(); 62 } 63 64 67 public boolean under(Class clazz) { 68 Assert.notNull(clazz, "Class must not be null"); 69 String className = clazz.getName(); 70 for (int i = 0; i < stack.length; i++) { 71 if (this.stack[i].getClassName().equals(className)) { 72 return true; 73 } 74 } 75 return false; 76 } 77 78 82 public boolean under(Class clazz, String methodName) { 83 Assert.notNull(clazz, "Class must not be null"); 84 Assert.notNull(methodName, "Method name must not be null"); 85 String className = clazz.getName(); 86 for (int i = 0; i < this.stack.length; i++) { 87 if (this.stack[i].getClassName().equals(className) && 88 this.stack[i].getMethodName().equals(methodName)) { 89 return true; 90 } 91 } 92 return false; 93 } 94 95 99 public boolean underToken(String token) { 100 if (token == null) { 101 return false; 102 } 103 StringWriter sw = new StringWriter (); 104 new Throwable ().printStackTrace(new PrintWriter (sw)); 105 String stackTrace = sw.toString(); 106 return stackTrace.indexOf(token) != -1; 107 } 108 109 public String toString() { 110 StringBuffer sb = new StringBuffer ("Jdk14ControlFlow: "); 111 for (int i = 0; i < this.stack.length; i++) { 112 if (i > 0) { 113 sb.append("\n\t@"); 114 } 115 sb.append(this.stack[i]); 116 } 117 return sb.toString(); 118 } 119 } 120 121 122 129 static class Jdk13ControlFlow implements ControlFlow { 130 131 private final String stackTrace; 132 133 private final int stackTraceLength; 134 135 public Jdk13ControlFlow() { 136 StringWriter sw = new StringWriter (); 137 new Throwable ().printStackTrace(new PrintWriter (sw)); 138 this.stackTrace = sw.toString(); 139 this.stackTraceLength = this.stackTrace.length(); 140 } 141 142 145 public boolean under(Class clazz) { 146 Assert.notNull(clazz, "Class must not be null"); 147 return this.stackTrace.indexOf(clazz.getName()) != -1; 148 } 149 150 159 public boolean under(Class clazz, String methodName) { 160 Assert.notNull(clazz, "Class must not be null"); 161 Assert.notNull(methodName, "Method name must not be null"); 162 String searchPattern = clazz.getName() + "." + methodName; 163 int patternLength = searchPattern.length(); 164 int index = 0; 165 do { 166 index = this.stackTrace.indexOf(searchPattern, index); 167 if (index != -1) { 168 int endIndex = index + patternLength; 169 if (endIndex == this.stackTraceLength) { 170 return true; 171 } 172 char afterPattern = this.stackTrace.charAt(endIndex); 173 if (afterPattern == '(' || Character.isWhitespace(afterPattern)) { 174 return true; 175 } 176 index = endIndex; 177 } 178 } 179 while (index != -1); 180 return false; 181 } 182 183 187 public boolean underToken(String token) { 188 return (token != null && this.stackTrace.indexOf(token) != -1); 189 } 190 } 191 192 } 193 | Popular Tags |