Fixed: [Issue 125] Support for custom options.
Code cleanup.
diff --git a/com.google.eclipse.protobuf.integration.test/src/com/google/eclipse/protobuf/model/util/Options_isExtendingOptionMessage_Test.java b/com.google.eclipse.protobuf.integration.test/src/com/google/eclipse/protobuf/model/util/Options_isExtendingOptionMessage_Test.java
deleted file mode 100644
index 38765fb..0000000
--- a/com.google.eclipse.protobuf.integration.test/src/com/google/eclipse/protobuf/model/util/Options_isExtendingOptionMessage_Test.java
+++ /dev/null
@@ -1,67 +0,0 @@
-/*
- * 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 static com.google.eclipse.protobuf.junit.core.Setups.integrationTestSetup;
-import static com.google.eclipse.protobuf.junit.core.XtextRule.createWith;
-import static com.google.eclipse.protobuf.model.OptionType.FILE;
-import static org.hamcrest.core.IsEqual.equalTo;
-import static org.junit.Assert.assertThat;
-import static org.mockito.Mockito.mock;
-
-import com.google.eclipse.protobuf.junit.core.XtextRule;
-import com.google.eclipse.protobuf.model.OptionType;
-import com.google.eclipse.protobuf.protobuf.*;
-
-import org.eclipse.emf.ecore.EObject;
-import org.junit.*;
-
-/**
- * Tests for <code>{@link Options#isExtendingOptionMessage(EObject, OptionType)}</code>
- *
- * @author alruiz@google.com (Alex Ruiz)
- */
-public class Options_isExtendingOptionMessage_Test {
-
- @Rule public XtextRule xtext = createWith(integrationTestSetup());
-
- private Options options;
-
- @Before public void setUp() {
- options = xtext.getInstanceOf(Options.class);
- }
-
- // import 'google/protobuf/descriptor.proto';
- //
- // extend google.protobuf.FileOptions {
- // optional string encoding = 1000;
- // }
- @Test public void should_return_true_if_name_of_extended_message_is_equal_to_message_name_in_OptionType() {
- ExtendMessage extend = xtext.find("FileOptions", ExtendMessage.class);
- boolean result = options.isExtendingOptionMessage(extend, FILE);
- assertThat(result, equalTo(true));
- }
-
- @Test public void should_return_false_if_given_EObject_is_not_ExtendMessage() {
- Import anImport = mock(Import.class);
- boolean result = options.isExtendingOptionMessage(anImport, FILE);
- assertThat(result, equalTo(false));
- }
-
- // import 'google/protobuf/descriptor.proto';
- //
- // extend google.protobuf.FieldOptions {
- // optional string encoding = 1000;
- // }
- @Test public void should_return_false_if_name_of_extended_message_is_not_equal_to_message_name_in_OptionType() {
- ExtendMessage extend = xtext.find("FieldOptions", ExtendMessage.class);
- boolean result = options.isExtendingOptionMessage(extend, FILE);
- assertThat(result, equalTo(false));
- }
-}
diff --git a/com.google.eclipse.protobuf.integration.test/src/com/google/eclipse/protobuf/scoping/ProtoDescriptor_enumTypeOf_Test.java b/com.google.eclipse.protobuf.integration.test/src/com/google/eclipse/protobuf/scoping/ProtoDescriptor_enumTypeOf_Test.java
index c4f3755..01f84f8 100644
--- a/com.google.eclipse.protobuf.integration.test/src/com/google/eclipse/protobuf/scoping/ProtoDescriptor_enumTypeOf_Test.java
+++ b/com.google.eclipse.protobuf.integration.test/src/com/google/eclipse/protobuf/scoping/ProtoDescriptor_enumTypeOf_Test.java
@@ -10,7 +10,7 @@
import static com.google.eclipse.protobuf.junit.core.Setups.integrationTestSetup;
import static com.google.eclipse.protobuf.junit.core.XtextRule.createWith;
-import static com.google.eclipse.protobuf.model.OptionType.FILE;
+import static com.google.eclipse.protobuf.scoping.OptionType.FILE;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.hamcrest.core.IsNull.nullValue;
import static org.junit.Assert.assertThat;
diff --git a/com.google.eclipse.protobuf.integration.test/src/com/google/eclipse/protobuf/scoping/ProtobufScopeProvider_scope_PropertyRef_property_Test.java b/com.google.eclipse.protobuf.integration.test/src/com/google/eclipse/protobuf/scoping/ProtobufScopeProvider_scope_PropertyRef_property_Test.java
index c66743d..9ab54a2 100644
--- a/com.google.eclipse.protobuf.integration.test/src/com/google/eclipse/protobuf/scoping/ProtobufScopeProvider_scope_PropertyRef_property_Test.java
+++ b/com.google.eclipse.protobuf.integration.test/src/com/google/eclipse/protobuf/scoping/ProtobufScopeProvider_scope_PropertyRef_property_Test.java
@@ -10,10 +10,10 @@
import static com.google.eclipse.protobuf.junit.core.Setups.integrationTestSetup;
import static com.google.eclipse.protobuf.junit.core.XtextRule.createWith;
-import static com.google.eclipse.protobuf.model.OptionType.*;
import static com.google.eclipse.protobuf.scoping.ContainAllNames.containAll;
import static com.google.eclipse.protobuf.scoping.ContainAllProperties.containAll;
import static com.google.eclipse.protobuf.scoping.IEObjectDescriptions.descriptionsIn;
+import static com.google.eclipse.protobuf.scoping.OptionType.*;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
diff --git a/com.google.eclipse.protobuf/META-INF/MANIFEST.MF b/com.google.eclipse.protobuf/META-INF/MANIFEST.MF
index b1316ee..db00741 100644
--- a/com.google.eclipse.protobuf/META-INF/MANIFEST.MF
+++ b/com.google.eclipse.protobuf/META-INF/MANIFEST.MF
@@ -23,7 +23,6 @@
Export-Package: com.google.eclipse.protobuf,
com.google.eclipse.protobuf.conversion,
com.google.eclipse.protobuf.grammar,
- com.google.eclipse.protobuf.model,
com.google.eclipse.protobuf.model.util,
com.google.eclipse.protobuf.parseTreeConstruction,
com.google.eclipse.protobuf.parser.antlr,
diff --git a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/model/util/Options.java b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/model/util/Options.java
index 608f3e8..de6cd8f 100644
--- a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/model/util/Options.java
+++ b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/model/util/Options.java
@@ -8,9 +8,6 @@
*/
package com.google.eclipse.protobuf.model.util;
-import org.eclipse.emf.ecore.EObject;
-
-import com.google.eclipse.protobuf.model.OptionType;
import com.google.eclipse.protobuf.protobuf.*;
import com.google.inject.Singleton;
@@ -39,38 +36,18 @@
}
/**
- * Returns the field of the <code>{@link Property}</code> the given <code>{@link CustomOption}</code> is referring to.
+ * Returns the field of the <code>{@link Property}</code> the given <code>{@link CustomOption}</code> is referring to.
* In the following example
* <pre>
* option (myOption).field = true;
* </pre>
* this method will return the <code>{@link Property}</code> "field" is pointing to.
* @param option the given {@code Option}.
- * @return the field of the {@code Property} the given {@code CustomOption} is referring to, or {@code null} if one
+ * @return the field of the {@code Property} the given {@code CustomOption} is referring to, or {@code null} if one
* cannot be found.
*/
public Property fieldFrom(CustomOption option) {
SimplePropertyRef ref = option.getPropertyField();
return (ref == null) ? null : ref.getProperty();
}
-
- /**
- * Indicates whether the given object is an "extend message" and its name matches the one specified in the given
- * option type.
- * @param o the object to check.
- * @param optionType the type of option we are interested in.
- * @return {@code true} if the given object is an "extend message" and its name matches the one specified in the given
- * option type; {@code false} otherwise.
- */
- public boolean isExtendingOptionMessage(EObject o, OptionType optionType) {
- if (!(o instanceof ExtendMessage)) return false;
- Message message = messageFrom((ExtendMessage) o);
- if (message == null) return false;
- return optionType.messageName().equals(message.getName());
- }
-
- private Message messageFrom(ExtendMessage extend) {
- MessageRef ref = extend.getMessage();
- return ref == null ? null : ref.getType();
- }
}
diff --git a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/CustomOptionDescriptions.java b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/CustomOptionDescriptions.java
index c459561..58a67d2 100644
--- a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/CustomOptionDescriptions.java
+++ b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/CustomOptionDescriptions.java
@@ -8,20 +8,19 @@
*/
package com.google.eclipse.protobuf.scoping;
-import static com.google.eclipse.protobuf.model.OptionType.typeOf;
+import static com.google.eclipse.protobuf.scoping.OptionType.typeOf;
import static java.util.Collections.emptyList;
import static org.eclipse.emf.ecore.util.EcoreUtil.getAllContents;
import static org.eclipse.xtext.resource.EObjectDescription.create;
import java.util.*;
-import org.eclipse.emf.common.util.*;
+import org.eclipse.emf.common.util.TreeIterator;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.*;
import org.eclipse.xtext.naming.QualifiedName;
import org.eclipse.xtext.resource.IEObjectDescription;
-import com.google.eclipse.protobuf.model.OptionType;
import com.google.eclipse.protobuf.model.util.*;
import com.google.eclipse.protobuf.protobuf.*;
import com.google.eclipse.protobuf.protobuf.Package;
@@ -35,7 +34,6 @@
@Inject private ModelFinder finder;
@Inject private Imports imports;
@Inject private LocalNamesProvider localNamesProvider;
- @Inject private Options options;
@Inject private Packages packages;
@Inject private QualifiedNameDescriptions qualifiedNamesDescriptions;
@Inject private Resources resources;
@@ -56,10 +54,10 @@
current = current.eContainer();
}
Protobuf root = finder.rootOf(option);
- descriptions.addAll(imported(root, type));
+ descriptions.addAll(imported(root, type));
return descriptions;
}
-
+
private Collection <IEObjectDescription> local(EObject root, OptionType optionType) {
return local(root, optionType, 0);
}
@@ -68,7 +66,7 @@
if (optionType == null) return emptyList();
Set<IEObjectDescription> descriptions = new LinkedHashSet<IEObjectDescription>();
for (EObject element : root.eContents()) {
- if (options.isExtendingOptionMessage(element, optionType)) {
+ if (isExtendingOptionMessage(element, optionType)) {
ExtendMessage extend = (ExtendMessage) element;
for (MessageElement e : extend.getElements()) {
if (!(e instanceof Property)) continue;
@@ -87,7 +85,7 @@
}
return descriptions;
}
-
+
private Collection<IEObjectDescription> imported(Protobuf root, OptionType optionType) {
List<Import> allImports = finder.importsIn(root);
if (allImports.isEmpty()) return emptyList();
@@ -131,7 +129,7 @@
TreeIterator<Object> contents = getAllContents(resource, true);
while (contents.hasNext()) {
Object next = contents.next();
- if (!options.isExtendingOptionMessage((EObject) next, optionType)) continue;
+ if (!isExtendingOptionMessage((EObject) next, optionType)) continue;
ExtendMessage extend = (ExtendMessage) next;
for (MessageElement e : extend.getElements()) {
if (!(e instanceof Property)) continue;
@@ -143,6 +141,18 @@
return descriptions;
}
+ private boolean isExtendingOptionMessage(EObject o, OptionType optionType) {
+ if (!(o instanceof ExtendMessage)) return false;
+ Message message = messageFrom((ExtendMessage) o);
+ if (message == null) return false;
+ return optionType.messageName().equals(message.getName());
+ }
+
+ private Message messageFrom(ExtendMessage extend) {
+ MessageRef ref = extend.getMessage();
+ return ref == null ? null : ref.getType();
+ }
+
/*
* Scope for 'y' in:
* option Type (x).y = 0;
diff --git a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/model/OptionType.java b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/OptionType.java
similarity index 91%
rename from com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/model/OptionType.java
rename to com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/OptionType.java
index a0dec62..9dc6359 100644
--- a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/model/OptionType.java
+++ b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/OptionType.java
@@ -6,7 +6,7 @@
*
* http://www.eclipse.org/legal/epl-v10.html
*/
-package com.google.eclipse.protobuf.model;
+package com.google.eclipse.protobuf.scoping;
import java.util.*;
import java.util.Map.Entry;
@@ -18,13 +18,13 @@
/**
* Types of options (by location.)
- *
+ *
* @author alruiz@google.com (Alex Ruiz)
*/
-public enum OptionType {
-
+enum OptionType {
+
// TODO move back to package "com.google.eclipse.protobuf.scoping" and make it package-protected.
-
+
FILE("FileOptions"), MESSAGE("MessageOptions"), FIELD("FieldOptions"), ENUM("EnumOptions"),
LITERAL("EnumValueOptions"), SERVICE("ServiceOptions"), RPC("MethodOptions");
@@ -50,7 +50,7 @@
* Returns the name of the message in descriptor.proto that specifies the type of an option.
* @return the name of the message in descriptor.proto that specifies the type of an option.
*/
- public String messageName() {
+ String messageName() {
return messageName;
}
@@ -59,7 +59,7 @@
* @param option the given option.
* @return the type of the given option or {@code null} if a type cannot be found.
*/
- public static OptionType typeOf(FieldOption option) {
+ static OptionType typeOf(FieldOption option) {
return findType(option.eContainer());
}
@@ -68,7 +68,7 @@
* @param option the given option.
* @return the type of the given option or {@code null} if a type cannot be found.
*/
- public static OptionType typeOf(Option option) {
+ static OptionType typeOf(Option option) {
return findType(option.eContainer());
}
diff --git a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/ProtoDescriptor.java b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/ProtoDescriptor.java
index 7d9f1b7..ac041b3 100644
--- a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/ProtoDescriptor.java
+++ b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/ProtoDescriptor.java
@@ -8,8 +8,8 @@
*/
package com.google.eclipse.protobuf.scoping;
-import static com.google.eclipse.protobuf.model.OptionType.*;
import static com.google.eclipse.protobuf.protobuf.ProtobufPackage.Literals.PROPERTY__TYPE;
+import static com.google.eclipse.protobuf.scoping.OptionType.*;
import static com.google.eclipse.protobuf.util.Closeables.closeQuietly;
import static com.google.eclipse.protobuf.util.Encodings.UTF_8;
import static java.util.Collections.*;
@@ -28,7 +28,6 @@
import org.eclipse.xtext.resource.XtextResource;
import com.google.common.annotations.VisibleForTesting;
-import com.google.eclipse.protobuf.model.OptionType;
import com.google.eclipse.protobuf.model.util.INodes;
import com.google.eclipse.protobuf.protobuf.*;
import com.google.eclipse.protobuf.protobuf.Enum;