2016-01-11 41 views
0

您好我正在嘗試創建和填充類型的儀表過程中的地圖。以前我用過地圖,它工作正常。但現在我想根據情況在該地圖中添加任何類型的值。所以我寫了如下的工具代碼。Javassist CannotCompileException當試圖將值添加到一個地圖<String,Object>

sBuilder.append("java.util.Map/*<String,Object>*/ "); 
     sBuilder.append(mapName); 
     sBuilder.append("= new java.util.HashMap/*<String,Object>*/();"); 
     for (ParameterName parameter : insertMapList) { 
      sBuilder.append(mapName); 
      sBuilder.append(".put(\""); 
      sBuilder.append(parameter.getKey()); 
      sBuilder.append("\","); 
      sBuilder.append(parameter.getParameterValue()); 
      sBuilder.append(");"); 
     } 

我一直在嘗試添加一個值爲<「query_4」,34>。但它拋出以下例外。

javassist.CannotCompileException: [source error] put(java.lang.String,int) not found in java.util.Map 
    at javassist.CtBehavior.insertBefore(CtBehavior.java:774) 
    at javassist.CtBehavior.insertBefore(CtBehavior.java:734) 
    at org.wso2.das.javaagent.instrumentation.InstrumentationClassTransformer.createInsertBefore(InstrumentationClassTransformer.java:179) 
    at org.wso2.das.javaagent.instrumentation.InstrumentationClassTransformer.instrumentMethod(InstrumentationClassTransformer.java:145) 
    at org.wso2.das.javaagent.instrumentation.InstrumentationClassTransformer.instrumentClass(InstrumentationClassTransformer.java:138) 
    at org.wso2.das.javaagent.instrumentation.InstrumentationClassTransformer.transform(InstrumentationClassTransformer.java:84) 
    at sun.instrument.TransformerManager.transform(TransformerManager.java:188) 
    at sun.instrument.InstrumentationImpl.transform(InstrumentationImpl.java:424) 
    at java.lang.ClassLoader.defineClass1(Native Method) 
    at java.lang.ClassLoader.defineClass(ClassLoader.java:800) 
    at org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader.defineClass(DefaultClassLoader.java:188) 
    at org.eclipse.osgi.baseadaptor.loader.ClasspathManager.defineClassHoldingLock(ClasspathManager.java:638) 
    at org.eclipse.osgi.baseadaptor.loader.ClasspathManager.defineClass(ClasspathManager.java:613) 
    at org.eclipse.osgi.baseadaptor.loader.ClasspathManager.findClassImpl(ClasspathManager.java:574) 
    at org.eclipse.osgi.baseadaptor.loader.ClasspathManager.findLocalClassImpl(ClasspathManager.java:492) 
    at org.eclipse.osgi.baseadaptor.loader.ClasspathManager.findLocalClass(ClasspathManager.java:465) 
    at org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader.findLocalClass(DefaultClassLoader.java:216) 
    at org.eclipse.osgi.internal.loader.BundleLoader.findLocalClass(BundleLoader.java:395) 
    at org.eclipse.osgi.internal.loader.BundleLoader.findClassInternal(BundleLoader.java:464) 
    at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:421) 
    at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:412) 
    at org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader.loadClass(DefaultClassLoader.java:107) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358) 
    at org.h2.Driver.connect(Driver.java:73) 
    at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:278) 
    at org.apache.tomcat.jdbc.pool.PooledConnection.connect(PooledConnection.java:182) 
    at org.apache.tomcat.jdbc.pool.ConnectionPool.createConnection(ConnectionPool.java:701) 
    at org.apache.tomcat.jdbc.pool.ConnectionPool.borrowConnection(ConnectionPool.java:635) 
    at org.apache.tomcat.jdbc.pool.ConnectionPool.getConnection(ConnectionPool.java:188) 
    at org.apache.tomcat.jdbc.pool.DataSourceProxy.getConnection(DataSourceProxy.java:128) 
    at org.wso2.carbon.user.core.claim.dao.ClaimDAO.getDialectCount(ClaimDAO.java:158) 
    at org.wso2.carbon.user.core.common.DefaultRealm.populateProfileAndClaimMaps(DefaultRealm.java:429) 
    at org.wso2.carbon.user.core.common.DefaultRealm.init(DefaultRealm.java:105) 
    at org.wso2.carbon.user.core.common.DefaultRealmService.initializeRealm(DefaultRealmService.java:262) 
    at org.wso2.carbon.user.core.common.DefaultRealmService.<init>(DefaultRealmService.java:99) 
    at org.wso2.carbon.user.core.common.DefaultRealmService.<init>(DefaultRealmService.java:112) 
    at org.wso2.carbon.user.core.internal.Activator.startDeploy(Activator.java:68) 
    at org.wso2.carbon.user.core.internal.BundleCheckActivator.start(BundleCheckActivator.java:61) 
    at org.eclipse.osgi.framework.internal.core.BundleContextImpl$1.run(BundleContextImpl.java:711) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at org.eclipse.osgi.framework.internal.core.BundleContextImpl.startActivator(BundleContextImpl.java:702) 
    at org.eclipse.osgi.framework.internal.core.BundleContextImpl.start(BundleContextImpl.java:683) 
    at org.eclipse.osgi.framework.internal.core.BundleHost.startWorker(BundleHost.java:381) 
    at org.eclipse.osgi.framework.internal.core.AbstractBundle.resume(AbstractBundle.java:390) 
    at org.eclipse.osgi.framework.internal.core.Framework.resumeBundle(Framework.java:1176) 
    at org.eclipse.osgi.framework.internal.core.StartLevelManager.resumeBundles(StartLevelManager.java:559) 
    at org.eclipse.osgi.framework.internal.core.StartLevelManager.resumeBundles(StartLevelManager.java:544) 
    at org.eclipse.osgi.framework.internal.core.StartLevelManager.incFWSL(StartLevelManager.java:457) 
    at org.eclipse.osgi.framework.internal.core.StartLevelManager.doSetStartLevel(StartLevelManager.java:243) 
    at org.eclipse.osgi.framework.internal.core.StartLevelManager.dispatchEvent(StartLevelManager.java:438) 
    at org.eclipse.osgi.framework.internal.core.StartLevelManager.dispatchEvent(StartLevelManager.java:1) 
    at org.eclipse.osgi.framework.eventmgr.EventManager.dispatchEvent(EventManager.java:230) 
    at org.eclipse.osgi.framework.eventmgr.EventManager$EventThread.run(EventManager.java:340) 
Caused by: compile error: put(java.lang.String,int) not found in java.util.Map 
    at javassist.compiler.TypeChecker.atMethodCallCore(TypeChecker.java:723) 
    at javassist.compiler.TypeChecker.atCallExpr(TypeChecker.java:688) 
    at javassist.compiler.JvstTypeChecker.atCallExpr(JvstTypeChecker.java:157) 
    at javassist.compiler.ast.CallExpr.accept(CallExpr.java:46) 
    at javassist.compiler.CodeGen.doTypeCheck(CodeGen.java:242) 
    at javassist.compiler.CodeGen.atStmnt(CodeGen.java:330) 
    at javassist.compiler.ast.Stmnt.accept(Stmnt.java:50) 
    at javassist.compiler.Javac.compileStmnt(Javac.java:569) 
    at javassist.CtBehavior.insertBefore(CtBehavior.java:754) 

爲什麼我得到這個異常。據我所知,我們可以將任何類型的值添加到對象類型變量。但爲什麼會拋出上述異常?

+1

看起來你需要框中的值。我不太瞭解Javassist以幫助你,但我一定會嘗試在文檔中找到拳擊... –

回答

0

正如Jon Skeet在上面的評論中提到的那樣,您似乎需要將int裝箱以獲得Integer(這是Object的子類型)。您可以致電Integer.valueOf(int)。所以,我想這個變化:

  sBuilder.append("\","); 
      sBuilder.append(parameter.getParameterValue()); 
      sBuilder.append(");"); 

這樣:

  sBuilder.append("\", Integer.valueOf("); 
      sBuilder.append(parameter.getParameterValue()); 
      sBuilder.append("));");