本文介绍了java.lang.VerifyError函数调用的对象参数不兼容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在编写一些Java代码时,我遇到了一个我无法识别的异常java.lang.VerifyError。一些谷歌搜索表明这通常是jvm / javac错误,我很好奇我的情况。

While writing some java code I run across an Exception that I did not recognise, the java.lang.VerifyError. Some googling indicated that this is often an jvm/javac bug and I'm curious if my case is.

我怀疑的行是

private Pair<Integer/*used size*/,Pair<K,V[]>[]>[] map=(Pair<Integer,Pair<K,V[]>[]>[])Array.newInstance(Pair.class,63);//good start number

map[b]=new Pair<Integer,Pair<K,V[]>[]>(7,(Pair<K,V[]>[])Array.newInstance(Pair.class,7));

但我不确定。

这是编译器错误还是我的代码有问题?

Is this a compiler bug or is my code at fault.

这些行是我在某处发现的泛型数组创建数组失败的解决方法。

Those lines are workarounds for the failure of array creation for arrays of generics that I found somewhere.

已附加代码。

package osm2spacebook;

import java.util.Iterator;
import java.lang.reflect.Array;
import java.util.NoSuchElementException;

public class MultiMap<K,V> implements Iterable<K>{
    private int num_keys;
    @SuppressWarnings("unchecked")
    private Pair<Integer/*used size*/,Pair<K,V[]>[]>[] map=(Pair<Integer,Pair<K,V[]>[]>[])Array.newInstance(Pair.class,63);//good start number
    @SuppressWarnings("unchecked")
    private int bucket(K key){//position in bucket
        int h=key.hashCode();
        int b=h%map.length;
        if(map[b]==null)
            map[b]=new Pair<Integer,Pair<K,V[]>[]>(7,(Pair<K,V[]>[])Array.newInstance(Pair.class,7));
        return b;
    }
    private int position(K key){//position within bucket
        int b=bucket(key);//IMPORTANT this must use the buket function to obtain this otherwise it is a race
        for(int i=0;i<map[b].v1;i++)
            if(map[b].v2[i].v1==key)
                return i;
        if(map[b].v1==map[b].v2.length)
            map[b].v2=java.util.Arrays.copyOf(map[b].v2,map[b].v1*2);
        return map[b].v1++;
    }
    public V put(K key,V value){
        Pair<K,V[]> m=map[bucket(key)].v2[position(key)];
        for(int i=0;i<m.v2.length;i++)
            if(m.v2[i]==value)
                return value;
        m.v2=java.util.Arrays.copyOf(m.v2,m.v2.length+1);
        return m.v2[m.v2.length-1]=value;
    }
    public V[] get(K key){
        V[] v=map[bucket(key)].v2[position(key)].v2;
        return java.util.Arrays.copyOf(v,v.length);
    }
    public V[] remove(K key){
        throw new UnsupportedOperationException("Not implemented"); //TODO
    }
    public V remove(K key,V value){
        throw new UnsupportedOperationException("Not implemented"); //TODO
    }
    public boolean contains(K key){
        return position(key)<map[bucket(key)].v1;
    }
    public int numKeys(){
        return num_keys;
    }
    public Iterator<K> iterator(){
        return new Iterator<K>(){
            int bucket=0;
            int position=0;
            public boolean hasNext(){
                while(bucket<map.length){
                    if(map[bucket]!=null)
                        if(position<map[bucket].v1)
                            return true;
                        else
                            position=0;
                    bucket++;
                }
                return false;
            }
            public K next(){
                if(hasNext())//positions cursor on next element if any
                    return map[bucket].v2[position++].v1;//updates position after read
                else
                    throw new NoSuchElementException();
            }
            public void remove(){
                throw new UnsupportedOperationException("Remove not supported in multimap iterator du to ambiguity");
            }
        };
    }
}

以及依赖的Pair类

package osm2spacebook;

public class Pair<T1,T2>{
    public T1 v1;
    public T2 v2;
    public Pair(T1 t1,T2 t2){
        v1=t1;
        v2=t2;
    }
}

完整的错误消息

Exception in thread "main" java.lang.VerifyError: (class: osm2spacebook/MultiMap, method: position signature: (Ljava/lang/Object;)I) Incompatible object argument for function call
    at osm2spacebook.SqlOutput.<init>(SqlOutput.java:64)
    at osm2spacebook.OsmImport.<init>(OsmImport.java:142)
    at osm2spacebook.OsmImport.main(OsmImport.java:280)

SqlOutput的第64行如下

Line 64 of SqlOutput is the following

private MultiMap<Integer,Integer> edge_index=new MultiMap<Integer,Integer>();


推荐答案

VerifyError通常意味着您已加载到类文件中某种形式的格式错误或引用了另一个已更改的类文件,这种更改已导致另一个类文件中的代码不再有效。例如,如果您编译了一个引用其他类中的方法的类文件,然后在更改该方法的签名后独立修改并重新编译了第二类,则会出现这种错误。

A VerifyError usually means that you loaded in a class file that is somehow malformed or which references another class file that has changed in a way that causes the code in another class file to no longer be valid. For example, if you compiled a class file that referenced a method in some other class, then independently modified and recompiled the second class after altering that method's signature, you'd get this sort of error.

我建议做一个干净的构建,看看这个问题是否消失了。如果不是,请检查您是否正在使用最新的JAR和源文件。

I'd suggest doing a clean build and seeing if this issue goes away. If not, check whether you're using up-to-date JARs and source files.

这篇关于java.lang.VerifyError函数调用的对象参数不兼容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 15:26