javassist

程序员需要了解依赖冲突的原因以及解决方案

♀尐吖头ヾ 提交于 2020-02-26 07:35:16
0x00. 前言 依赖冲突是日常开发中经常碰到的过程,如果运气好,并不会有什么问题。偏偏小黑哥有点背,碰到好几次生产问题,排查一整晚,最后发现却是依赖冲突的引起的问题。 没碰到过这个问题同学可能没什么感觉,小黑哥举两个最近碰到例子,让大家感受一些。 例子 1: 我们公司有个古老的业务基础包 A。B,C 业务依赖这个包。某个团队拷贝 A 的部分代码进行重构, 类名与路径完全一样 ,然后重新打包成 D 发布。 一次业务改动,B 业务也引入了 D 包,测试环境运行的时候,一切 OK,但是在生产运行时,却抛出 NoSuchMethodError 。 问题原因在于 B 业务依赖 A,D。而 A,D 存在两个同包同名类,运行的时候,具体加载谁,不同环境还真不一样。 例子 2: A 业务使用 Dubbo 进行 RPC 调用, Dubbo 需要依赖 javassist 。当前依赖关系为: A------->Dubbo------->javassist-3.18.1.GA 某次改动中引入另外一个第三方开源包,其依赖 javassist-3.15.0-GA 。生产发布的时候,将 javassist-3.15.0-GA 打包到应用中,由于生产环节为 JDK1.8,从而导致运行直接失败。 除了上述问题,依赖冲突还可能导致应用抛出 ClassNotFoundException ,

两周自制脚本语言-第10天 无法割舍的数组

社会主义新天地 提交于 2020-02-25 18:32:24
第10天 无法割舍的数组 目标:为Stone语言添加简单的数组功能,下标(index)只能使用整数值。 10.1扩展语法分析器 代码清单10.1 与数组相关的语法规则 elements : expr { "," expr } primary : ( "[" [ elements ] "]" | "(" expr ")" | NUMBER | IDENTIFIER | STRING ) { postfix } postfix : "(" [ args ] ")" | "[" expr "]" 代码清单10.2 ArrayParser.java // 代码清单10.2 ArrayParser.java package stone; import stone.ast.*; import javassist.gluonj.Reviser; import static stone.Parser.rule; @Reviser public class ArrayParser extends FuncParser { Parser elements = rule(ArrayLiteral.class).ast(expr).repeat(rule().sep(",").ast(expr)); public ArrayParser() { reserved.add("]"); primary

Javassist: insert a method at the beginning of catch block

隐身守侯 提交于 2020-02-06 02:51:08
问题 I have code: ControlFlow cf = new ControlFlow(method); for (ControlFlow.Block block : cf.basicBlocks()) { ControlFlow.Catcher catchBlocks[] = block.catchers(); for (int i = 0;i < catchBlocks.length;i++) { int position = catchBlocks[i].block().position(); method.insertAt(position, "System.out.println(\"catch block\")") } } This code snippet inserts the print statement at beginning of the method, which is not what I want. I want the code to be placed like: void foo() { try { a(); } catch

Javassist: insert a method at the beginning of catch block

对着背影说爱祢 提交于 2020-02-06 02:50:13
问题 I have code: ControlFlow cf = new ControlFlow(method); for (ControlFlow.Block block : cf.basicBlocks()) { ControlFlow.Catcher catchBlocks[] = block.catchers(); for (int i = 0;i < catchBlocks.length;i++) { int position = catchBlocks[i].block().position(); method.insertAt(position, "System.out.println(\"catch block\")") } } This code snippet inserts the print statement at beginning of the method, which is not what I want. I want the code to be placed like: void foo() { try { a(); } catch

Javassist's CtMethod.insertAt(line,src) instruments code at the wrong bytecode position

限于喜欢 提交于 2020-01-16 18:34:55
问题 My goal is to insert a little bit of instrumentation code at the beginning of each basic block of code. It seems like a fairly simple task with Javaassist's ControlFlow.Block and CtMethod.insertAt(). Here's the relevant chunk of code so far (it's located in the transform function): ControlFlow flow=new ControlFlow(m); //m is the CtMethod currently being instrumented Block[] blockArray=flow.basicBlocks(); for(Block thisbb : blockArray){ //Dynamically Update Method Statistics String blockUpdate

javassist.CannotCompileException: [source error] ) is missing what is this?

◇◆丶佛笑我妖孽 提交于 2020-01-16 01:00:20
问题 I'm trying to write some Bytecode manipulation in my web application now when I try to inject my code into my methods it always throws me the error javassist.CannotCompileException: [source error] ) is missing I don't know why and what this is ... I've googled a bit and some people say It's a bug from version 1.0 javassist but I thinks that's really unrealistic. private void changeMethod(CtMethod method) throws NotFoundException, CannotCompileException { if (method.hasAnnotation(Loggable

javassist.CannotCompileException: [source error] ) is missing what is this?

こ雲淡風輕ζ 提交于 2020-01-16 01:00:03
问题 I'm trying to write some Bytecode manipulation in my web application now when I try to inject my code into my methods it always throws me the error javassist.CannotCompileException: [source error] ) is missing I don't know why and what this is ... I've googled a bit and some people say It's a bug from version 1.0 javassist but I thinks that's really unrealistic. private void changeMethod(CtMethod method) throws NotFoundException, CannotCompileException { if (method.hasAnnotation(Loggable

renaming a field using javassist at runtime in the pre-main method (java instrumentation)

谁说我不能喝 提交于 2020-01-13 09:34:32
问题 I want to rename a field inside a java class at runtime. In addition, Any method that access that field ;wether it's read or write; I need it to be modified to use the new name instead of the old name.... All this will be done inside the pre-main method... As an Exmaple, given the following code: public class Class1 { String strCompany; public String Test() { strCompany = "TestCompany"; return strCompany; } } In the above class, I need to change the field "strCompany" to be "strCompany2", in

android replace a method call at runtime

杀马特。学长 韩版系。学妹 提交于 2020-01-13 06:01:26
问题 I am developing an Android app with a 3rd-party library. I want to replace a method call in the library. Please note that I cannot obtain the library's source code, so that I have to change it at runtime. For example, let's assume there is doA() method in a class Foo in the library class Foo { doA() { //method body } ... } I want to replace the method body of doA() with my own code. I have done some exploration and found the following stackoverflow thread: Replacing a method call in a class

框架升级填坑总结

眉间皱痕 提交于 2020-01-08 18:20:25
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 因公司业务发展,将jdk1.6升级至1.8,tomcat6升级至8。因老项目使用struts2.3.20版本,使用velocity模板渲染,需针对此框架整体升级至struts2.5.x,spring4.x。 注: struts2.5.x适用jdk1.7及以上。 1、更新项目maven包。struts2-core,struts2-json-plugin,struts2-spring-plugin及其余引用版本。删除原低版本引用,例如 :xwork-core ( 2.5版本 struts内置引用了新版 ),保证只有单一新版,防止冲突。 2、修改web.xml。删掉标红的ng. 。 <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher. ng. filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> 3、修改struts.xml文件头部信息标红部位,修改为新的大版本数值,此处使用2.5.x,因此为2.5 <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD