尝试向Map <string,object>

时间:2016-01-11 06:45:28

标签: java instrumentation javassist

您好我正在尝试在仪器中创建并填充类型的地图。以前我使用过Map,它工作正常。但现在我想根据情况在该地图中添加任何类型的值。因此,我编写了如下的仪器代码。

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(");");
        }

我一直在尝试将值添加为&lt;&#34; query_4&#34;,34&gt;。但它正在抛出以下异常。

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)

为什么我会收到此异常。据我所知,我们可以向Object类型变量添加任何类型的值。但为什么它会抛出异常?

1 个答案:

答案 0 :(得分:0)

正如Jon Skeet在上面的评论中提到的,看起来您需要将int打包以获得IntegerObject的子类型)。你可以致电Integer.valueOf(int)来做到这一点。所以,我想改变这个:

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

到此:

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