maven编译报错“不兼容的类型”

来源:互联网 发布:淘宝导航黑色的代码 编辑:程序博客网 时间:2024/06/08 15:54

刚开始用maven,没看出来哪里有问题。之前普通java工程编译没问题的,依赖的问题也都解决了。

一般来说,编译有问题有几种:

1、jdk版本问题:maven编译插件默认支持的是jdk版本比较低,不能够支持现有的语法。

     解决方法:为编译插件指定source和target为合适的jdk版本,如下的pom片段所示。

2、源码的编码问题:

    解决方法:为编译插件指定encoding的值为想要的平台,使用UTF-8的较多。阿里好像使用GBK,因为GBK编码的文件比较小。


这次问题不是以上两种,折腾半天也没找到原因。没经验就是比较痛苦。


一段庸俗且啰嗦hibernate查询代码如下:

<span style="font-family:Microsoft YaHei;font-size:14px;">       @Override       public List<Mapping> getMappings(final String sourceName,                   final String sCode, final String sMarket,                   final String dCode, final Integer dMarket,                   final boolean isUnmatched, final Date start, final Date end)                   throws BusinessException {             return this.getHibernateTemplate().execute(new HibernateCallback() { //这是第60行                   public Object doInHibernate(Session session ) throws HibernateException, SQLException {                        Criteria criteria = session.createCriteria(Mapping. class);                         if (!sourceName.equals("" ))                              criteria.add(Restrictions. eq("sourceName", sourceName));                         if (!sCode.equals("" ))                              criteria.add(Restrictions. eq("sCode", sCode));                         if (!sMarket.equals("" ))                              criteria.add(Restrictions. eq("sMarket", sMarket));                         if (isUnmatched) {                              criteria.add(Restrictions. isNull("dCode"));                              criteria.add(Restrictions. isNull("dMarket"));                        } else {                               if (dCode != null)                                    criteria.add(Restrictions. eq("dCode", dCode));                               if (dMarket != null)                                    criteria.add(Restrictions. eq("dMarket", dMarket));                        }                         if (start != null)                              criteria.add(Restrictions. ge("updatedDate", start));                         if (end != null)                              criteria.add(Restrictions. le("updatedDate", end));                         return criteria.list();                  }            });      }</span>



相关POM配置片段如下:
<span style="font-family:Microsoft YaHei;font-size:14px;"><build><finalName>test</finalName><plugins>  <plugin>    <artifactId>maven-compiler-plugin</artifactId>    <version>3.1</version>    <configuration>    <span style="color:#ff0000;"><source>1.6</source>    <target>1.6</target>    <encoding>UTF8</encoding> </span>    </configuration>    </plugin>  </plugins></build></span>

其他一些debug信息

Apache Maven 3.0.2 (r1056850; 2011-01-09 08:58:10+0800)
Java version: 1.6.0_21, vendor: Sun Microsystems Inc.
Java home: C:\Program Files\Java\jdk1.6.0_21\jre
。。。。。。
[DEBUG]   (f) compilerId = javac
[DEBUG]   (f) debug = true
[DEBUG]   (f) failOnError = true
[DEBUG]   (f) forceJavacCompilerUse = false
[DEBUG]   (f) fork = false
[DEBUG]   (f) generatedSourcesDirectory = 
F:\workspace\test\target\generated-sources\annotations
[DEBUG]   (f) mojoExecution = org.apache.maven.plugins:maven-compiler-
plugin:3.1:compile {execution: default-cli}
[DEBUG]   (f) optimize = false
[DEBUG]   (f) outputDirectory = F:\workspace\test\target\classes
[DEBUG]   (f) projectArtifact = test:test:jar:0.0.1-SNAPSHOT
[DEBUG]   (f) showDeprecation = false
[DEBUG]   (f) showWarnings = false
[DEBUG]   (f) skipMultiThreadWarning = false
[DEBUG]   (f) source = 1.6
[DEBUG]   (f) staleMillis = 0
[DEBUG]   (f) target = 1.6
[DEBUG]   (f) useIncrementalCompilation = true
[DEBUG]   (f) verbose = false
[DEBUG]   (f) mavenSession = org.apache.maven.execution.MavenSession@5364
[DEBUG]   (f) session = org.apache.maven.execution.MavenSession@5364
[DEBUG] -- end configuration --
[DEBUG] Using compiler 'javac'.


编译的错误日志如下:。。。。(众多debug信息)
[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] 
/F:/workspace/*/src/main/java/com/*/platform/dao/hibernate/UserInfoDao.java:
[60,59] 
F:\workspace\*\src\main\java\com\*\platform\dao\hibernate\UserInfoDaoHiberna
te.java:60: 不兼容的类型
找到: java.lang.Object

需要: java.util.List<User>


根据日志,显然是第60行的this.getHibernateTemplate().execute返回值为Object,而整个方法的返回值类型为List<Mapping>,类型不兼容。 

但为什么之前不是maven工程就没问题呢???


然后忽然想起maven的文档上说,maven支持多种编译器。 那么很有可能是因为编译器版本不同导致的。

文档 上如是说

Plexus Compiler component has some other compiler Ids available under the groupId org.codehaus.plexus:

  • aspectj with artifactId plexus-compiler-aspectj.
  • csharp with artifactId plexus-compiler-csharp.
  • eclipse with artifactId plexus-compiler-eclipse.
  • jikes with artifactId plexus-compiler-jikes.
各种编译器具有不同的行为,这确实是一个原因。不过重点在于,这段代码的风骚到底在哪里呢?


继续根据日志信息分析代码,

1、返回类型为Object,意味着execute方法返回值为Object,

2、但execute方法的返回值是由回调HibernateCallback决定的。

3、仔细看HibernateCallback的doInHibernate方法返回的就是Object类型。问题就在这里了。


HibernateCallback是框架的代码,要支持返回任何类型。而这没有指定泛型,故返回值为Object。骚点找到了,new HibernateCallback(){}改成new HibernateCallbacknew HibernateCallback<List<Mapping>>(){}就行了。


折腾了半天,教训有两个:

1、使用IDE的提示功能绝对不会出现这样的问题,IDE会不断提示添加泛型信息;

2、不管用什么,官方说明文档还是要好好看。这要是接个插座,不看文档,弄不好把家里电器就烧了。


0 0