001 /*
002 * Copyright 2010-2013 JetBrains s.r.o.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017 package org.jetbrains.jet.lang.resolve.kotlin.header;
018
019 import org.jetbrains.annotations.NotNull;
020 import org.jetbrains.annotations.Nullable;
021 import org.jetbrains.jet.lang.resolve.java.AbiVersionUtil;
022 import org.jetbrains.jet.lang.resolve.java.JvmClassName;
023 import org.jetbrains.jet.lang.resolve.name.ClassId;
024 import org.jetbrains.jet.lang.resolve.name.FqName;
025 import org.jetbrains.jet.lang.resolve.name.Name;
026
027 import java.util.*;
028
029 import static org.jetbrains.jet.lang.resolve.java.AbiVersionUtil.isAbiVersionCompatible;
030 import static org.jetbrains.jet.lang.resolve.java.JvmAnnotationNames.*;
031 import static org.jetbrains.jet.lang.resolve.kotlin.KotlinJvmBinaryClass.*;
032 import static org.jetbrains.jet.lang.resolve.kotlin.header.KotlinClassHeader.Kind.*;
033
034 public class ReadKotlinClassHeaderAnnotationVisitor implements AnnotationVisitor {
035 private static final Map<JvmClassName, KotlinClassHeader.Kind> HEADER_KINDS = new HashMap<JvmClassName, KotlinClassHeader.Kind>();
036
037 static {
038 HEADER_KINDS.put(JvmClassName.byFqNameWithoutInnerClasses(KOTLIN_CLASS), CLASS);
039 HEADER_KINDS.put(JvmClassName.byFqNameWithoutInnerClasses(KOTLIN_PACKAGE), PACKAGE_FACADE);
040 HEADER_KINDS.put(KotlinSyntheticClass.CLASS_NAME, SYNTHETIC_CLASS);
041
042 @SuppressWarnings("deprecation")
043 List<FqName> incompatible = Arrays.asList(OLD_JET_CLASS_ANNOTATION, OLD_JET_PACKAGE_CLASS_ANNOTATION, OLD_KOTLIN_CLASS,
044 OLD_KOTLIN_PACKAGE, OLD_KOTLIN_PACKAGE_FRAGMENT, OLD_KOTLIN_TRAIT_IMPL);
045 for (FqName fqName : incompatible) {
046 HEADER_KINDS.put(JvmClassName.byFqNameWithoutInnerClasses(fqName), INCOMPATIBLE_ABI_VERSION);
047 }
048 }
049
050 private int version = AbiVersionUtil.INVALID_VERSION;
051 private String[] annotationData = null;
052 private KotlinClassHeader.Kind headerKind = null;
053 private KotlinSyntheticClass.Kind syntheticClassKind = null;
054
055 @Nullable
056 public KotlinClassHeader createHeader() {
057 if (headerKind == null) {
058 return null;
059 }
060
061 if (!AbiVersionUtil.isAbiVersionCompatible(version)) {
062 return new KotlinClassHeader(INCOMPATIBLE_ABI_VERSION, version, null, null);
063 }
064
065 if ((headerKind == CLASS || headerKind == PACKAGE_FACADE) && annotationData == null) {
066 // This means that the annotation is found and its ABI version is compatible, but there's no "data" string array in it.
067 // We tell the outside world that there's really no annotation at all
068 return null;
069 }
070
071 return new KotlinClassHeader(headerKind, version, annotationData, syntheticClassKind);
072 }
073
074 @Nullable
075 @Override
076 public AnnotationArgumentVisitor visitAnnotation(@NotNull ClassId classId) {
077 JvmClassName annotation = JvmClassName.byClassId(classId);
078 KotlinClassHeader.Kind newKind = HEADER_KINDS.get(annotation);
079 if (newKind == null) return null;
080
081 if (headerKind != null) {
082 // Ignore all Kotlin annotations except the first found
083 return null;
084 }
085
086 headerKind = newKind;
087
088 if (newKind == CLASS || newKind == PACKAGE_FACADE) {
089 return kotlinClassOrPackageVisitor(annotation);
090 }
091 else if (newKind == SYNTHETIC_CLASS) {
092 return syntheticClassAnnotationVisitor();
093 }
094
095 return null;
096 }
097
098 @Override
099 public void visitEnd() {
100 }
101
102 @NotNull
103 private AnnotationArgumentVisitor kotlinClassOrPackageVisitor(@NotNull final JvmClassName annotationClassName) {
104 return new AnnotationArgumentVisitor() {
105 @Override
106 public void visit(@Nullable Name name, @Nullable Object value) {
107 visitIntValueForSupportedAnnotation(name, value, annotationClassName);
108 }
109
110 @Override
111 public void visitEnum(@NotNull Name name, @NotNull ClassId enumClassId, @NotNull Name enumEntryName) {
112 unexpectedArgument(name, annotationClassName);
113 }
114
115 @Override
116 @Nullable
117 public AnnotationArrayArgumentVisitor visitArray(@NotNull Name name) {
118 if (name.asString().equals(DATA_FIELD_NAME)) {
119 return stringArrayVisitor();
120 }
121 else if (isAbiVersionCompatible(version)) {
122 throw new IllegalStateException("Unexpected array argument " + name + " for annotation " + annotationClassName);
123 }
124
125 return null;
126 }
127
128 @NotNull
129 private AnnotationArrayArgumentVisitor stringArrayVisitor() {
130 final List<String> strings = new ArrayList<String>(1);
131 return new AnnotationArrayArgumentVisitor() {
132 @Override
133 public void visit(@Nullable Object value) {
134 if (!(value instanceof String)) {
135 throw new IllegalStateException("Unexpected argument value: " + value);
136 }
137
138 strings.add((String) value);
139 }
140
141 @Override
142 public void visitEnum(@NotNull ClassId enumClassId, @NotNull Name enumEntryName) {
143 unexpectedArgument(null, annotationClassName);
144 }
145
146 @Override
147 public void visitEnd() {
148 annotationData = strings.toArray(new String[strings.size()]);
149 }
150 };
151 }
152
153 @Override
154 public void visitEnd() {
155 }
156 };
157 }
158
159 @NotNull
160 private AnnotationArgumentVisitor syntheticClassAnnotationVisitor() {
161 return new AnnotationArgumentVisitor() {
162 @Override
163 public void visit(@Nullable Name name, @Nullable Object value) {
164 visitIntValueForSupportedAnnotation(name, value, KotlinSyntheticClass.CLASS_NAME);
165 }
166
167 @Override
168 public void visitEnum(@NotNull Name name, @NotNull ClassId enumClassId, @NotNull Name enumEntryName) {
169 if (enumClassId.equals(KotlinSyntheticClass.KIND_CLASS_ID) && name.equals(KotlinSyntheticClass.KIND_FIELD_NAME)) {
170 // Don't call KotlinSyntheticClass.Kind.valueOf() here, because it will throw an exception if there's no such value,
171 // but we don't want to fail if we're loading the header with an _incompatible_ ABI version
172 syntheticClassKind = KotlinSyntheticClass.Kind.valueOfOrNull(enumEntryName.asString());
173 if (syntheticClassKind != null) return;
174 }
175 if (isAbiVersionCompatible(version)) {
176 throw new IllegalStateException("Unexpected enum entry for synthetic class annotation: " +
177 name + "=" + enumClassId + "." + enumEntryName);
178 }
179 }
180
181 @Nullable
182 @Override
183 public AnnotationArrayArgumentVisitor visitArray(@NotNull Name name) {
184 return unexpectedArgument(name, KotlinSyntheticClass.CLASS_NAME);
185 }
186
187 @Override
188 public void visitEnd() {
189 }
190 };
191 }
192
193 private void visitIntValueForSupportedAnnotation(@Nullable Name name, @Nullable Object value, @NotNull JvmClassName className) {
194 if (name != null && name.asString().equals(ABI_VERSION_FIELD_NAME)) {
195 version = value == null ? AbiVersionUtil.INVALID_VERSION : (Integer) value;
196 }
197 else {
198 unexpectedArgument(name, className);
199 }
200 }
201
202 @Nullable
203 private AnnotationArrayArgumentVisitor unexpectedArgument(@Nullable Name name, @NotNull JvmClassName annotationClassName) {
204 if (isAbiVersionCompatible(version)) {
205 throw new IllegalStateException("Unexpected argument " + name + " for annotation " + annotationClassName);
206 }
207 return null;
208 }
209 }