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;
018
019 import org.jetbrains.annotations.NotNull;
020 import org.jetbrains.jet.lang.descriptors.ClassDescriptor;
021 import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
022 import org.jetbrains.jet.lang.descriptors.NamespaceDescriptor;
023 import org.jetbrains.jet.lang.resolve.DescriptorUtils;
024 import org.jetbrains.jet.lang.resolve.java.JvmAbi;
025 import org.jetbrains.jet.lang.resolve.name.FqName;
026 import org.jetbrains.jet.lang.resolve.name.FqNameUnsafe;
027 import org.jetbrains.jet.lang.resolve.name.Name;
028
029 import java.util.ArrayList;
030 import java.util.List;
031
032 public class DeserializedResolverUtils {
033 private DeserializedResolverUtils() {
034 }
035
036 @NotNull
037 public static FqName kotlinFqNameToJavaFqName(@NotNull FqNameUnsafe kotlinFqName) {
038 List<String> correctedSegments = new ArrayList<String>();
039 for (Name segment : kotlinFqName.pathSegments()) {
040 if (segment.asString().startsWith("<class-object-for")) {
041 correctedSegments.add(JvmAbi.CLASS_OBJECT_CLASS_NAME);
042 }
043 else {
044 assert !segment.isSpecial();
045 correctedSegments.add(segment.asString());
046 }
047 }
048 return FqName.fromSegments(correctedSegments);
049 }
050
051 @NotNull
052 public static FqNameUnsafe naiveKotlinFqName(@NotNull ClassDescriptor descriptor) {
053 DeclarationDescriptor containing = descriptor.getContainingDeclaration();
054 if (containing instanceof ClassDescriptor) {
055 return naiveKotlinFqName((ClassDescriptor) containing).child(descriptor.getName());
056 }
057 else if (containing instanceof NamespaceDescriptor) {
058 return DescriptorUtils.getFQName(containing).child(descriptor.getName());
059 }
060 else {
061 throw new IllegalArgumentException("Class doesn't have a FQ name: " + descriptor);
062 }
063 }
064 }