Skip to main content



      Home
Home » Language IDEs » Java Development Tools (JDT) » getAllTypes() does not return Class in an ICompilationUnit(Getting an Exception instead)
getAllTypes() does not return Class in an ICompilationUnit [message #532206] Fri, 07 May 2010 10:18
Eclipse UserFriend
Hi,

I want to process deletion events in a Java Project to keep a corresponding UML diagram in sync.

For this purpose I have implemented an IElementChangedListener. When a Java-File is deleted I want to inform my model (the UML diagram document), that all types from that Java file have to be removed from the document.

What happens instead is that when I want to access the types in the ICompilationUnit via getAllTypes() I get an Exception. That is because the File and therefor the Compilation Unit and all Types in it are already long gone and I'm trying to accessing non-existing elements (processCompilationUnitChange method below).

How do I get access to the names of the deleted types then? They are not seperately listed in the array returned by getAffectedChildren either (that array is empty), so I'm pretty lost.

Any help would be highly appreciated.
Source code and the interesting part of the stack trace follows below.

Regards,
Chris

	@Override
	public void elementChanged(ElementChangedEvent event) {
		processJavaElementDelta(event.getDelta());
	}
	
	/**
	 * Recursively process delta of a Java Element change event
	 * @param delta the Java Element change to process
	 */
	public void processJavaElementDelta(IJavaElementDelta delta){
		IJavaElement changedElement = delta.getElement();
		switch(changedElement.getElementType()){
			case IJavaElement.JAVA_MODEL:
			case IJavaElement.JAVA_PROJECT:
			case IJavaElement.PACKAGE_FRAGMENT_ROOT:
			case IJavaElement.PACKAGE_FRAGMENT:{
				processJavaElementDeltaChildren(delta.getAffectedChildren());
				break;
			}
			case IJavaElement.COMPILATION_UNIT:{
				processCompilationUnitChange(delta);
				break;
			}
			case IJavaElement.TYPE:{
				processTypeDeltaChange(delta);
				break;
			}
			case IJavaElement.METHOD:{
				processMethodChange(delta);
				break;
			}
			default:{
				break;
			}
		}
	}

	/**
	 * Recursively process delta children of a Java Element change event
	 * @param affectedChildren the changed children of the given changed element
	 */
	private void processJavaElementDeltaChildren(IJavaElementDelta[] affectedChildren) {
		for(IJavaElementDelta delta : affectedChildren){
			processJavaElementDelta(delta);
		}
	}

	/**
	 * Recursively process a change in a Java compilation unit
	 * @param delta the change in the compilation unit
	 */
	private void processCompilationUnitChange(IJavaElementDelta delta) {
		ICompilationUnit unit = (ICompilationUnit)delta.getElement();
		
		// perform different actions based on kind of change
		switch(delta.getKind()){
			// deletion
			case IJavaElementDelta.REMOVED:{
				try {
					for(IType type : unit.getAllTypes()){
						getCastedModel().removeType(type);
					}
				} catch (JavaModelException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				break;
			}
		}
		
		processJavaElementDeltaChildren(delta.getAffectedChildren());
	}
	
	/**
	 * Recursively process a change in a Java Type
	 * @param delta the change in the Java Type to process
	 */
	private void processTypeDeltaChange(IJavaElementDelta delta) {
		// perform different actions based on kind of change
		switch(delta.getKind()){
			// deletion
			case IJavaElementDelta.REMOVED:{
				getCastedModel().removeType((IType)delta.getElement());
				break;
			}
		}
		
		processJavaElementDeltaChildren(delta.getAffectedChildren());
	}

	/**
	 * Recursively process a change in a Java Method
	 * @param delta the change in the Java Method to process
	 */
	private void processMethodChange(IJavaElementDelta delta) {
		// TODO Does nothing yet
	}


Quote:
Java Model Exception: Java Model Status [DeletionCandidate.java [in com.superinc.sample [in src [in CgtTest]]] does not exist]
at org.eclipse.jdt.internal.core.JavaElement.newJavaModelExcept ion(JavaElement.java:502)
at org.eclipse.jdt.internal.core.Openable.generateInfos(Openabl e.java:246)
at org.eclipse.jdt.internal.core.JavaElement.openWhenClosed(Jav aElement.java:515)
at org.eclipse.jdt.internal.core.JavaElement.getElementInfo(Jav aElement.java:252)
at org.eclipse.jdt.internal.core.JavaElement.getElementInfo(Jav aElement.java:238)
at org.eclipse.jdt.internal.core.JavaElement.getChildren(JavaEl ement.java:193)
at org.eclipse.jdt.internal.core.JavaElement.getChildrenOfType( JavaElement.java:207)
at org.eclipse.jdt.internal.core.CompilationUnit.getTypes(Compi lationUnit.java:920)
at org.eclipse.jdt.internal.core.CompilationUnit.getAllTypes(Co mpilationUnit.java:605)
at edu.rwth.hci.codegestalt.controller.CgtDiagramEditPart.proce ssCompilationUnitChange(CgtDiagramEditPart.java:180)
at edu.rwth.hci.codegestalt.controller.CgtDiagramEditPart.proce ssJavaElementDelta(CgtDiagramEditPart.java:141)
at edu.rwth.hci.codegestalt.controller.CgtDiagramEditPart.proce ssJavaElementDeltaChildren(CgtDiagramEditPart.java:164)
at edu.rwth.hci.codegestalt.controller.CgtDiagramEditPart.proce ssJavaElementDelta(CgtDiagramEditPart.java:137)
at edu.rwth.hci.codegestalt.controller.CgtDiagramEditPart.proce ssJavaElementDeltaChildren(CgtDiagramEditPart.java:164)
at edu.rwth.hci.codegestalt.controller.CgtDiagramEditPart.proce ssJavaElementDelta(CgtDiagramEditPart.java:137)
at edu.rwth.hci.codegestalt.controller.CgtDiagramEditPart.proce ssJavaElementDeltaChildren(CgtDiagramEditPart.java:164)
at edu.rwth.hci.codegestalt.controller.CgtDiagramEditPart.proce ssJavaElementDelta(CgtDiagramEditPart.java:137)
at edu.rwth.hci.codegestalt.controller.CgtDiagramEditPart.proce ssJavaElementDeltaChildren(CgtDiagramEditPart.java:164)
at edu.rwth.hci.codegestalt.controller.CgtDiagramEditPart.proce ssJavaElementDelta(CgtDiagramEditPart.java:137)
at edu.rwth.hci.codegestalt.controller.CgtDiagramEditPart.eleme ntChanged(CgtDiagramEditPart.java:123)
Previous Topic:Impossible to change font in the Outline view in mac os X
Next Topic:source not found - Edit source lookup path...
Goto Forum:
  


Current Time: Sat Mar 15 21:17:10 EDT 2025

Powered by FUDForum. Page generated in 0.04034 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top