I'm trying to access a document class that has id, name, text and list of words. I try to compare id which I have with the ids and when found get the list of words attached to this id to find exact word. I was trying with java reflection but I'm unable to get it working?
Any help is highly appreciated.
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; } } and I am trying this
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; } } } And I get this error
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)
Docinstance after all.