2016-06-30 8 views
0

ID、名前、テキスト、および単語のリストを持つドキュメントクラスにアクセスしようとしています。私はIDと私が持っているIDを比較しようとすると、正確な単語を見つけるためにこのIDに付けられた単語のリストを見つけたら見つけました。私はJavaの反射で試していたが、私はそれを働かせることができないのですか?
ご協力いただきまして誠にありがとうございます。Javaリフレクションでクラスのフィールドにアクセスする

public class Doc { 
    private static int documentID; 
    private static Doc docInstance = null; 
    private String documentText; 
    private ArrayList<String> listOfTokens; 
    static int docCount = 0; 

    public Doc() { 
     documentID = 0; 
     listOfTokens = new ArrayList<String>(); 
     tokFreq = 0; 
     docCount++; 
    } 

    public static Doc getDocInstance() { 
     if (docInstance == null) { 
      docInstance = new Doc(); 
     } 
     return docInstance; 
    } 

    public ArrayList<String> getListOfTokens() { 
     return listOfTokens; 
    } 
} 

と、私はこの

public static void createDocumentVector(TreeMap<Integer,Integer> 
documentVector, TreeMap<String, ArrayList<Integer>>qm, int N) throws 
NoSuchFieldException, SecurityException, IllegalArgumentException, 
IllegalAccessException, InstantiationException, NoSuchMethodException, 
InvocationTargetException 
{ 
    int eachDoc = 0; 

    Collection<String> allKeys = qm.keySet(); 
    ArrayList<Integer> l1 = new ArrayList<Integer>(); 
    boolean addedTerm = false; 

    /** 
    Obtain an Iterator for Collection 
    */ 
    Iterator<String> itr = allKeys.iterator(); 
    String key; 
    int termFrequency = 0; 
    int documentFrequency = 0; 

    /** 
    Iterate through TreeMap values iterator 
    */ 
    while(itr.hasNext()) 
    { 
     key = (String)itr.next(); 
     Integer LL = 0; 
     l1 = qm.get(key); // Returns value of that key 
     for (int k = 0; k < l1.size(); k++) 
     { 
      LL = l2.get(k); 

      Doc obj = new Doc(); 
      Class<? extends Doc> docOb = obj.getClass(); 
      Field field1 = docOb.getDeclaredField("documentID"); 
      field1.setAccessible(true); 
      Field field2 = docOb.getDeclaredField("listOfTokens"); 
      field1.setAccessible(true); 

      if (field1.isAccessible()) { 
       Method setID = docOb.getDeclaredMethod("setDocumentID", new Class[]{int.class}); 
       setID.setAccessible(true); 
       setID.invoke(docOb, LL); 
      } 

      Method listTock = docOb.getMethod("getListOfTokens"); 
      ArrayList<String> per = (ArrayList<String>) listTock.invoke(docOb, null); 
      for (String tock : per) { 
       if(tock.equals(key)) { 
        termFrequency++; 
       } 
      } 

      documentFrequency = l1.size(); 
      eachDoc.add(getTFIDF(termFrequency, documentFrequency, N)); 
      documentVector.put(eachDoc, LL); 
      addedTerm = true; 
     } 


    } 
} 

をしようとしていますそして、私はこのエラー

Exception in thread "main" java.lang.IllegalArgumentException: object is not an instance of declaring class 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
at java.lang.reflect.Method.invoke(Unknown Source) 
+0

[codereview.stackexchange.com](http://codereview.stackexchange.com)のコードレビューを頼むのは良い考えかもしれませんが、私は非常に珍しいことをいくつか見ていますあなたが投稿したコード。あなたが見ているエラーに固有のものではなく、一般的なものです。 –

+0

この単純なタスクでリフレクションを使用する理由は何ですか?あなたは明示的に 'Doc'インスタンスを作成しています。 –

+0

OK、多分私はここでReflectionを実装しません!どのような提案???、docクラスは、すべてのドキュメントIDを保持するものがないため。たぶん私はdocのidとtockensのリストを格納するhashmapを追加する必要がありますか? – Bebo

答えて

3

TreeMap<List<Integer>,Integer>が原因のjavadocでこの説明に、エラーが発生し得ます。

マップは、そのキー

の{@linkplain匹敵天然 順序付け}に従ってソートされているが、ComparableインタフェースをList実装していません。だからTreeMapList<Integer>をキーとして使用することはできません。

私の英語は貧しい、あなたが理解できることを願っています!

+0

私はそれを知っていますが、私はそれを変更しましたが、まだ同じエラーが発生しています!どんな提案もお願いします.. – Bebo

0

Docの完全定義を投稿しましたか? setDocumentIDメソッドがありません。

とにかく、私は次のコードを使用してエラーを再現することができた:

public class DocReflection { 

    private static int documentId; // static field 

    private void setDocumentId(int docId) { // but setter is not static. 
     documentId = docId; 
    } 


    public static void main(String [] args) throws NoSuchFieldException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { 
     DocReflection docReflection = new DocReflection(); 
     Class<? extends DocReflection> cls = docReflection.getClass(); 
     Field docId = cls.getDeclaredField("documentId"); 
     docId.setAccessible(true); 

     if (docId.isAccessible()) { 
      Method setId = cls.getDeclaredMethod("setDocumentId", new Class[]{int.class}); 
      setId.setAccessible(true); 
      setId.invoke(cls, 1); // <-- Invoking non-static method with class object. 
     } 
    } 
} 

出力:

Exception in thread "main" java.lang.IllegalArgumentException: object is not an instance of declaring class 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:498) 
    at com.company.DocReflection.main(DocReflection.java:28) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:498) 
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144) 

ソリューションsetDocumentId静的を作ることです。

関連する問題