当前位置: 代码迷 >> java >> Gradle抱怨它不能将long转换为int,即使该方法需要很长时间作为参数
  详细解决方案

Gradle抱怨它不能将long转换为int,即使该方法需要很长时间作为参数

热度:51   发布时间:2023-07-31 11:21:17.0

运行“gradle build”时,我的一个项目出现了以下错误,其中几个类得到以下编译错误:

cannot be applied to given types;
                this._logFilter.setFirstResult(firstResult);
                               ^
  required: int
  found: long
  reason: actual argument long cannot be converted to int by method invocation conversion

尽管如此,方法setFirstResult需要一个long参数。 这是代码:

public void setFirstResult(long firstResult) {
    this._firstResult = firstResult;
}

我已经尝试过--refresh-dependencies并清除缓存等。这些都不适用于我。 此外,这个项目是一个maven项目,我将其转换为使用gradle。

编辑:

我根据评论的请求在这里添加了额外的上下文:

这是this._logFilter的源代码

public class GlobalMessageLogFilter {
    private long _firstResult = 0L;

    private long _maxResults = 100L;

    private Application _application;

    private SeverityLevelEnum _severityLevel;

    private EnvironmentEnum _environment;

    private String _userName;

    private Category _category;

    public EnvironmentEnum getEnvironment() {
        return this._environment;
    }

    public void setEnvironment(EnvironmentEnum environment) {
        this._environment = environment;
    }

    public long getFirstResult() {
        return this._firstResult;
    }

    public void setFirstResult(long firstResult) {
        this._firstResult = firstResult;
    }

    public long getMaxResults() {
        return this._maxResults;
    }

    public void setMaxResults(long maxResults) {
        this._maxResults = maxResults;
    }

    public Application getApplication() {
        return this._application;
    }

    public void setApplication(Application application) {
        this._application = application;
    }

    public SeverityLevelEnum getSeverityLevel() {
        return this._severityLevel;
    }

    public void setSeverityLevel(SeverityLevelEnum severityLevel) {
        this._severityLevel = severityLevel;
    }

    public String getUserName() {
        return this._userName;
    }

    public void setUserName(String userName) {
        this._userName = userName;
    }

    public Category getCategory() {
        return this._category;
    }

    public void setCategory(Category category) {
        this._category = category;
    }
}

这是完整的堆栈跟踪

λ gradle build
:compileJava
C:\Java Source\wicket\administration\GlobalMessageLogProvider.java:36: error: method setFirstResult in class GlobalMessageLogFilter cannot be applied to given types;
                this._logFilter.setFirstResult(firstResult);
                               ^
  required: int
  found: long
  reason: actual argument long cannot be converted to int by method invocation conversion
C:\Java Source\wicket\administration\GlobalMessageLogProvider.java:37: error: method setMaxResults in class GlobalMessageLogFilter cannot be applied to given types;
                this._logFilter.setMaxResults(maxResults);
                               ^
  required: int
  found: long
  reason: actual argument long cannot be converted to int by method invocation conversion
C:\Java Source\wicket\administration\users\UserSecurityProvider.java:35: error: method setFirst in class UserSearchFilter cannot be applied to given types;
                this._filter.setFirst(first);
                            ^
  required: int
  found: long
  reason: actual argument long cannot be converted to int by method invocation conversion
C:\Java Source\wicket\administration\users\UserSecurityProvider.java:36: error: method setCount in class UserSearchFilter cannot be applied to given types;
                this._filter.setCount(count);
                            ^
  required: int
  found: long
  reason: actual argument long cannot be converted to int by method invocation conversion

编辑:添加了GlobalMessageLogProvider的源代码

public class GlobalMessageLogProvider extends SortableDataProvider<GlobalMessageLog, String>
{
    @SpringBean
    private GlobalMessageLogRepository _globalMessageLogRepository;
    private GlobalMessageLogFilter _logFilter;
    private boolean _searchAllowed = false;


    public GlobalMessageLogProvider(GlobalMessageLogFilter globalMessageLogFilter)
    {
        Injector.get().inject(this);
        this._logFilter = globalMessageLogFilter;
    }


    @Override
    public Iterator<? extends GlobalMessageLog> iterator(long firstResult, long maxResults)
    {
        this._logFilter.setFirstResult(firstResult);
        this._logFilter.setMaxResults(maxResults);
        Iterator<GlobalMessageLog> results = Arrays.<GlobalMessageLog> asList().iterator();

        if (this._searchAllowed)
        {
            if (super.getSort() == null)
            {
                results = this._globalMessageLogRepository.search(this._logFilter, "id", false).iterator();
            } else
            {
                results =
                        this._globalMessageLogRepository.search(this._logFilter,
                                super.getSort().getProperty(),
                                super.getSort().isAscending()).iterator();
            }
        }
        return results;
    }
}

我发现了问题,我能够解决它。 多数评论指出setFirstResult(int firstResult)是它使用的类的错误版本。 他们是对的。

基本上, GlobalMessageLogProvider已被更改,但jar的最新版本从未被推送到Nexus服务器(我们自己的私有第三方依赖服务器)。 所以,每当我拉下罐子时,它总是引用旧瓶子。

手动将最新的jar推送到Nexus服务器后,我能够成功地使用gradle构建项目而不会出现错误。

所以从这个问题中解脱出来:我们需要创建一个构建过程,自动构建/推送最新版本的jar到Nexus服务器。 我正在考虑使用一些自定义命令/脚本通过我们的TeamCity构建服务器创建构建/推送过程。 (如果有的话,请随时提供更好的建议/练习提示)。

谢谢大家的善意帮助,我衷心感谢。

  相关解决方案