From 65d9c1413d4cee7d9917fbabe8f75c9a0364bd54 Mon Sep 17 00:00:00 2001 From: Sebastian Zarnekow Date: Fri, 9 Mar 2012 14:55:33 +0100 Subject: [PATCH] [xbase][compiler] Fix: NPE in compiler --- .../xtext/nodemodel/util/NodeModelUtils.java | 18 +++++++++++++----- .../xtext/xtext/XtextDiagnosticConverter.java | 6 ++++-- .../.settings/org.eclipse.jdt.core.prefs | 2 +- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/plugins/org.eclipse.xtext/src/org/eclipse/xtext/nodemodel/util/NodeModelUtils.java b/plugins/org.eclipse.xtext/src/org/eclipse/xtext/nodemodel/util/NodeModelUtils.java index f5e5f7706..c904d3aba 100644 --- a/plugins/org.eclipse.xtext/src/org/eclipse/xtext/nodemodel/util/NodeModelUtils.java +++ b/plugins/org.eclipse.xtext/src/org/eclipse/xtext/nodemodel/util/NodeModelUtils.java @@ -14,6 +14,8 @@ import java.util.List; import org.eclipse.emf.common.notify.Adapter; import org.eclipse.emf.ecore.EObject; import org.eclipse.emf.ecore.EStructuralFeature; +import org.eclipse.jdt.annotation.NonNull; +import org.eclipse.jdt.annotation.Nullable; import org.eclipse.xtext.AbstractRule; import org.eclipse.xtext.Action; import org.eclipse.xtext.Assignment; @@ -60,7 +62,8 @@ public class NodeModelUtils { * @param leafNodeOffset the offset that is covered by the searched node. * @return the leaf node at the given offset or null. */ - public static ILeafNode findLeafNodeAtOffset(INode node, int leafNodeOffset) { + @Nullable + public static ILeafNode findLeafNodeAtOffset(@NonNull INode node, int leafNodeOffset) { INode localNode = node; while(!(localNode instanceof AbstractNode)) { localNode = localNode.getParent(); @@ -115,7 +118,8 @@ public class NodeModelUtils { * @return the node that is directly associated with the given object. * @see NodeModelUtils#findActualNodeFor(EObject) */ - public static ICompositeNode getNode(EObject object) { + @Nullable + public static ICompositeNode getNode(@Nullable EObject object) { if (object == null) return null; List adapters = object.eAdapters(); @@ -132,6 +136,7 @@ public class NodeModelUtils { * * @return the list of nodes that were used to assign values to the given feature for the given object. */ + @NonNull public static List findNodesForFeature(EObject semanticObject, EStructuralFeature structuralFeature) { ICompositeNode node = findActualNodeFor(semanticObject); if (node != null) { @@ -205,7 +210,8 @@ public class NodeModelUtils { * @param semanticObject the semantic object whose node should be provided. * @return the node that covers all assigned values of the given object. */ - public static ICompositeNode findActualNodeFor(EObject semanticObject) { + @Nullable + public static ICompositeNode findActualNodeFor(@Nullable EObject semanticObject) { ICompositeNode node = getNode(semanticObject); if (node != null) { while (GrammarUtil.containingAssignment(node.getGrammarElement()) == null && node.getParent() != null && !node.getParent().hasDirectSemanticElement()) { @@ -222,7 +228,8 @@ public class NodeModelUtils { * * @return the semantic object that is really associated with the actual container node of the given node. */ - public static EObject findActualSemanticObjectFor(INode node) { + @Nullable + public static EObject findActualSemanticObjectFor(@Nullable INode node) { if (node == null) return null; if (node.hasDirectSemanticElement()) @@ -251,7 +258,8 @@ public class NodeModelUtils { return findActualSemanticObjectFor(node.getParent()); } - private static EObject findActualSemanticObjectInChildren(INode node, EObject grammarElement) { + @Nullable + private static EObject findActualSemanticObjectInChildren(@NonNull INode node, @Nullable EObject grammarElement) { if (node.hasDirectSemanticElement()) return node.getSemanticElement(); AbstractRule rule = null; diff --git a/plugins/org.eclipse.xtext/src/org/eclipse/xtext/xtext/XtextDiagnosticConverter.java b/plugins/org.eclipse.xtext/src/org/eclipse/xtext/xtext/XtextDiagnosticConverter.java index f5ea53aa8..969e3f3b6 100644 --- a/plugins/org.eclipse.xtext/src/org/eclipse/xtext/xtext/XtextDiagnosticConverter.java +++ b/plugins/org.eclipse.xtext/src/org/eclipse/xtext/xtext/XtextDiagnosticConverter.java @@ -33,8 +33,10 @@ public class XtextDiagnosticConverter extends DiagnosticConverterImpl{ ITextRegion location = locationInFileProvider.getSignificantTextRegion(obj); if (location != null) { ICompositeNode rootNode = NodeModelUtils.getNode(EcoreUtil.getRootContainer(obj)); - ILeafNode leafNode = NodeModelUtils.findLeafNodeAtOffset(rootNode, location.getOffset()); - return getLocationForNode(leafNode); + if (rootNode != null) { + ILeafNode leafNode = NodeModelUtils.findLeafNodeAtOffset(rootNode, location.getOffset()); + return getLocationForNode(leafNode); + } } else { return super.getLocationData(obj.eContainer(), null, index); } diff --git a/tests/org.eclipse.xtext.tests/.settings/org.eclipse.jdt.core.prefs b/tests/org.eclipse.xtext.tests/.settings/org.eclipse.jdt.core.prefs index 219724848..3e46a6376 100644 --- a/tests/org.eclipse.xtext.tests/.settings/org.eclipse.jdt.core.prefs +++ b/tests/org.eclipse.xtext.tests/.settings/org.eclipse.jdt.core.prefs @@ -21,7 +21,7 @@ org.eclipse.jdt.core.compiler.annotation.nonnull=org.eclipse.jdt.annotation.NonN org.eclipse.jdt.core.compiler.annotation.nonnullbydefault=org.eclipse.jdt.annotation.NonNullByDefault org.eclipse.jdt.core.compiler.annotation.nonnullisdefault=disabled org.eclipse.jdt.core.compiler.annotation.nullable=org.eclipse.jdt.annotation.Nullable -org.eclipse.jdt.core.compiler.annotation.nullanalysis=enabled +org.eclipse.jdt.core.compiler.annotation.nullanalysis=disabled org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5 org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve