From 843991083d14d94ceaed1f03521c77bfa2d91641 Mon Sep 17 00:00:00 2001 From: max-kammerer Date: Thu, 7 Feb 2013 16:50:01 +0400 Subject: [PATCH] For namespaces with multiple files keep 'Compiled from' information empty --- .../jet/codegen/NamespaceCodegen.java | 8 ++- compiler/testData/codegen/sourceInfo/foo1.kt | 5 ++ compiler/testData/codegen/sourceInfo/foo2.kt | 5 ++ .../codegen/sourceInfo/singleClass.kt | 4 ++ .../jet/codegen/SourceInfoGenTest.java | 61 +++++++++++++++++++ 5 files changed, 80 insertions(+), 3 deletions(-) create mode 100644 compiler/testData/codegen/sourceInfo/foo1.kt create mode 100644 compiler/testData/codegen/sourceInfo/foo2.kt create mode 100644 compiler/testData/codegen/sourceInfo/singleClass.kt create mode 100644 compiler/tests/org/jetbrains/jet/codegen/SourceInfoGenTest.java diff --git a/compiler/backend/src/org/jetbrains/jet/codegen/NamespaceCodegen.java b/compiler/backend/src/org/jetbrains/jet/codegen/NamespaceCodegen.java index 8c2dc027d16..cf849e13bc3 100644 --- a/compiler/backend/src/org/jetbrains/jet/codegen/NamespaceCodegen.java +++ b/compiler/backend/src/org/jetbrains/jet/codegen/NamespaceCodegen.java @@ -62,7 +62,7 @@ public class NamespaceCodegen extends MemberCodegen { name = fqName; this.files = namespaceFiles; - final PsiFile sourceFile = namespaceFiles.iterator().next().getContainingFile(); + final PsiFile sourceFile = namespaceFiles.size() == 1 ? namespaceFiles.iterator().next().getContainingFile() : null; v.addOptionalDeclaration(new ClassBuilderOnDemand.ClassBuilderCallback() { @Override @@ -75,8 +75,10 @@ public class NamespaceCodegen extends MemberCodegen { "java/lang/Object", new String[0] ); - // TODO figure something out for a namespace that spans multiple files - v.visitSource(sourceFile.getName(), null); + //We don't generate any source information for namespace with multiple files + if (sourceFile != null) { + v.visitSource(sourceFile.getName(), null); + } } }); } diff --git a/compiler/testData/codegen/sourceInfo/foo1.kt b/compiler/testData/codegen/sourceInfo/foo1.kt new file mode 100644 index 00000000000..473a615fc51 --- /dev/null +++ b/compiler/testData/codegen/sourceInfo/foo1.kt @@ -0,0 +1,5 @@ +package foo; + +fun box() = { + 1 +} \ No newline at end of file diff --git a/compiler/testData/codegen/sourceInfo/foo2.kt b/compiler/testData/codegen/sourceInfo/foo2.kt new file mode 100644 index 00000000000..42d7dc54796 --- /dev/null +++ b/compiler/testData/codegen/sourceInfo/foo2.kt @@ -0,0 +1,5 @@ +package foo; + +fun box2() = { + 1 +} \ No newline at end of file diff --git a/compiler/testData/codegen/sourceInfo/singleClass.kt b/compiler/testData/codegen/sourceInfo/singleClass.kt new file mode 100644 index 00000000000..a74ea7aa4ce --- /dev/null +++ b/compiler/testData/codegen/sourceInfo/singleClass.kt @@ -0,0 +1,4 @@ +class SingleClass { + + +} \ No newline at end of file diff --git a/compiler/tests/org/jetbrains/jet/codegen/SourceInfoGenTest.java b/compiler/tests/org/jetbrains/jet/codegen/SourceInfoGenTest.java new file mode 100644 index 00000000000..54c0aa57ab3 --- /dev/null +++ b/compiler/tests/org/jetbrains/jet/codegen/SourceInfoGenTest.java @@ -0,0 +1,61 @@ +/* + * Copyright 2010-2013 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jetbrains.jet.codegen; + +import org.jetbrains.asm4.ClassReader; +import org.jetbrains.asm4.ClassVisitor; +import org.jetbrains.asm4.Opcodes; +import org.jetbrains.jet.ConfigurationKind; + +public class SourceInfoGenTest extends CodegenTestCase { + + protected void setUp() throws Exception { + super.setUp(); + createEnvironmentWithMockJdkAndIdeaAnnotations(ConfigurationKind.JDK_ONLY); + } + + public void testSingleFileNamespace() { + String producer = "sourceInfo/foo1.kt"; + loadFiles(producer); + assertEquals(producer, getProducerInfo("foo/FooPackage.class")); + } + + public void testMultiFileNamespace() { + loadFiles("sourceInfo/foo1.kt", "sourceInfo/foo2.kt"); + assertEquals(null, getProducerInfo("foo/FooPackage.class")); + } + + public void testSingleClass() { + String producer = "sourceInfo/singleClass.kt"; + loadFiles(producer); + assertEquals(producer, getProducerInfo("SingleClass.class")); + } + + private String getProducerInfo(String name) { + ClassReader classReader = new ClassReader(generateClassesInFile().asBytes(name)); + + final String [] producer = new String [1]; + classReader.accept(new ClassVisitor(Opcodes.ASM4) { + + @Override + public void visitSource(String source, String debug) { + producer[0] = source; + } + + }, 0); + return producer[0]; + } +}