Getting ClassCastException Error when trying to access DOM

I am trying to parse the res/layouts/main.xml file in Android project. So following instructions of http://confluence.jetbrains.com/display/IDEADEV/Accessing+XML+through+IntelliJ+IDEA+DOM , But I keep getting ClassCastException errors, specifically

interface Root extends LayoutViewElement /*LayoutViewElement*/
{
    TextView getTextView();
}


interface TextView extends com.intellij.util.xml.DomElement
{
    List<AndroidText> getAndroidText();
}

interface AndroidText extends com.intellij.util.xml.DomElement
{
    String getValue();
}




        Root rootElement = (Root) manager.getFileElement((XmlFile) manager.getFileElement((com.intellij.psi.xml.XmlFile) psiFile).getRootElement();
        TextView textView = rootElement.getTextView();
        List<AndroidText> androidTexts = textView.getAndroidText();
        for (AndroidText androidText : androidTexts)
        {
            String attributeValue = androidText.getValue();
            if(attributeValue.compareTo("Hello Rwanda") == 0)
                return true;
        }



[  63277]  ERROR - llij.ide.plugins.PluginManager - org.jetbrains.android.dom.layout.LayoutViewElement$$EnhancerByCGLIB$$5a17eacd cannot be cast to com.xxx.xxx.test.Root
java.lang.ClassCastException: org.jetbrains.android.dom.layout.LayoutViewElement$$EnhancerByCGLIB$$5a17eacd cannot be cast to com.xxx.xxx.test.Root
     at com.xxx.xxx.test.Tutorial1.checkTextViewString(Tutorial1.java:81)
     at com.xxx.xxx.test.Tutorial1$AnotherActionListener.actionPerformed(Tutorial1.java:29)
     at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
     at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
     at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
     at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
     at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
     at java.awt.Component.processMouseEvent(Component.java:6297)
     at javax.swing.JComponent.processMouseEvent(JComponent.java:3275)
     at java.awt.Component.processEvent(Component.java:6062)
     at java.awt.Container.processEvent(Container.java:2039)
     at java.awt.Component.dispatchEventImpl(Component.java:4660)
     at java.awt.Container.dispatchEventImpl(Container.java:2097)
     at java.awt.Component.dispatchEvent(Component.java:4488)
     at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4575)
     at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4236)
     at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4166)
     at java.awt.Container.dispatchEventImpl(Container.java:2083)
     at java.awt.Window.dispatchEventImpl(Window.java:2489)
     at java.awt.Component.dispatchEvent(Component.java:4488)
     at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:674)
     at java.awt.EventQueue.access$400(EventQueue.java:81)
     at java.awt.EventQueue$2.run(EventQueue.java:633)
     at java.awt.EventQueue$2.run(EventQueue.java:631)
     at java.security.AccessController.doPrivileged(Native Method)
     at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
     at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
     at java.awt.EventQueue$3.run(EventQueue.java:647)
     at java.awt.EventQueue$3.run(EventQueue.java:645)
     at java.security.AccessController.doPrivileged(Native Method)
     at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
     at java.awt.EventQueue.dispatchEvent(EventQueue.java:644)
     at com.intellij.ide.IdeEventQueue.defaultDispatchEvent(IdeEventQueue.java:697)
     at com.intellij.ide.IdeEventQueue._dispatchEvent(IdeEventQueue.java:520)
     at com.intellij.ide.IdeEventQueue.dispatchEvent(IdeEventQueue.java:335)
     at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
     at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
     at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
     at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
     at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
     at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
