Commit MetaInfo

Revision560311568533485f5856cd92bf0c1a5f3f6fa5a5 (tree)
Time2012-02-08 15:38:19
Authorjwat <jwat@user...>
Commiterjwat

Log Message

* Add ASN1Extendable annotation.
* Change return type of collection method of CollectionType from Collection<T> to List<T>.

Change Summary

Incremental Difference

--- /dev/null
+++ b/jp/bitmeister/asn1/annotation/ASN1Extendable.java
@@ -0,0 +1,70 @@
1+/*
2+ * Copyright 2011 BitMeister Inc.
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+package jp.bitmeister.asn1.annotation;
17+
18+import java.lang.annotation.ElementType;
19+import java.lang.annotation.Retention;
20+import java.lang.annotation.RetentionPolicy;
21+import java.lang.annotation.Target;
22+
23+/**
24+ * Indicates that a type is extendable SET, SEQUENCE or CHOICE.
25+ *
26+ * <p>
27+ * If an {@code @ASN1Extendable} annotation is present on a SET, SEQUENCE or
28+ * CHOICE type class, the class can be base class for other SET, SEQUENCE or
29+ * CHOICE type class. This annotation will be used for defining common attribute
30+ * or method to some of SET, SEQUENCE or CHOICE classes by using class
31+ * inheritance. A type annotated as {@code @ASN1Extendable} can't define own
32+ * elements.
33+ * </p>
34+ * <p>
35+ * For example : <code><pre>
36+ * &#064;ASN1Extendable
37+ * public class IdentifiableSequence extends SEQUENCE {
38+ *
39+ * private ID id;
40+ *
41+ * public ID getID() {
42+ * return id;
43+ * }
44+ *
45+ * public void SetID(ID id) {
46+ * this.id = id;
47+ * }
48+ *
49+ * }
50+ *
51+ * public class User extends IdentifiableSequence {
52+ *
53+ * &#064;ASN1ElementType(0)
54+ * public INTEGER userNo;
55+ *
56+ * &#064;ASN1ElementType(value = 1, optional = true)
57+ * public PrintableString userName;
58+ *
59+ * }
60+ *
61+ * </pre></code>
62+ *
63+ * </p>
64+ *
65+ * @author WATANABE, Jun. <jwat at bitmeister.jp>
66+ */
67+@Target(ElementType.TYPE)
68+@Retention(RetentionPolicy.RUNTIME)
69+public @interface ASN1Extendable {
70+}
--- a/jp/bitmeister/asn1/codec/ber/BerDecoder.java
+++ b/jp/bitmeister/asn1/codec/ber/BerDecoder.java
@@ -555,7 +555,7 @@ public class BerDecoder implements ASN1Decoder,
555555 * When an error occurred while the decoding process.
556556 */
557557 private <T extends ASN1Type> void processCollection(
558- final CollectionType<T> data) throws ASN1DecodingException {
558+ final CollectionType<T, ?> data) throws ASN1DecodingException {
559559 if (data.size() > 0) {
560560 data.clear();
561561 }
--- a/jp/bitmeister/asn1/codec/ber/BerEncoder.java
+++ b/jp/bitmeister/asn1/codec/ber/BerEncoder.java
@@ -274,7 +274,7 @@ public class BerEncoder implements ASN1Encoder,
274274 * jp.bitmeister.asn1.processor.ASN1Visitor#visit(jp.bitmeister.asn1.type
275275 * .builtin.SEQUENCE_OF)
276276 */
277- public EncodedOctets visit(SEQUENCE_OF<? extends ASN1Type> data)
277+ public EncodedOctets visit(SEQUENCE_OF<?> data)
278278 throws ASN1EncodingException {
279279 return processCollection(data);
280280 }
@@ -297,7 +297,7 @@ public class BerEncoder implements ASN1Encoder,
297297 * jp.bitmeister.asn1.processor.ASN1Visitor#visit(jp.bitmeister.asn1.type
298298 * .builtin.SET_OF)
299299 */
300- public ConstructedOctets visit(SET_OF<? extends ASN1Type> data)
300+ public ConstructedOctets visit(SET_OF<?> data)
301301 throws ASN1EncodingException {
302302 return processCollection(data);
303303 }
@@ -441,7 +441,7 @@ public class BerEncoder implements ASN1Encoder,
441441 * @throws ASN1EncodingException
442442 * When an error occurred while the encoding process.
443443 */
444- ConstructedOctets processCollection(CollectionType<?> data)
444+ ConstructedOctets processCollection(CollectionType<? extends ASN1Type, ?> data)
445445 throws ASN1EncodingException {
446446 ConstructedOctets octets = newConstructedOctets();
447447 for (ASN1Type e : data.collection()) {
--- a/jp/bitmeister/asn1/codec/xer/XerDecoder.java
+++ b/jp/bitmeister/asn1/codec/xer/XerDecoder.java
@@ -957,7 +957,7 @@ public class XerDecoder implements ASN1Decoder,
957957 * @return Handler for the data.
958958 */
959959 private <T extends ASN1Type> DataDecoder decoderForCollectionType(
960- final CollectionType<T> data) {
960+ final CollectionType<T, ?> data) {
961961 return new ConstructedDataDecoder() {
962962
963963 private T component;
--- a/jp/bitmeister/asn1/codec/xer/XerEncoder.java
+++ b/jp/bitmeister/asn1/codec/xer/XerEncoder.java
@@ -477,7 +477,7 @@ public class XerEncoder implements ASN1Encoder,
477477 * @throws ASN1EncodingException
478478 * When an error occurred while the encoding process.
479479 */
480- private String processCollection(CollectionType<? extends ASN1Type> data)
480+ private String processCollection(CollectionType<? extends ASN1Type, ?> data)
481481 throws ASN1EncodingException {
482482 StringBuilder enclosure = builder;
483483 builder = new StringBuilder();
--- a/jp/bitmeister/asn1/processor/ASN1StringBuilder.java
+++ b/jp/bitmeister/asn1/processor/ASN1StringBuilder.java
@@ -136,8 +136,8 @@ public class ASN1StringBuilder implements
136136 if (specification.hasIdentifier()) {
137137 builder.append(specification.identifier());
138138 if (specification.reference() == null) {
139- if (data instanceof CollectionType<?>) {
140- CollectionType<?> collection = (CollectionType<?>) data;
139+ if (data instanceof CollectionType<?, ?>) {
140+ CollectionType<?, ?> collection = (CollectionType<?, ?>) data;
141141 if (!collection.componentType().equals(ANY.class)) {
142142 builder.append(COLLECTION_OF).append(
143143 collection.componentSpecification()
@@ -298,7 +298,7 @@ public class ASN1StringBuilder implements
298298 * jp.bitmeister.asn1.processor.ASN1Visitor#visit(jp.bitmeister.asn1.type
299299 * .builtin.SEQUENCE_OF)
300300 */
301- public Void visit(SEQUENCE_OF<? extends ASN1Type> data) {
301+ public Void visit(SEQUENCE_OF<?> data) {
302302 writeCollection(data);
303303 return null;
304304 }
@@ -322,7 +322,7 @@ public class ASN1StringBuilder implements
322322 * jp.bitmeister.asn1.processor.ASN1Visitor#visit(jp.bitmeister.asn1.type
323323 * .builtin.SET_OF)
324324 */
325- public Void visit(SET_OF<? extends ASN1Type> data) {
325+ public Void visit(SET_OF<?> data) {
326326 writeCollection(data);
327327 return null;
328328 }
@@ -473,7 +473,7 @@ public class ASN1StringBuilder implements
473473 * @param data
474474 * The collection data.
475475 */
476- private void writeCollection(CollectionType<? extends ASN1Type> data) {
476+ private void writeCollection(CollectionType<? extends ASN1Type, ?> data) {
477477 Iterator<? extends ASN1Type> itr = data.collection().iterator();
478478 if (itr.hasNext()) {
479479 while (true) {
--- a/jp/bitmeister/asn1/sample/FrightStatusMain.java
+++ b/jp/bitmeister/asn1/sample/FrightStatusMain.java
@@ -39,6 +39,7 @@ import jp.bitmeister.asn1.value.HexString;
3939 public class FrightStatusMain {
4040
4141 public static void main(String[] args) {
42+
4243 AllFrights frights = new AllFrights(
4344 new FrightStatus(
4445 new FrightNumber("JP041"),
@@ -71,7 +72,6 @@ public class FrightStatusMain {
7172
7273 System.out.println("Source data:");
7374 System.out.println(frights);
74-
7575 derEncAndDec(frights);
7676 xerEncAndDec(frights);
7777
--- a/jp/bitmeister/asn1/sample/FrightStatusTypes.java
+++ b/jp/bitmeister/asn1/sample/FrightStatusTypes.java
@@ -21,6 +21,7 @@ import jp.bitmeister.asn1.annotation.ASN1Alternative;
2121 import jp.bitmeister.asn1.annotation.ASN1DefinedTypes;
2222 import jp.bitmeister.asn1.annotation.ASN1Element;
2323 import jp.bitmeister.asn1.annotation.ASN1Enumeration;
24+import jp.bitmeister.asn1.annotation.ASN1Extendable;
2425 import jp.bitmeister.asn1.annotation.ASN1ModuleTags;
2526 import jp.bitmeister.asn1.annotation.ASN1Tag;
2627 import jp.bitmeister.asn1.type.ASN1Module;
@@ -78,8 +79,15 @@ public class FrightStatusTypes extends ASN1Module {
7879 }
7980
8081 }
82+
83+ @ASN1Extendable
84+ public static class AbstractSequence extends SEQUENCE {
85+
86+
87+
88+ }
8189
82- public static class Information extends SEQUENCE {
90+ public static class Information extends AbstractSequence {
8391
8492 @ASN1Element(value = 0)
8593 public Airport airport;
--- a/jp/bitmeister/asn1/type/ASN1Module.java
+++ b/jp/bitmeister/asn1/type/ASN1Module.java
@@ -127,5 +127,5 @@ public abstract class ASN1Module {
127127 }
128128 }
129129 }
130-
130+
131131 }
--- a/jp/bitmeister/asn1/type/CollectionType.java
+++ b/jp/bitmeister/asn1/type/CollectionType.java
@@ -34,12 +34,12 @@ import jp.bitmeister.asn1.type.builtin.SET_OF;
3434 * @see SET_OF
3535 * @author WATANABE, Jun. <jwat at bitmeister.jp>
3636 */
37-public abstract class CollectionType<T extends ASN1Type> extends ASN1Type
37+public abstract class CollectionType<T extends ASN1Type, C extends Collection<T>> extends ASN1Type
3838 implements SizeCountable {
3939
4040 private Class<T> componentType;
4141
42- private Collection<T> collection;
42+ private C collection;
4343
4444 /**
4545 * Instantiate a {@code CollectionType} instance whose component is the
@@ -74,10 +74,10 @@ public abstract class CollectionType<T extends ASN1Type> extends ASN1Type
7474 *
7575 * @return The collection.
7676 */
77- public Collection<T> collection() {
77+ public C collection() {
7878 return collection;
7979 }
80-
80+
8181 /**
8282 * Returns the type of component of this data.
8383 *
@@ -102,7 +102,7 @@ public abstract class CollectionType<T extends ASN1Type> extends ASN1Type
102102 *
103103 * @return A new collection.
104104 */
105- protected abstract Collection<T> newCollection();
105+ protected abstract C newCollection();
106106
107107 /*
108108 * (non-Javadoc)
@@ -140,8 +140,8 @@ public abstract class CollectionType<T extends ASN1Type> extends ASN1Type
140140 */
141141 @Override
142142 public boolean valueEquals(Object other) {
143- if (other instanceof CollectionType<?>) {
144- return collection.equals(((CollectionType<?>) other).collection);
143+ if (other instanceof CollectionType<?, ?>) {
144+ return collection.equals(((CollectionType<?, ?>) other).collection);
145145 }
146146 return false;
147147 }
@@ -164,7 +164,7 @@ public abstract class CollectionType<T extends ASN1Type> extends ASN1Type
164164 @Override
165165 @SuppressWarnings("unchecked")
166166 public Object clone() {
167- CollectionType<T> clone = ASN1Type.instantiate(getClass());
167+ CollectionType<T, C> clone = ASN1Type.instantiate(getClass());
168168 clone.collection.addAll(collection);
169169 return clone;
170170 }
--- a/jp/bitmeister/asn1/type/ConstructiveType.java
+++ b/jp/bitmeister/asn1/type/ConstructiveType.java
@@ -23,6 +23,7 @@ import java.util.List;
2323 import java.util.Map;
2424
2525 import jp.bitmeister.asn1.annotation.ASN1Element;
26+import jp.bitmeister.asn1.annotation.ASN1Extendable;
2627 import jp.bitmeister.asn1.exception.ASN1IllegalArgument;
2728 import jp.bitmeister.asn1.exception.ASN1IllegalDefinition;
2829 import jp.bitmeister.asn1.exception.ASN1InvalidDataValue;
@@ -71,7 +72,8 @@ public abstract class ConstructiveType extends StructuredType {
7172 Class<? extends ConstructiveType> parent = (Class<? extends ConstructiveType>) type
7273 .getSuperclass();
7374 ElementSpecification[] array;
74- if (parent == SET.class || parent == SEQUENCE.class) {
75+ if (parent == SET.class || parent == SEQUENCE.class
76+ || parent.isAnnotationPresent(ASN1Extendable.class)) {
7577 Collections.sort(elements);
7678 array = elements.toArray(new ElementSpecification[0]);
7779 if (TypeSpecification.getSpecification(type).tagDefault() == ASN1TagDefault.AUTOMATIC_TAGS) {
@@ -86,7 +88,8 @@ public abstract class ConstructiveType extends StructuredType {
8688 if (!elements.isEmpty()) {
8789 ASN1IllegalDefinition ex = new ASN1IllegalDefinition();
8890 ex.setMessage(
89- "If a class does not extend SET or SEQUENCE directly, it can not define own elements.",
91+ "If a class does not extend SET, SEQUENCE or a type annotated as ASN1Extendable" +
92+ " directly, it can not define own elements.",
9093 null, type, null, null);
9194 throw ex;
9295 }
--- a/jp/bitmeister/asn1/type/SelectiveType.java
+++ b/jp/bitmeister/asn1/type/SelectiveType.java
@@ -22,6 +22,7 @@ import java.util.List;
2222 import java.util.Map;
2323
2424 import jp.bitmeister.asn1.annotation.ASN1Alternative;
25+import jp.bitmeister.asn1.annotation.ASN1Extendable;
2526 import jp.bitmeister.asn1.exception.ASN1IllegalArgument;
2627 import jp.bitmeister.asn1.exception.ASN1IllegalDefinition;
2728 import jp.bitmeister.asn1.type.builtin.CHOICE;
@@ -67,7 +68,7 @@ public abstract class SelectiveType extends StructuredType {
6768 Class<? extends SelectiveType> parent = (Class<? extends SelectiveType>) type
6869 .getSuperclass();
6970 NamedTypeSpecification[] array;
70- if (parent == CHOICE.class) {
71+ if (parent == CHOICE.class || parent.isAnnotationPresent(ASN1Extendable.class)) {
7172 if (alternatives.isEmpty()) {
7273 ASN1IllegalDefinition ex = new ASN1IllegalDefinition();
7374 ex.setMessage(
@@ -84,7 +85,8 @@ public abstract class SelectiveType extends StructuredType {
8485 if (!alternatives.isEmpty()) {
8586 ASN1IllegalDefinition ex = new ASN1IllegalDefinition();
8687 ex.setMessage(
87- "If a class does not extend CHOICE directly, it can not define own alternatives.",
88+ "If a class does not extend CHOICE or a type annotated as ASN1Extendable directly," +
89+ "it can not define own alternatives.",
8890 null, type, null, null);
8991 throw ex;
9092 }
--- a/jp/bitmeister/asn1/type/TypeSpecification.java
+++ b/jp/bitmeister/asn1/type/TypeSpecification.java
@@ -20,6 +20,7 @@ import java.util.Map;
2020
2121 import jp.bitmeister.asn1.annotation.ASN1Anonymous;
2222 import jp.bitmeister.asn1.annotation.ASN1BuiltIn;
23+import jp.bitmeister.asn1.annotation.ASN1Extendable;
2324 import jp.bitmeister.asn1.annotation.ASN1Identifier;
2425 import jp.bitmeister.asn1.annotation.ASN1ModuleRef;
2526 import jp.bitmeister.asn1.annotation.ASN1Tag;
@@ -46,6 +47,7 @@ public class TypeSpecification {
4647 * The {@code Class} object of an ASN.1 type.
4748 * @return The {@code TypeSpecification} of the type.
4849 */
50+ @SuppressWarnings("unchecked")
4951 static TypeSpecification getSpecification(Class<? extends ASN1Type> type) {
5052 if (TYPE_SPECIFICATIONS.containsKey(type)) {
5153 return TYPE_SPECIFICATIONS.get(type);
@@ -61,12 +63,10 @@ public class TypeSpecification {
6163 } else if (type.isMemberClass()) {
6264 Class<?> enclosure = type.getDeclaringClass();
6365 if (ASN1Module.class.isAssignableFrom(enclosure)) {
64- @SuppressWarnings("unchecked")
6566 Class<? extends ASN1Module> module = (Class<? extends ASN1Module>) enclosure;
6667 specification.module = module;
6768 }
6869 if (ASN1Type.class.isAssignableFrom(enclosure)) {
69- @SuppressWarnings("unchecked")
7070 Class<? extends ASN1Type> enclosingType = (Class<? extends ASN1Type>) enclosure;
7171 specification.module = getSpecification(enclosingType).module;
7272 }
@@ -81,9 +81,10 @@ public class TypeSpecification {
8181 // set reference type specification.
8282 if (!type.isAnnotationPresent(ASN1BuiltIn.class)) {
8383 try {
84- @SuppressWarnings("unchecked")
85- Class<? extends ASN1Type> referenceType = (Class<? extends ASN1Type>) type
86- .getSuperclass();
84+ Class<? extends ASN1Type> referenceType = type;
85+ do {
86+ referenceType = (Class<? extends ASN1Type>) referenceType.getSuperclass();
87+ } while (referenceType.isAnnotationPresent(ASN1Extendable.class));
8788 specification.reference = getSpecification(referenceType);
8889 } catch (ClassCastException e) {
8990 ASN1IllegalDefinition ex = new ASN1IllegalDefinition();
--- a/jp/bitmeister/asn1/type/builtin/SEQUENCE_OF.java
+++ b/jp/bitmeister/asn1/type/builtin/SEQUENCE_OF.java
@@ -16,7 +16,7 @@
1616 package jp.bitmeister.asn1.type.builtin;
1717
1818 import java.util.ArrayList;
19-import java.util.Collection;
19+import java.util.List;
2020
2121 import jp.bitmeister.asn1.annotation.ASN1BuiltIn;
2222 import jp.bitmeister.asn1.annotation.ASN1Identifier;
@@ -45,7 +45,7 @@ import jp.bitmeister.asn1.type.CollectionType;
4545 @ASN1Identifier("SEQUENCE")
4646 @ASN1XmlTypeName("SEQUENCE_OF")
4747 @ASN1Tag(tagClass = ASN1TagClass.UNIVERSAL, value = 16, tagMode = ASN1TagMode.IMPLICIT)
48-public abstract class SEQUENCE_OF<T extends ASN1Type> extends CollectionType<T> {
48+public abstract class SEQUENCE_OF<T extends ASN1Type> extends CollectionType<T, List<T>> {
4949
5050 /**
5151 * Instantiate an empty {@code SEQUENCE_OF}.
@@ -75,7 +75,7 @@ public abstract class SEQUENCE_OF<T extends ASN1Type> extends CollectionType<T>
7575 * @see jp.bitmeister.asn1.type.CollectionType#newCollection()
7676 */
7777 @Override
78- protected Collection<T> newCollection() {
78+ protected List<T> newCollection() {
7979 return new ArrayList<T>();
8080 }
8181
--- a/jp/bitmeister/asn1/type/builtin/SET_OF.java
+++ b/jp/bitmeister/asn1/type/builtin/SET_OF.java
@@ -16,7 +16,7 @@
1616 package jp.bitmeister.asn1.type.builtin;
1717
1818 import java.util.ArrayList;
19-import java.util.Collection;
19+import java.util.List;
2020
2121 import jp.bitmeister.asn1.annotation.ASN1BuiltIn;
2222 import jp.bitmeister.asn1.annotation.ASN1Identifier;
@@ -44,7 +44,7 @@ import jp.bitmeister.asn1.type.CollectionType;
4444 @ASN1Identifier("SET")
4545 @ASN1XmlTypeName("SET_OF")
4646 @ASN1Tag(tagClass = ASN1TagClass.UNIVERSAL, value = 17, tagMode = ASN1TagMode.IMPLICIT)
47-public abstract class SET_OF<T extends ASN1Type> extends CollectionType<T> {
47+public abstract class SET_OF<T extends ASN1Type> extends CollectionType<T, List<T>> {
4848
4949 /**
5050 * Instantiate an empty {@code SET_OF}.
@@ -75,7 +75,7 @@ public abstract class SET_OF<T extends ASN1Type> extends CollectionType<T> {
7575 */
7676 @Override
7777 @SuppressWarnings("serial")
78- protected Collection<T> newCollection() {
78+ protected List<T> newCollection() {
7979 return new ArrayList<T>() {
8080
8181 @Override
Show on old repository browser