private static Optional<Declaration> searchLocalVariable( final Source source, final Integer line, final Integer col, final String symbol) { final EntryMessage entryMessage = log.traceEntry("line={} col={} symbol={}", line, col, symbol); final Optional<Variable> variable = source.getVariable(line, col); final Optional<Declaration> result = variable .map( var -> { final Declaration declaration = new Declaration(symbol, var.fqcn, Declaration.Type.VAR, var.argumentIndex); return Optional.of(declaration); }) .orElseGet(() -> searchFieldVar(source, line, symbol)); log.traceExit(entryMessage); return result; }
private static Optional<Declaration> searchReserved( @SuppressWarnings("unused") final Source source, final Integer line, final Integer col, final String symbol) { final EntryMessage entryMessage = log.traceEntry("line={} col={} symbol={}", line, col, symbol); Optional<Declaration> result = Optional.empty(); if (symbol.equals("package") || symbol.equals("import") || symbol.equals("new") || symbol.equals("try") || symbol.equals("throw") || symbol.equals("finally") || symbol.equals("public") || symbol.equals("private") || symbol.equals("protected") || symbol.equals("return") || symbol.equals("static") || symbol.equals("final")) { result = Optional.of(new Declaration(symbol, "", Declaration.Type.OTHER, 0)); } log.traceExit(entryMessage); return result; }
private static Optional<Declaration> searchField( final Source source, final Integer line, final Integer col, final String symbol) { final EntryMessage entryMessage = log.traceEntry("line={} col={} symbol={}", line, col, symbol); final Optional<Declaration> result = source .searchFieldAccess(line, col, symbol) .map( fa -> { String scope = fa.scope; if (scope != null && !scope.isEmpty()) { scope = scope + '.' + symbol; } else { scope = symbol; } return new Declaration( scope.trim(), fa.returnType, Declaration.Type.FIELD, fa.argumentIndex); }); log.traceExit(entryMessage); return result; }
@Override public void dumpVariable() { final EntryMessage entryMessage = log.traceEntry("**** {} {} methods:{}", this.getScopeType(), this.name, this.scopes.size()); super.dumpVariable(log); for (final ExpressionScope expressionScope : this.expressions) { expressionScope.dumpVariable(); } for (final BlockScope blockScope : this.scopes) { blockScope.dumpVariable(); } for (final ClassScope cs : this.classScopes) { cs.dumpVariable(); } log.traceExit(entryMessage); }
@Override public void dumpFieldAccess() { final EntryMessage entryMessage = log.traceEntry("**** {} {} methods:{}", this.getScopeType(), this.name, this.scopes.size()); super.dumpFieldAccess(log); for (final ExpressionScope expressionScope : this.expressions) { expressionScope.dumpFieldAccess(); } for (final BlockScope blockScope : this.scopes) { blockScope.dumpFieldAccess(); } for (final ClassScope cs : this.classScopes) { cs.dumpFieldAccess(); } log.traceExit(entryMessage); }
@Override public void dump() { final EntryMessage entryMessage = log.traceEntry("**** {} {} methods:{}", this.getScopeType(), this.name, this.scopes.size()); super.dump(log); for (final ExpressionScope expressionScope : this.expressions) { expressionScope.dump(); } for (final BlockScope blockScope : this.scopes) { blockScope.dump(); } for (final ClassScope cs : this.classScopes) { cs.dump(); } log.traceExit(entryMessage); }
@Override public void dumpVariable() { final EntryMessage entryMessage = log.traceEntry("**** {} {} return {}", this.getScopeType(), this.name, this.returnType); super.dumpVariable(log); for (final ExpressionScope expressionScope : this.expressions) { expressionScope.dumpVariable(); } for (final BlockScope blockScope : this.scopes) { blockScope.dumpVariable(); } log.traceExit(entryMessage); }
@Override public void dumpFieldAccess() { final EntryMessage entryMessage = log.traceEntry("**** {} {} return {}", this.getScopeType(), this.name, this.returnType); super.dumpFieldAccess(log); for (final ExpressionScope expressionScope : this.expressions) { expressionScope.dumpFieldAccess(); } for (final BlockScope blockScope : this.scopes) { blockScope.dumpFieldAccess(); } log.traceExit(entryMessage); }
@Override public SignatureVisitor visitTypeArgument(char c) { final EntryMessage message = log.traceEntry("params={} current={} c={}", this.parameterTypes, this.current, c); final MethodSignatureVisitor visitor = new MethodSignatureVisitor(this.name, this); visitor.hasTypes = true; visitor.isParameter = this.isParameter; visitor.isReturn = this.isReturn; visitor.current = this.current; switch (c) { case SignatureVisitor.INSTANCEOF: break; case SignatureVisitor.SUPER: visitor.isSuper = true; break; case SignatureVisitor.EXTENDS: visitor.isExtends = true; break; } log.traceExit(message); return visitor; }
@Override public void visitBaseType(char c) { final String baseType = ASMReflector.toPrimitive(c); final EntryMessage message = log.traceEntry("baseType={} parameterTypes={} c={}", baseType, this.parameterTypes, c); TypeInfo typeInfo = new TypeInfo(baseType, baseType); if (this.parent != null && this.isReturn) { // set return type this.parent.returnType = typeInfo; log.traceExit(message); return; } if (this.current == null) { this.current = typeInfo; this.visitEnd(); } log.traceExit(message); }
@Override public void visitBaseType(final char c) { final String primitive = ASMReflector.toPrimitive(c); final TypeInfo typeInfo = new TypeInfo(primitive, primitive); if (this.typeInfo == null) { this.typeInfo = typeInfo; if (this.holdArray) { this.typeInfo.isArray = true; this.holdArray = false; } } if (this.currentType.size() == 0) { // set main this.currentType.push(typeInfo); } final EntryMessage em = log.traceEntry( "c={} name={} typeInfo={} currentType={}", c, this.name, this.typeInfo, this.currentType); log.traceExit(em); }
FieldAnalyzeVisitor parseSignature() { final EntryMessage m = log.traceEntry("fieldSignature={}", fieldSignature); boolean isStatic = (Opcodes.ACC_STATIC & this.access) > 0; SignatureReader signatureReader = new SignatureReader(this.fieldSignature); FieldSignatureVisitor visitor; if (isStatic) { visitor = new FieldSignatureVisitor(this.name, new ArrayList<>(4)); } else { visitor = new FieldSignatureVisitor(this.name, this.classAnalyzeVisitor.classTypeParameters); } if (this.typeMap != null) { visitor.setTypeMap(this.typeMap); } this.fieldSignatureVisitor = visitor; signatureReader.acceptType(fieldSignatureVisitor); return log.traceExit(m, this); }
MethodAnalyzeVisitor parseSignature() { final EntryMessage entryMessage = log.traceEntry("name={} methodSignature={}", this.name, this.methodSignature); final boolean isStatic = (Opcodes.ACC_STATIC & this.access) > 0; final SignatureReader signatureReader = new SignatureReader(this.methodSignature); MethodSignatureVisitor visitor; if (isStatic) { visitor = new MethodSignatureVisitor(this.name, new ArrayList<>(4)); } else { visitor = new MethodSignatureVisitor(this.name, this.classAnalyzeVisitor.classTypeParameters); } if (this.typeMap != null) { visitor.setTypeMap(this.typeMap); } signatureReader.accept(visitor); this.formalType = visitor.getFormalType(); this.parameterTypes = visitor.getParameterTypes(); this.typeParameters = visitor.getTypeParameters(); this.returnType = visitor.getReturnType(); log.traceExit(entryMessage); return this; }
protected EntryMessage entryMsg(final String format, final Object... params) { final int count = params == null ? 0 : params.length; if (count == 0) { if (Strings.isEmpty(format)) { return flowMessageFactory.newEntryMessage(null); } return flowMessageFactory.newEntryMessage(new SimpleMessage(format)); } if (format != null) { return flowMessageFactory.newEntryMessage(new ParameterizedMessage(format, params)); } final StringBuilder sb = new StringBuilder(); sb.append("params("); for (int i = 0; i < count; i++) { if (i > 0) { sb.append(", "); } final Object parm = params[i]; sb.append(parm instanceof Message ? ((Message) parm).getFormattedMessage() : String.valueOf(parm)); } sb.append(')'); return flowMessageFactory.newEntryMessage(new SimpleMessage(sb)); }
@Test public void testTraceEntryExit() { currentLevel = Level.TRACE; final FlowMessageFactory fact = new DefaultFlowMessageFactory(); final ParameterizedMessage paramMsg = new ParameterizedMessage("Tracy {}", "Logan"); currentEvent = new LogEvent(ENTRY_MARKER.getName(), fact.newEntryMessage(paramMsg), null); final EntryMessage entry = traceEntry("Tracy {}", "Logan"); final ReusableParameterizedMessage msg = ReusableParameterizedMessageTest.set( new ReusableParameterizedMessage(), "Tracy {}", "Logan"); ReusableParameterizedMessageTest.set(msg, "Some other message {}", 123); currentEvent = new LogEvent(null, msg, null); trace("Some other message {}", 123); // ensure original entry message not overwritten assertEquals("Tracy Logan", entry.getMessage().getFormattedMessage()); currentEvent = new LogEvent(EXIT_MARKER.getName(), fact.newExitMessage(entry), null); traceExit(entry); // ensure original entry message not overwritten assertEquals("Tracy Logan", entry.getMessage().getFormattedMessage()); }
@Test public void testTraceEntryMessage() { currentLevel = Level.TRACE; final FlowMessageFactory fact = new DefaultFlowMessageFactory(); final ParameterizedMessage paramMsg = new ParameterizedMessage("Tracy {}", "Logan"); currentEvent = new LogEvent(ENTRY_MARKER.getName(), fact.newEntryMessage(paramMsg), null); final ReusableParameterizedMessage msg = ReusableParameterizedMessageTest.set( new ReusableParameterizedMessage(), "Tracy {}", "Logan"); final EntryMessage entry = traceEntry(msg); ReusableParameterizedMessageTest.set(msg, "Some other message {}", 123); currentEvent = new LogEvent(null, msg, null); trace("Some other message {}", 123); // ensure original entry message not overwritten assertEquals("Tracy Logan", entry.getMessage().getFormattedMessage()); currentEvent = new LogEvent(EXIT_MARKER.getName(), fact.newExitMessage(entry), null); traceExit(entry); // ensure original entry message not overwritten assertEquals("Tracy Logan", entry.getMessage().getFormattedMessage()); }
@Test public void flowTracingString_SupplierOfObjectMessages() { final EntryMessage msg = logger.traceEntry("doFoo(a={}, b={})", new Supplier<Message>() { @Override public Message get() { return new ObjectMessage(1); } }, new Supplier<Message>() { @Override public Message get() { return new ObjectMessage(2); } }); logger.traceExit(msg, 3); assertEquals(2, results.size()); assertThat("Incorrect Entry", results.get(0), startsWith("ENTER[ FLOW ] TRACE Enter")); assertThat("Missing entry data", results.get(0), containsString("doFoo(a=1, b=2)")); assertThat("Incorrect Exit", results.get(1), startsWith("EXIT[ FLOW ] TRACE Exit")); assertThat("Missing exit data", results.get(1), containsString("doFoo(a=1, b=2): 3")); }
@Test public void flowTracingString_SupplierOfStrings() { final EntryMessage msg = logger.traceEntry("doFoo(a={}, b={})", new Supplier<String>() { @Override public String get() { return "1"; } }, new Supplier<String>() { @Override public String get() { return "2"; } }); logger.traceExit(msg, 3); assertEquals(2, results.size()); assertThat("Incorrect Entry", results.get(0), startsWith("ENTER[ FLOW ] TRACE Enter")); assertThat("Missing entry data", results.get(0), containsString("doFoo(a=1, b=2)")); assertThat("Incorrect Exit", results.get(1), startsWith("EXIT[ FLOW ] TRACE Exit")); assertThat("Missing exit data", results.get(1), containsString("doFoo(a=1, b=2): 3")); }
public final boolean degelateCommand(MessageReceivedEvent event, String command, String matchedText) { EntryMessage entryMessage = logger.traceEntry("CommandDelegator will delegate command {}.", matchedText); int handleCount = 0; for (@NotNull ICommandHandler handler : HANDLERS) { try { if (handler.handle(event, command, matchedText)) { handleCount++; } } catch (Exception e) { logger.error("Handler " + handler.getClass().getSimpleName() + " failed to handle command.", e); } } return logger.traceExit(entryMessage, handleCount > 0); }
private static Optional<Declaration> searchMethodCall( final Source source, final Integer line, final Integer col, final String symbol) { final EntryMessage entryMessage = log.traceEntry("line={} col={} symbol={}", line, col, symbol); final Optional<MethodCall> methodCall = source.getMethodCall(line, col, true); final Optional<Declaration> result = methodCall.map( mc -> { final String methodName = mc.name; final List<String> arguments = mc.getArguments(); final String declaringClass = mc.declaringClass; if (declaringClass == null) { return null; } final CachedASMReflector reflector = CachedASMReflector.getInstance(); final MemberDescriptor method = searchMethod(declaringClass, methodName, arguments) .orElseGet(() -> searchConstructor(declaringClass, arguments).orElse(null)); String declaration; if (method != null) { declaration = method.getDeclaration(); } else { final String args = Joiner.on(", ").join(arguments); declaration = mc.returnType + ' ' + methodName + '(' + args + ')'; } String scope = mc.scope; if (scope != null && !scope.isEmpty()) { scope = scope + '.' + symbol; } else { scope = symbol; } return new Declaration( scope.trim(), declaration, Declaration.Type.METHOD, mc.argumentIndex); }); log.traceExit(entryMessage); return result; }
public static String getChecksum(final File file) throws IOException { final EntryMessage entryMessage = log.traceEntry("file={}", file); try { final MessageDigest md = MessageDigest.getInstance(ALGORITHM_SHA_512); try (final InputStream is = Files.newInputStream(file.toPath()); DigestInputStream dis = new DigestInputStream(is, md)) { final byte[] buf = new byte[4096]; while (dis.read(buf) != -1) {} final byte[] digest = md.digest(); final StringBuilder sb = new StringBuilder(128); for (final int b : digest) { sb.append(Character.forDigit(b >> 4 & 0xF, 16)); sb.append(Character.forDigit(b & 0xF, 16)); } log.traceExit(entryMessage); return sb.toString(); } } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } }
@Override public void dump() { EntryMessage em = log.traceEntry("**** {} {}", this.getClassName(), this.range); super.dumpVariable(log); super.dumpFieldAccess(log); log.traceExit(em); }
public Optional<AccessSymbol> getExpressionReturn() { EntryMessage em = log.traceEntry("expressionReturn={}", this.expressionReturn); Optional<Variable> var = this.variables.stream().filter(Variable::isDecl).findFirst(); if (var.isPresent()) { return log.traceExit(em, Optional.empty()); } Optional<AccessSymbol> aReturn = Optional.ofNullable(this.expressionReturn); return log.traceExit(em, aReturn); }
public void dumpVariable() { final EntryMessage entryMessage = log.traceEntry("{}", Strings.repeat("*", 100)); for (final ClassScope cs : classScopes) { cs.dumpVariable(); } log.traceExit(entryMessage); }
public void dumpFieldAccess() { final EntryMessage entryMessage = log.traceEntry("{}", Strings.repeat("*", 100)); for (final ClassScope cs : classScopes) { cs.dumpFieldAccess(); } log.traceExit(entryMessage); }
public void dump() { final EntryMessage entryMessage = log.traceEntry("{}", Strings.repeat("*", 100)); for (final ClassScope cs : classScopes) { cs.dump(); } log.trace("unused={}", this.unused); log.trace("unknown={}", this.unknown); log.traceExit(entryMessage); }
private Optional<Location> searchClassOrInterface( final Source source, final int line, final int col, String symbol) { final EntryMessage entryMessage = log.traceEntry("line={} col={} symbol={}", line, col, symbol); if (symbol.startsWith("@")) { symbol = symbol.substring(1); } final List<String> searchTargets = new ArrayList<>(4); String fqcn = source.getImportedClassFQCN(symbol, null); if (fqcn == null) { final CachedASMReflector reflector = CachedASMReflector.getInstance(); final Map<String, String> standardClasses = reflector.getStandardClasses(); fqcn = standardClasses.get(symbol); if (fqcn == null) { if (!source.getPackageName().isEmpty()) { fqcn = source.getPackageName() + '.' + symbol; } else { fqcn = symbol; } searchTargets.add(fqcn); // Add inner class final String finalSym = symbol; source .getTypeScope(line) .ifPresent( typeScope -> { final String firstFQCN = typeScope.getFQCN(); searchTargets.add(firstFQCN + ClassNameUtils.INNER_MARK + finalSym); }); } } else { searchTargets.add(fqcn); } final Optional<Location> location = searchTargets.stream().map(this::getFQCNLocation).filter(Objects::nonNull).findFirst(); log.traceExit(entryMessage); return location; }
private MethodSignatureVisitor(String name, MethodSignatureVisitor parent) { super(Opcodes.ASM5); final EntryMessage message = log.traceEntry("name={}", name); this.name = name; this.parent = parent; this.classTypeParameters = parent.classTypeParameters; this.typeMap = parent.typeMap; log.traceExit(message); }
@Override public void visitFormalTypeParameter(final String s) { final EntryMessage message = log.traceEntry("s={} current={}", s, this.current); if (this.formalType == null) { this.formalType = new TypeInfo("", ""); this.formalType.typeParameters = new ArrayList<>(4); this.formalType.typeParameters.add(new TypeInfo(s, s)); } else { this.formalType.typeParameters.add(new TypeInfo(s, s)); } log.traceExit(message); }
@Override public SignatureVisitor visitParameterType() { final EntryMessage message = log.traceEntry("name={} current={}", this.name, this.current); MethodSignatureVisitor visitor = new MethodSignatureVisitor(this.name, this); visitor.isParameter = true; log.traceExit(message); return visitor; }
@Override public SignatureVisitor visitReturnType() { final EntryMessage message = log.traceEntry("name={} parameterTypes={}", this.name, this.parameterTypes); MethodSignatureVisitor visitor = new MethodSignatureVisitor(this.name, this); visitor.isReturn = true; log.traceExit(message); return visitor; }
@Override public SignatureVisitor visitClassBound() { final EntryMessage message = log.traceEntry("current={}", this.current); MethodSignatureVisitor visitor = new MethodSignatureVisitor(this.name, this); visitor.current = this.formalType; this.formalType = null; visitor.isFormalType = true; visitor.isClassBound = true; log.traceExit(message); return visitor; }
FieldSignatureVisitor(final String name, final List<String> classTypeParameters) { super(Opcodes.ASM5); final EntryMessage em = log.traceEntry("name={} classTypeParameters={}", name, classTypeParameters); this.name = name; this.classTypeParameters = classTypeParameters; this.typeParameters = new HashSet<>(2); log.traceExit(em); isInstance = false; isSuper = false; isExtends = false; holdArray = false; }
@Override public void visitClassType(final String s) { final String className = ClassNameUtils.replaceSlash(s); final TypeInfo typeInfo = new TypeInfo(className, className); if (this.typeInfo == null) { this.typeInfo = typeInfo; if (this.holdArray) { this.typeInfo.isArray = true; this.holdArray = false; } } final EntryMessage em = log.traceEntry( "s={} name={} typeInfo={} currentType={}", s, this.name, this.typeInfo, this.currentType); if (this.currentType.size() == 0) { // set main this.currentType.push(typeInfo); } else { final TypeInfo current = this.currentType.peek(); if (current != null && current.typeParameters != null && isInstance) { current.typeParameters.add(typeInfo); // swap this.currentType.push(typeInfo); } } log.traceExit(em); }
@Override public SignatureVisitor visitArrayType() { final TypeInfo current = this.currentType.peek(); if (current != null) { current.isArray = true; } else { // on hold array flag this.holdArray = true; } final EntryMessage em = log.traceEntry("name={} current={} currentType={}", this.name, current, this.currentType); return log.traceExit(em, super.visitArrayType()); }
@Override public void visitEnd() { final EntryMessage em = log.traceEntry( "name={} typeInfo={} currentType={} ", this.name, this.typeInfo, this.currentType); if (this.currentType.size() > 1) { this.currentType.pop(); } log.traceExit(em); }
@Override public void visitEnd() { final EntryMessage m = log.traceEntry("fieldSignature={}", fieldSignature); final String modifier = ASMReflector.toModifier(access, false); final String fqcn = fieldSignatureVisitor.getResult(); final FieldDescriptor fd = new FieldDescriptor(this.classAnalyzeVisitor.className, this.name, modifier, fqcn); fd.setTypeParameters(fieldSignatureVisitor.getTypeParameters()); this.classAnalyzeVisitor.getMembers().add(fd); log.traceExit(m); }
MethodAnalyzeVisitor( final ClassAnalyzeVisitor classAnalyzeVisitor, final int access, final String name, final String desc, final String signature, final String[] exceptions) { super(Opcodes.ASM5); final EntryMessage entryMessage = log.traceEntry( "classAnalyzeVisitor={} access={} name={} desc={} signature={} exceptions={}", classAnalyzeVisitor, access, name, desc, signature, exceptions); this.classAnalyzeVisitor = classAnalyzeVisitor; this.access = access; this.name = name; this.exceptions = exceptions; Type[] args = Type.getArgumentTypes(desc); this.parameterNames = new String[args.length]; boolean isStatic = (Opcodes.ACC_STATIC & access) > 0; this.lvtSlotIndex = computeLvtSlotIndices(isStatic, args); String target = desc; if (signature != null) { target = signature; } this.methodSignature = target; this.interfaceMethod = this.classAnalyzeVisitor.getClassIndex().isInterface(); // log.trace("name:{} sig:{}", name, target); // log.trace("classIndex:{}", classAnalyzeVisitor.getClassIndex().isInterface); // log.debug("methodName {} desc {} sig {}", name, desc, signature); log.traceExit(entryMessage); }
@Override public void visitCode() { final EntryMessage entryMessage = log.traceEntry("name={}", this.name); if (this.interfaceMethod) { this.hasDefault = true; } super.visitCode(); log.traceExit(entryMessage); }
@Override public void visitEnd() { final EntryMessage entryMessage = log.traceEntry("returnType={} parameterTypes={}", this.returnType, this.parameterTypes); if (this.returnType == null) { // void this.returnType = new TypeInfo("void", "void"); } if (this.parameterTypes == null) { // void this.parameterTypes = new ArrayList<>(4); } if (((this.parameterTypes.size() != this.parameterNames.length) || (this.parameterNames.length > 0 && this.parameterNames[0] == null)) && !tryGetParameterName(this.classAnalyzeVisitor.className, this.name)) { setDefaultParameterNames(); } for (int i = 0; i < this.parameterTypes.size(); i++) { TypeInfo typeInfo = this.parameterTypes.get(i); typeInfo.paramName = this.parameterNames[i]; } // log.debug("{} ({})", this.name, this.parameterTypes); this.toMemberDescriptor(); log.traceExit(entryMessage); }