Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not support users.stream().filter(User::isCool).collect(Collectors.toList()) when use reflections.getMemberUsage(method) #442

Open
jusescn opened this issue Nov 23, 2022 · 1 comment

Comments

@jusescn
Copy link

jusescn commented Nov 23, 2022

biz code:
public String buildWord(){
List users = new ArrayList();
List names = users.stream()
//.filter(e -> e.isCool() &&e.getName().equals(""))
.filter(User::isCool)
.map(User::getName)
.collect(Collectors.toList());
return names.toString();
}

Reflections reflections = new Reflections(new ConfigurationBuilder()
.setUrls(Collections.singletonList(ClasspathHelper.forClass(UsageTest.class)))
.setScanners(new MemberUsageScanner()));
Method[] methods = User.class.getDeclaredMethods();
for (Method method : methods) {
Collection members = reflections.getMemberUsage(method);
System.out.println("method:"+method+",members:"+members);
}
members return null.

@jusescn
Copy link
Author

jusescn commented Nov 23, 2022

I find a jar to fix . a goog jar to fix stream ;

com.sebastian-daschner
jaxrs-analyzer
0.9

core code to fix this bug;
https://1maven.com/classname/com.sebastian-daschner:jaxrs-analyzer:0.9/com.sebastian_daschner.jaxrs_analyzer.analysis.bytecode.collection/InvokeInstructionBuilder/cl

or simple use like this;
ByteCodeCollector collector = new ByteCodeCollector();
List list = collector.buildInstructions(member);
for (Instruction instruction : list) {
if (instruction instanceof InvokeInstruction) {
InvokeInstruction invokeInstruction = (InvokeInstruction) instruction;
MethodIdentifier methodIdentifier = invokeInstruction.getIdentifier();
System.out.println("methodIdentifier:" + methodIdentifier);
}
if (instruction instanceof InvokeDynamicInstruction) {
InvokeDynamicInstruction invokeDynamicInstruction = (InvokeDynamicInstruction) instruction;
MethodIdentifier methodIdentifier = invokeDynamicInstruction.getIdentifier();
System.out.println("methodIdentifier:" + methodIdentifier);
}
}

or if you like ,you can update MemberUsageScanner like this( I do not test them,copy from up.)
MethodInfo methodInfo = member.getMethodInfo();
CodeAttribute codeAttribute = methodInfo.getCodeAttribute();
ConstPool pool = methodInfo.getConstPool();
CodeIterator codeIterator = codeAttribute.iterator();
final int lambdaIndex = pool.getInvokeDynamicNameAndType(index);
final String lambdaSignature = pool.getUtf8Info(pool.getNameAndTypeDescriptor(lambdaIndex));
final String lambdaMethodName = pool.getUtf8Info(pool.getNameAndTypeName(lambdaIndex));
final SignatureAttribute.MethodSignature methodSignature = SignatureAttribute.toMethodSignature(lambdaSignature);
SignatureAttribute.Type lambdaReturnType = methodSignature.getReturnType();
SignatureAttribute.Type[] lambdaParameters = methodSignature.getParameterTypes();

                   final CtClass ctClass;
                   try {
                       ctClass = ClassPool.getDefault().get(pool.getClassName());
                   } catch (NotFoundException e) {
                       throw new IllegalStateException("Could not analyze bytecode");
                   }

                   final BootstrapMethodsAttribute bootstrapMethods = (BootstrapMethodsAttribute) ctClass.getClassFile().getAttribute("BOOTSTRAP_ATTRIBUTE_NAME");
                   final int bootstrapIndex = pool.getInvokeDynamicBootstrap(index);
                   final int actualMethodIndex = bootstrapMethods.getMethods()[bootstrapIndex].arguments[1];
                   final int actualMethodRefIndex = pool.getMethodHandleIndex(actualMethodIndex);
                   final boolean actualMethodStatic = pool.getMethodHandleKind(actualMethodIndex) == ConstPool.REF_invokeStatic;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant