Simplify ClassFileFactory

Get rid of outdated assertions, mark createText() as TestOnly, add
NotNull/Nullable annotations.

Inline useless forClass/forTraitImpl/... methods, because this way it's simpler
and some code was already using newVisitor() anyway
This commit is contained in:
Alexander Udalov
2014-05-23 17:32:57 +04:00
parent ae27ac1661
commit 4ee7d2e72a
4 changed files with 22 additions and 41 deletions
@@ -24,10 +24,10 @@ import com.intellij.util.Function;
import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.TestOnly;
import org.jetbrains.jet.OutputFile;
import org.jetbrains.jet.OutputFileCollection;
import org.jetbrains.jet.codegen.state.GenerationState;
import org.jetbrains.jet.lang.descriptors.ClassDescriptor;
import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.lang.resolve.name.FqName;
import org.jetbrains.org.objectweb.asm.Type;
@@ -36,7 +36,6 @@ import java.io.File;
import java.util.*;
import static org.jetbrains.jet.codegen.AsmUtil.asmTypeByFqNameWithoutInnerClasses;
import static org.jetbrains.jet.codegen.AsmUtil.isPrimitive;
import static org.jetbrains.jet.lang.resolve.java.PackageClassUtils.getPackageClassFqName;
public class ClassFileFactory implements OutputFileCollection {
@@ -53,7 +52,7 @@ public class ClassFileFactory implements OutputFileCollection {
}
@NotNull
ClassBuilder newVisitor(@NotNull Type asmType, @NotNull PsiFile sourceFile) {
public ClassBuilder newVisitor(@NotNull Type asmType, @NotNull PsiFile sourceFile) {
return newVisitor(asmType, Collections.singletonList(sourceFile));
}
@@ -90,11 +89,11 @@ public class ClassFileFactory implements OutputFileCollection {
@Override
@Nullable
public OutputFile get(@NotNull String relativePath) {
if (generators.containsKey(relativePath)) return new OutputClassFile(relativePath);
return null;
return generators.containsKey(relativePath) ? new OutputClassFile(relativePath) : null;
}
@NotNull
@TestOnly
public String createText() {
StringBuilder answer = new StringBuilder();
@@ -106,7 +105,8 @@ public class ClassFileFactory implements OutputFileCollection {
return answer.toString();
}
public PackageCodegen forPackage(final FqName fqName, final Collection<JetFile> files) {
@NotNull
public PackageCodegen forPackage(@NotNull final FqName fqName, @NotNull final Collection<JetFile> files) {
assert !isDone : "Already done!";
PackageCodegen codegen = package2codegen.get(fqName);
if (codegen == null) {
@@ -124,32 +124,8 @@ public class ClassFileFactory implements OutputFileCollection {
return codegen;
}
public ClassBuilder forClassImplementation(ClassDescriptor aClass, PsiFile sourceFile) {
Type type = state.getTypeMapper().mapClass(aClass);
if (isPrimitive(type)) {
throw new IllegalStateException("Codegen for primitive type is not possible: " + aClass);
}
return newVisitor(type, sourceFile);
}
public ClassBuilder forLambdaInlining(Type lambdaType, PsiFile sourceFile) {
if (isPrimitive(lambdaType)) {
throw new IllegalStateException("Codegen for primitive type is not possible: " + lambdaType);
}
return newVisitor(lambdaType, sourceFile);
}
@NotNull
public ClassBuilder forPackagePart(@NotNull Type asmType, @NotNull PsiFile sourceFile) {
return newVisitor(asmType, sourceFile);
}
@NotNull
public ClassBuilder forTraitImplementation(@NotNull ClassDescriptor aClass, @NotNull GenerationState state, @NotNull PsiFile file) {
return newVisitor(state.getTypeMapper().mapTraitImpl(aClass), file);
}
private static Collection<File> toIoFilesIgnoringNonPhysical(Collection<? extends PsiFile> psiFiles) {
private static Collection<File> toIoFilesIgnoringNonPhysical(@NotNull Collection<? extends PsiFile> psiFiles) {
List<File> result = Lists.newArrayList();
for (PsiFile psiFile : psiFiles) {
VirtualFile virtualFile = psiFile.getVirtualFile();
@@ -162,10 +138,10 @@ public class ClassFileFactory implements OutputFileCollection {
return result;
}
private final class OutputClassFile implements OutputFile {
final String relativeClassFilePath;
private class OutputClassFile implements OutputFile {
private final String relativeClassFilePath;
OutputClassFile(String relativeClassFilePath) {
public OutputClassFile(String relativeClassFilePath) {
this.relativeClassFilePath = relativeClassFilePath;
}
@@ -158,12 +158,14 @@ public abstract class MemberCodegen<T extends JetElement/* TODO: & JetDeclaratio
badDescriptor(descriptor, state.getClassBuilderMode());
}
ClassBuilder classBuilder = state.getFactory().forClassImplementation(descriptor, aClass.getContainingFile());
Type classType = state.getTypeMapper().mapClass(descriptor);
ClassBuilder classBuilder = state.getFactory().newVisitor(classType, aClass.getContainingFile());
ClassContext classContext = parentContext.intoClass(descriptor, OwnerKind.IMPLEMENTATION, state);
new ImplementationBodyCodegen(aClass, classContext, classBuilder, state, parentCodegen).generate();
if (aClass instanceof JetClass && ((JetClass) aClass).isTrait()) {
ClassBuilder traitImplBuilder = state.getFactory().forTraitImplementation(descriptor, state, aClass.getContainingFile());
Type traitImplType = state.getTypeMapper().mapTraitImpl(descriptor);
ClassBuilder traitImplBuilder = state.getFactory().newVisitor(traitImplType, aClass.getContainingFile());
ClassContext traitImplContext = parentContext.intoClass(descriptor, OwnerKind.TRAIT_IMPL, state);
new TraitImplBodyCodegen(aClass, traitImplContext, traitImplBuilder, state, parentCodegen).generate();
}
@@ -285,7 +285,7 @@ public class PackageCodegen {
if (!generatePackagePart) return null;
ClassBuilder builder = state.getFactory().forPackagePart(packagePartType, file);
ClassBuilder builder = state.getFactory().newVisitor(packagePartType, file);
new PackagePartCodegen(builder, file, packagePartType, packagePartContext, state).generate();
@@ -29,7 +29,10 @@ import org.jetbrains.jet.codegen.state.GenerationState;
import org.jetbrains.jet.codegen.state.JetTypeMapper;
import org.jetbrains.org.objectweb.asm.*;
import org.jetbrains.org.objectweb.asm.commons.InstructionAdapter;
import org.jetbrains.org.objectweb.asm.tree.*;
import org.jetbrains.org.objectweb.asm.tree.AbstractInsnNode;
import org.jetbrains.org.objectweb.asm.tree.FieldInsnNode;
import org.jetbrains.org.objectweb.asm.tree.MethodNode;
import org.jetbrains.org.objectweb.asm.tree.VarInsnNode;
import java.io.IOException;
import java.util.*;
@@ -294,8 +297,8 @@ public class AnonymousObjectTransformer {
@NotNull
private ClassBuilder createClassBuilder() {
return new RemappingClassBuilder(state.getFactory().forLambdaInlining(newLambdaType, inliningContext.getRoot().callElement.getContainingFile()),
new TypeRemapper(inliningContext.typeMapping));
ClassBuilder classBuilder = state.getFactory().newVisitor(newLambdaType, inliningContext.getRoot().callElement.getContainingFile());
return new RemappingClassBuilder(classBuilder, new TypeRemapper(inliningContext.typeMapping));
}
@NotNull