INRIA / spoon

Spoon is a metaprogramming library to analyze and transform Java source code. :spoon: is made with :heart:, :beers: and :sparkles:. It parses source files to build a well-designed AST with powerful analysis and transformation API.

Home Page:http://spoon.gforge.inria.fr/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Bug]: can not process lombok annatations

wangjianxin199003 opened this issue · comments

Describe the bug

spoon.compiler.ModelBuildingException: log cannot be resolved at ${path}/PtZzUserServiceImpl.java:37
at spoon.support.compiler.jdt.JDTBasedSpoonCompiler.report(JDTBasedSpoonCompiler.java:663)
at spoon.support.compiler.jdt.JDTBasedSpoonCompiler.reportProblems(JDTBasedSpoonCompiler.java:645)
at spoon.support.compiler.jdt.JDTBasedSpoonCompiler.build(JDTBasedSpoonCompiler.java:119)
at spoon.support.compiler.jdt.JDTBasedSpoonCompiler.build(JDTBasedSpoonCompiler.java:100)
at spoon.Launcher.buildModel(Launcher.java:781)
at spoon.Launcher.run(Launcher.java:732)
at com.zhuanzhuan.arch.service.resource.management.service.SourceCodeScanService.main(SourceCodeScanService.java:349)

Source code you are trying to analyze/transform

package com.bj58.zhuanzhuan.zzkfass.ass.service.search.impl;

import java.util.Objects;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.bj58.zhuanzhuan.zzkfass.ass.service.search.PtZzUserService;
import com.bj58.zhuanzhuan.zzuser.contract.IZZUserService;
import com.bj58.zhuanzhuan.zzuser.entity.ZZUser;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class PtZzUserServiceImpl implements  PtZzUserService {

    @Autowired
    private IZZUserService zzUserService;

    @Value("${zz.user.decode.source.config}")
    private Integer decryptSource;

    @Value("${zz.user.decode.key.config}")
    private String decryptKey;

    /**
     * 根据用户的 uid 获取用户信息
     *
     * @param logStr
     * @param uid
     * @return
     */
    public ZZUser getZzUserByUid(String logStr, Long uid) {

        log.info("{} cmd=getZzUserByUid uid={}", logStr, uid); //here is line 37
        if (Objects.isNull(uid)) {
            return null;
        }
        try {
            return zzUserService.getByUid(uid, logStr);
        } catch (Exception e) {
            log.error(logStr, e);
        }
        return null;
    }
}

Source code for your Spoon processing

MavenLauncher launcher = new MavenLauncher("${path}/pom.xml", MavenLauncher.SOURCE_TYPE.ALL_SOURCE,"/home/wangjianxin/.local/share/JetBrains/Toolbox/apps/intellij-idea-ultimate-2/plugins/maven/lib/maven3");
        launcher.getEnvironment().setShouldCompile(true);
        launcher.getEnvironment().setAutoImports(true);
        launcher.getEnvironment().setIgnoreSyntaxErrors(true);
        LinkedList<JavaMethodProcessor.JavaMethod> list = new LinkedList<>();
        launcher.addProcessor(new JavaMethodProcessor(list));
        launcher.run();

Actual output

spoon.compiler.ModelBuildingException: log cannot be resolved at ${path}/PtZzUserServiceImpl.java:37
	at spoon.support.compiler.jdt.JDTBasedSpoonCompiler.report(JDTBasedSpoonCompiler.java:663)
	at spoon.support.compiler.jdt.JDTBasedSpoonCompiler.reportProblems(JDTBasedSpoonCompiler.java:645)
	at spoon.support.compiler.jdt.JDTBasedSpoonCompiler.build(JDTBasedSpoonCompiler.java:119)
	at spoon.support.compiler.jdt.JDTBasedSpoonCompiler.build(JDTBasedSpoonCompiler.java:100)
	at spoon.Launcher.buildModel(Launcher.java:781)
	at spoon.Launcher.run(Launcher.java:732)
	at com.zhuanzhuan.arch.service.resource.management.service.SourceCodeScanService.main(SourceCodeScanService.java:349)

Expected output

none

Spoon Version

11.0.0

JVM Version

8

What operating system are you using?

linux

This is not really a bug in spoon. You ask spoon to please ensure the model should compile — but it is not proper java code. Lombok adds its own custom processing extending the java compiler and syntax. Supporting all transformations that Lombok does is out of scope for spoon, as the code is no longer Java.

If you disable "setShouldCompile", you should get a model though. As the code is not valid, some parts of it might be a bit surprising (e.g. what fields refer to if they do not exist in the source code).

You can also use https://projectlombok.org/features/delombok to pre-process your sources into a pure-Java form spoon can analyze.

谢谢,我把shouldComplie参数设置为false,解决了我的问题

This is not really a bug in spoon. You ask spoon to please ensure the model should compile — but it is not proper java code. Lombok adds its own custom processing extending the java compiler and syntax. Supporting all transformations that Lombok does is out of scope for spoon, as the code is no longer Java.

If you disable "setShouldCompile", you should get a model though. As the code is not valid, some parts of it might be a bit surprising (e.g. what fields refer to if they do not exist in the source code).

You're welcome! I set the shouldCompile parameter to false and resolved my issue.