Code cleanup.
diff --git a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/model/util/Protobufs.java b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/model/util/Protobufs.java
new file mode 100644
index 0000000..4c8be6d
--- /dev/null
+++ b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/model/util/Protobufs.java
@@ -0,0 +1,32 @@
+/*
+ * Copyright (c) 2011 Google Inc.
+ *
+ * All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse
+ * Public License v1.0 which accompanies this distribution, and is available at
+ *
+ * http://www.eclipse.org/legal/epl-v10.html
+ */
+package com.google.eclipse.protobuf.model.util;
+
+import com.google.eclipse.protobuf.parser.NonProto2;
+import com.google.eclipse.protobuf.protobuf.Protobuf;
+import com.google.inject.Singleton;
+
+/**
+ * Utility methods related to <code>{@link Protobuf}</code>s.
+ *
+ * @author alruiz@google.com (Alex Ruiz)
+ */
+@Singleton
+public class Protobufs {
+
+ /**
+ * Indicates whether the given <code>{@link Protobuf}</code> is not {@code null} and has "proto2" syntax.
+ * @param protobuf the {@code Protobuf} to check.
+ * @return {@code true} if the given <code>{@link Protobuf}</code> is not {@code null} and has "proto2" syntax,
+ * {@code false} otherwise.
+ */
+ public boolean isProto2(Protobuf protobuf) {
+ return protobuf != null && !(protobuf instanceof NonProto2);
+ }
+}
diff --git a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/AstWalker.java b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/AstWalker.java
index 0c680c4..62db31a 100644
--- a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/AstWalker.java
+++ b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/AstWalker.java
@@ -19,7 +19,6 @@
import org.eclipse.xtext.resource.IEObjectDescription;
import com.google.eclipse.protobuf.model.util.*;
-import com.google.eclipse.protobuf.parser.NonProto2;
import com.google.eclipse.protobuf.protobuf.*;
import com.google.eclipse.protobuf.protobuf.Package;
import com.google.inject.Inject;
@@ -32,6 +31,7 @@
@Inject private ModelFinder modelFinder;
@Inject private Imports imports;
@Inject private Packages packages;
+ @Inject private Protobufs protobufs;
@Inject private Resources resources;
Collection<IEObjectDescription> traverseAst(EObject start, ScopeFinder scopeFinder, Object criteria) {
@@ -86,7 +86,7 @@
Resource imported = resources.importedResource(anImport, resourceSet);
if (imported == null) continue;
Protobuf rootOfImported = modelFinder.rootOf(imported);
- if (rootOfImported instanceof NonProto2) continue;
+ if (!protobufs.isProto2(rootOfImported)) continue;
if (rootOfImported != null) {
descriptions.addAll(publicImported(rootOfImported, scopeFinder, criteria));
if (arePackagesRelated(fromImporter, rootOfImported)) {
@@ -101,7 +101,7 @@
}
private Collection<IEObjectDescription> publicImported(Protobuf start, ScopeFinder scopeFinder, Object criteria) {
- if (start instanceof NonProto2) return emptySet();
+ if (!protobufs.isProto2(start)) return emptySet();
List<Import> allImports = modelFinder.publicImportsIn(start);
if (allImports.isEmpty()) return emptyList();
ResourceSet resourceSet = start.eResource().getResourceSet();
diff --git a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/validation/ImportValidator.java b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/validation/ImportValidator.java
index 099db1c..08f5396 100644
--- a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/validation/ImportValidator.java
+++ b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/validation/ImportValidator.java
@@ -20,24 +20,24 @@
import org.eclipse.xtext.validation.*;
import com.google.eclipse.protobuf.model.util.*;
-import com.google.eclipse.protobuf.parser.NonProto2;
import com.google.eclipse.protobuf.protobuf.*;
import com.google.inject.Inject;
/**
* Verifies that imports only refer to "proto2" files.
- *
+ *
* @author alruiz@google.com (Alex Ruiz)
*/
public class ImportValidator extends AbstractDeclarativeValidator {
@Inject private ModelFinder finder;
+ @Inject private Protobufs protobufs;
@Inject private Resources resources;
@Override public void register(EValidatorRegistrar registrar) {}
/**
- * Verifies that the imports in the given root only refer to "proto2" files. If non-proto2 imports are found, this
+ * Verifies that the imports in the given root only refer to "proto2" files. If non-proto2 imports are found, this
* validator creates warning markers for such imports.
* @param root the root containing the imports to check.
*/
@@ -48,7 +48,7 @@
private void warnIfNonProto2ImportsFound(Resource resource) {
Protobuf root = finder.rootOf(resource);
- if (isNotProto2(root)) return;
+ if (!protobufs.isProto2(root)) return;
ResourceSet resourceSet = resource.getResourceSet();
boolean hasNonProto2 = false;
List<Pair<Import, Resource>> resourcesToCheck = new ArrayList<Pair<Import, Resource>>();
@@ -57,7 +57,7 @@
for (Import anImport : finder.importsIn(root)) {
Resource imported = resources.importedResource(anImport, resourceSet);
checked.add(imported.getURI());
- if (isNotProto2(finder.rootOf(imported))) {
+ if (!isProto2(imported)) {
hasNonProto2 = true;
warnNonProto2ImportFoundIn(anImport);
continue;
@@ -75,14 +75,12 @@
private boolean hasNonProto2(Pair<Import, Resource> toCheck, Set<URI> checked, ResourceSet resourceSet) {
Protobuf root = finder.rootOf(toCheck.getSecond());
- if (isNotProto2(root)) return false;
+ if (!protobufs.isProto2(root)) return false;
List<Pair<Import, Resource>> resourcesToCheck = new ArrayList<Pair<Import, Resource>>();
for (Import anImport : finder.importsIn(root)) {
Resource imported = resources.importedResource(anImport, resourceSet);
if (checked.contains(imported.getURI())) continue;
- if (isNotProto2(finder.rootOf(imported))) {
- return true;
- }
+ if (!isProto2(imported)) return true;
resourcesToCheck.add(pair(toCheck.getFirst(), imported));
}
for (Pair<Import, Resource> p : resourcesToCheck) {
@@ -91,8 +89,8 @@
return false;
}
- private boolean isNotProto2(Protobuf root) {
- return root instanceof NonProto2;
+ private boolean isProto2(Resource r) {
+ return protobufs.isProto2(finder.rootOf(r));
}
private void warnNonProto2ImportFoundIn(Import anImport) {
diff --git a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/validation/ProtobufJavaValidator.java b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/validation/ProtobufJavaValidator.java
index 2b24b50..4009bed 100644
--- a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/validation/ProtobufJavaValidator.java
+++ b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/validation/ProtobufJavaValidator.java
@@ -20,7 +20,6 @@
import org.eclipse.xtext.validation.*;
import com.google.eclipse.protobuf.model.util.*;
-import com.google.eclipse.protobuf.parser.NonProto2;
import com.google.eclipse.protobuf.protobuf.*;
import com.google.eclipse.protobuf.protobuf.Package;
import com.google.inject.Inject;
@@ -37,16 +36,17 @@
@Inject private FieldOptions fieldOptions;
@Inject private IndexedElements indexedElements;
- @Inject private ImportUriResolver uriResolver;
- @Inject private IQualifiedNameProvider qualifiedNameProvider;
@Inject private Fields properties;
+ @Inject private Protobufs protobufs;
+ @Inject private IQualifiedNameProvider qualifiedNameProvider;
+ @Inject private ImportUriResolver uriResolver;
@Check public void checkIsProto2(Protobuf protobuf) {
- if (protobuf instanceof NonProto2) {
+ if (!protobufs.isProto2(protobuf)) {
warning(nonProto2, null);
}
}
-
+
@Check public void checkDefaultValueType(FieldOption option) {
if (!fieldOptions.isDefaultValueOption(option)) return;
MessageField field = (MessageField) option.eContainer();
@@ -60,12 +60,12 @@
error(expectedTrueOrFalse, FIELD_OPTION__VALUE);
}
}
-
+
@Check public void checkImportIsResolved(Import anImport) {
if (retryUntilItIsResolved(anImport)) return;
error(format(importNotFound, anImport.getImportURI()), IMPORT__IMPORT_URI);
}
-
+
private boolean retryUntilItIsResolved(Import anImport) {
if (isResolved(anImport)) return true;
uriResolver.apply(anImport);