[  63284]  ERROR - llij.ide.plugins.PluginManager - IntelliJ IDEA (Community Edition) 13.0.3  Build #IC-133.SNAPSHOT
[  63284]  ERROR - llij.ide.plugins.PluginManager - JDK: 1.6.0_45
[  63284]  ERROR - llij.ide.plugins.PluginManager - VM: Java HotSpot(TM) 64-Bit Server VM
[  63285]  ERROR - llij.ide.plugins.PluginManager - Vendor: Sun Microsystems Inc.
[  63285]  ERROR - llij.ide.plugins.PluginManager - OS: Linux
[  63285]  ERROR - llij.ide.plugins.PluginManager - Last Action:  
[  73587]   WARN - api.vfs.impl.local.FileWatcher - Watcher terminated with exit code 0
[  73603]  ERROR - til.concurrency.QueueProcessor - java.lang.InterruptedException
com.intellij.openapi.application.RuntimeInterruptedException: java.lang.InterruptedException
     at com.intellij.openapi.application.impl.ApplicationImpl.runReadAction(ApplicationImpl.java:905)
     at com.intellij.openapi.vcs.changes.VcsDirtyScopeManagerImpl.filePathsDirty(VcsDirtyScopeManagerImpl.java:161)
     at com.intellij.openapi.vcs.changes.VcsDirtyScopeVfsListener$FileAndDirsCollector.markDirty(VcsDirtyScopeVfsListener.java:251)
     at com.intellij.openapi.vcs.changes.VcsDirtyScopeVfsListener$FileAndDirsCollector.access$300(VcsDirtyScopeVfsListener.java:196)
     at com.intellij.openapi.vcs.changes.VcsDirtyScopeVfsListener$1.run(VcsDirtyScopeVfsListener.java:92)
     at com.intellij.openapi.vcs.ZipperUpdater$1.run(ZipperUpdater.java:64)
     at com.intellij.util.concurrency.QueueProcessor.runSafely(QueueProcessor.java:238)
     at com.intellij.util.Alarm$Request$1.run(Alarm.java:297)
     at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:439)
     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
     at java.util.concurrent.FutureTask.run(FutureTask.java:138)
     at com.intellij.util.concurrency.QueueProcessor$RunnableConsumer.consume(QueueProcessor.java:298)
     at com.intellij.util.concurrency.QueueProcessor$RunnableConsumer.consume(QueueProcessor.java:295)
     at com.intellij.util.concurrency.QueueProcessor$2$1.run(QueueProcessor.java:110)
     at com.intellij.util.concurrency.QueueProcessor.runSafely(QueueProcessor.java:238)
     at com.intellij.util.concurrency.QueueProcessor$2.consume(QueueProcessor.java:107)
     at com.intellij.util.concurrency.QueueProcessor$2.consume(QueueProcessor.java:104)
     at com.intellij.util.concurrency.QueueProcessor$3$1.run(QueueProcessor.java:215)
     at com.intellij.util.concurrency.QueueProcessor.runSafely(QueueProcessor.java:238)
     at com.intellij.util.concurrency.QueueProcessor$3.run(QueueProcessor.java:212)
     at com.intellij.openapi.application.impl.ApplicationImpl$8.run(ApplicationImpl.java:420)
     at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:439)
     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
     at java.util.concurrent.FutureTask.run(FutureTask.java:138)
     at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
     at java.lang.Thread.run(Thread.java:662)
     at com.intellij.openapi.application.impl.ApplicationImpl$1$1.run(ApplicationImpl.java:150)
Caused by: java.lang.InterruptedException
     at java.util.concurrent.locks.AbstractQueuedSynchronizer.doAcquireSharedInterruptibly(AbstractQueuedSynchronizer.java:979)
     at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireSharedInterruptibly(AbstractQueuedSynchronizer.java:1281)
     at java.util.concurrent.locks.ReentrantReadWriteLock$ReadLock.lockInterruptibly(ReentrantReadWriteLock.java:639)
     at com.intellij.openapi.application.impl.ApplicationImpl.runReadAction(ApplicationImpl.java:902)
     ... 27 more
[  73605]  ERROR - til.concurrency.QueueProcessor - IntelliJ IDEA (Community Edition) 13.0.3  Build #IC-133.SNAPSHOT
[  73605]  ERROR - til.concurrency.QueueProcessor - JDK: 1.6.0_45
[  73605]  ERROR - til.concurrency.QueueProcessor - VM: Java HotSpot(TM) 64-Bit Server VM
[  73605]  ERROR - til.concurrency.QueueProcessor - Vendor: Sun Microsystems Inc.
[  73605]  ERROR - til.concurrency.QueueProcessor - OS: Linux
Disconnected from the target VM, address: '127.0.0.1:39491', transport: 'socket'

Process finished with exit code 0

2 comments

LayoutViewElement is registered as root element via ViewLayoutDomFileDescription. What is "Root" for? What are you trying to achieve/extend here?

0

Hi,
Thanks for reply,

I am trying to parse the file in res/layouts/main.xml in Android projecct. In the current example i want read the android:text attribute in TextView element. I have been browsing the github code of other projects and plugins and I have modified my code as such :

interface LinearLayoutElement extends DomElement
{
    TextView getTextView();
}

interface TextView extends DomElement
{
    AndroidText getAndroidText();
}

interface AndroidText extends DomElement
{
    String getValue();

    AndroidAttributeValue<String> getText();
}



DomManager manager = DomManager.getDomManager(project);

LinearLayoutElement linearLayoutElement = (LinearLayout) manager.getFileElement((com.intellij.psi.xml.XmlFile) psiFile).getRootElement();xtView textView = linearLayoutElement.getTextView();

AndroidText androidText = textView.getAndroidText();
String attributeValue = androidText.getText().getStringValue();
if(attributeValue.compareTo("Hello World!") == 0)
return true;


I am currently getting attributeValue returned as null though.

0

Please sign in to leave a comment.