jboss-javassist / javassist

Java bytecode engineering toolkit
www.javassist.org
Other
4.11k stars 700 forks source link

CannotCompileException: [source error] syntax error near l$lambda-0 #412

Open gitee-shareware opened 2 years ago

gitee-shareware commented 2 years ago

method string: public static void Publicinstall$lambda-0(java.lang.Throwable var0){return install$lambda-0(var0);}

CtMethod.make(methodstring,ctClass);

error: javassist.CannotCompileException: [source error] syntax error near "l$lambda-0(java.lang"

heweimxcz commented 1 year ago

it happen to me

heweimxcz commented 1 year ago

but i dont know how to solve it

heweimxcz commented 1 year ago

you resolve it?

chibash commented 1 year ago

Can you show a runnable test code that reproduce this error? The given information is too incomplete to give an useful feedback.

heweimxcz commented 1 year ago

Can you show a runnable test code that reproduce this error? The given information is too incomplete to give an useful feedback.

CtMethod.make("public static int addActionInterceptor\$lambda-1(com.xxx.xxx.xxx.router.action.interceptor.ActionInterceptor var0,com.xxx.xxx.xxx.router.action.interceptor.ActionInterceptor var1){return addActionInterceptor\$lambda-1(var0,var1);}", ctClass)

chibash commented 1 year ago

No, that's not a runnable test code. It must be a complete .java source file and all related data. For example, CtMethod.make must read the bytecode (.class file) of ctClass and check whether it declares the addAction ... lambda-1 method. It must read the bytecode of com.xxx.xxx.ActionInterceptor as well. So the test code must include those class files.

heweimxcz commented 1 year ago

private final static addActionInterceptor$lambda-1(Lcom/xxx/xxx/xxx/router/action/interceptor/ActionInterceptor;Lcom/xxx/xxx/xxx/router/action/interceptor/ActionInterceptor;)I L0 LINENUMBER 62 L0 ALOAD 0 IFNONNULL L1 L2 LINENUMBER 63 L2 ICONST_M1 GOTO L3 L1 LINENUMBER 64 L1 FRAME SAME ALOAD 1 IFNONNULL L4 L5 LINENUMBER 65 L5 ICONST_1 GOTO L3 L4 LINENUMBER 66 L4 FRAME SAME ALOAD 1 INVOKEVIRTUAL com/xxx/xxx/xxx/router/action/interceptor/ActionInterceptor.getPriority ()I ALOAD 0 INVOKEVIRTUAL com/xxx/xxx/xxx/router/action/interceptor/ActionInterceptor.getPriority ()I ISUB L3 LINENUMBER 62 L3 FRAME SAME1 I IRETURN L6 LOCALVARIABLE o1 Lcom/xxx/xxx/xxx/router/action/interceptor/ActionInterceptor; L0 L6 0 LOCALVARIABLE o2 Lcom/xxx/xxx/xxx/router/action/interceptor/ActionInterceptor; L0 L6 1 MAXSTACK = 2 MAXLOCALS = 2

this method is generate by java compiler
heweimxcz commented 1 year ago

i want to use javassist add a similar method to call origin method that generate by java compiler

heweimxcz commented 1 year ago

you mean CtMethod.make must pass paramter like java origin source?

chibash commented 1 year ago

CtMethod.make() must read the class files of all the classes that the given source code refers to.

gitee-shareware commented 1 year ago

Can you show a runnable test code that reproduce this error? The given information is too incomplete to give an useful feedback.

Any class

For example:

public class PatchTemplate implements ChangeQuickRedirect { public static final String MATCH_ALL_PARAMETER = "(\w\.)\w*"; public PatchTemplate() { } private static final Map<Object, Object> keyToValueRelation = new WeakHashMap<>(); @Override public Object accessDispatch(String methodName, Object[] paramArrayOfObject) { return null; } @Override public boolean isSupport(String methodName, Object[] paramArrayOfObject) { return true; } }

public interface ChangeQuickRedirect { Object accessDispatch(String methodName, Object[] paramArrayOfObject); boolean isSupport(String methodName, Object[] paramArrayOfObject); }

//error:javassist.CannotCompileException: [source error] syntax error near "l$lambda-0(java.lang"

private CtClass createControlClass() throws Exception { CtClass controlClass = Config.classPool.get("com.xxx.PatchTemplate"); String methodBody = "public static void " + "Publicinstall$lambda-0(java.lang.Throwable var0){return install$lambda-0(var0);}"; controlClass.addMethod(CtMethod.make(methodBody, controlClass)); controlClass.defrost(); return controlClass; }

heweimxcz commented 1 year ago

Can you show a runnable test code that reproduce this error? The given information is too incomplete to give an useful feedback.

Any class

For example: `public class PatchTemplate implements ChangeQuickRedirect { public static final String MATCH_ALLPARAMETER = "(\w*.)\w_"; public PatchTemplate() { }

private static final Map<Object, Object> keyToValueRelation = new WeakHashMap<>();

@Override
public Object accessDispatch(String methodName, Object[] paramArrayOfObject) {
    return null;
}

@Override
public boolean isSupport(String methodName, Object[] paramArrayOfObject) {
    return true;
}

}`

public interface ChangeQuickRedirect { Object accessDispatch(String methodName, Object[] paramArrayOfObject);

boolean isSupport(String methodName, Object[] paramArrayOfObject);

}

//error:javassist.CannotCompileException: [source error] syntax error near "l$lambda-0(java.lang" private CtClass createControlClass() throws Exception { CtClass controlClass = Config.classPool.get("com.xxx.PatchTemplate"); String methodBody = "public static void " + "Publicinstall$lambda-0(java.lang.Throwable var0){return install$lambda-0(var0);}"; controlClass.addMethod(CtMethod.make(methodBody, controlClass)); controlClass.defrost(); return controlClass; }

你也在用美团的robust?

heweimxcz commented 1 year ago

image

Can you show a runnable test code that reproduce this error? The given information is too incomplete to give an useful feedback.

Any class

For example: `public class PatchTemplate implements ChangeQuickRedirect { public static final String MATCH_ALLPARAMETER = "(\w*.)\w_"; public PatchTemplate() { }

private static final Map<Object, Object> keyToValueRelation = new WeakHashMap<>();

@Override
public Object accessDispatch(String methodName, Object[] paramArrayOfObject) {
    return null;
}

@Override
public boolean isSupport(String methodName, Object[] paramArrayOfObject) {
    return true;
}

}`

public interface ChangeQuickRedirect { Object accessDispatch(String methodName, Object[] paramArrayOfObject);

boolean isSupport(String methodName, Object[] paramArrayOfObject);

}

//error:javassist.CannotCompileException: [source error] syntax error near "l$lambda-0(java.lang" private CtClass createControlClass() throws Exception { CtClass controlClass = Config.classPool.get("com.xxx.PatchTemplate"); String methodBody = "public static void " + "Publicinstall$lambda-0(java.lang.Throwable var0){return install$lambda-0(var0);}"; controlClass.addMethod(CtMethod.make(methodBody, controlClass)); controlClass.defrost(); return controlClass; }

image

其实就是这段自动生成的方法用javassist编译不过,我用的java11

gitee-shareware commented 1 year ago

error:javassist.CannotCompileException: [source error] syntax error near "l$lambda-0(java.lang"

this is a library issue, and javassist does something special with the '-' symbol.

gitee-shareware commented 1 year ago

你也在用美团的robust?

是的,你可以对‘-’符号做特殊处理,可以规避此问题

heweimxcz commented 1 year ago

你替换了dx.jar吗

heweimxcz commented 1 year ago

你也在用美团的robust?

是的,你可以对‘-’符号做特殊处理,可以规避此问题

我用javassist ByteCode 的API也能规避这个问题,但后来dx.jar执行的那一步还是出错

heweimxcz commented 1 year ago

你用的java版本是11吗

hasonguo commented 1 year ago

@heweimxcz @OldSixth 这面解决了吗?可以贴一下思路吗?怎么对‘-’符号做特殊处理?