J2K serialization: invoke automatic J2K conversion

This commit is contained in:
Alexander Udalov
2016-06-07 15:11:10 +03:00
parent 89c0af0d80
commit 6692aca5f3
4 changed files with 469 additions and 544 deletions
File diff suppressed because it is too large Load Diff
@@ -14,52 +14,50 @@
* limitations under the License. * limitations under the License.
*/ */
package org.jetbrains.kotlin.serialization; package org.jetbrains.kotlin.serialization
import org.jetbrains.annotations.NotNull; import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.descriptors.*; import org.jetbrains.kotlin.types.FlexibleType
import org.jetbrains.kotlin.types.FlexibleType; import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.KotlinType;
public abstract class SerializerExtension { abstract class SerializerExtension {
@NotNull abstract val stringTable: StringTable
public abstract StringTable getStringTable();
public boolean shouldUseTypeTable() { open fun shouldUseTypeTable(): Boolean {
return false; return false
} }
public void serializeClass(@NotNull ClassDescriptor descriptor, @NotNull ProtoBuf.Class.Builder proto) { open fun serializeClass(descriptor: ClassDescriptor, proto: ProtoBuf.Class.Builder) {
} }
public void serializePackage(@NotNull ProtoBuf.Package.Builder proto) { open fun serializePackage(proto: ProtoBuf.Package.Builder) {
} }
public void serializeConstructor(@NotNull ConstructorDescriptor descriptor, @NotNull ProtoBuf.Constructor.Builder proto) { open fun serializeConstructor(descriptor: ConstructorDescriptor, proto: ProtoBuf.Constructor.Builder) {
} }
public void serializeFunction(@NotNull FunctionDescriptor descriptor, @NotNull ProtoBuf.Function.Builder proto) { open fun serializeFunction(descriptor: FunctionDescriptor, proto: ProtoBuf.Function.Builder) {
} }
public void serializeProperty(@NotNull PropertyDescriptor descriptor, @NotNull ProtoBuf.Property.Builder proto) { open fun serializeProperty(descriptor: PropertyDescriptor, proto: ProtoBuf.Property.Builder) {
} }
public void serializeEnumEntry(@NotNull ClassDescriptor descriptor, @NotNull ProtoBuf.EnumEntry.Builder proto) { open fun serializeEnumEntry(descriptor: ClassDescriptor, proto: ProtoBuf.EnumEntry.Builder) {
} }
public void serializeValueParameter(@NotNull ValueParameterDescriptor descriptor, @NotNull ProtoBuf.ValueParameter.Builder proto) { open fun serializeValueParameter(descriptor: ValueParameterDescriptor, proto: ProtoBuf.ValueParameter.Builder) {
} }
public void serializeFlexibleType(@NotNull FlexibleType flexibleType, @NotNull ProtoBuf.Type.Builder lowerProto, @NotNull ProtoBuf.Type.Builder upperProto) { open fun serializeFlexibleType(flexibleType: FlexibleType, lowerProto: ProtoBuf.Type.Builder, upperProto: ProtoBuf.Type.Builder) {
} }
public void serializeType(@NotNull KotlinType type, @NotNull ProtoBuf.Type.Builder proto) { open fun serializeType(type: KotlinType, proto: ProtoBuf.Type.Builder) {
} }
public void serializeTypeParameter(@NotNull TypeParameterDescriptor typeParameter, @NotNull ProtoBuf.TypeParameter.Builder proto) { open fun serializeTypeParameter(typeParameter: TypeParameterDescriptor, proto: ProtoBuf.TypeParameter.Builder) {
} }
public void serializeErrorType(@NotNull KotlinType type, @NotNull ProtoBuf.Type.Builder builder) { open fun serializeErrorType(type: KotlinType, builder: ProtoBuf.Type.Builder) {
throw new IllegalStateException("Cannot serialize error type: " + type); throw IllegalStateException("Cannot serialize error type: " + type)
} }
} }
@@ -14,18 +14,17 @@
* limitations under the License. * limitations under the License.
*/ */
package org.jetbrains.kotlin.serialization; package org.jetbrains.kotlin.serialization
import org.jetbrains.annotations.NotNull; import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.descriptors.ClassDescriptor; import org.jetbrains.kotlin.descriptors.ClassifierDescriptorWithTypeParameters
import org.jetbrains.kotlin.descriptors.ClassifierDescriptorWithTypeParameters;
import java.io.OutputStream; import java.io.OutputStream
public interface StringTable { interface StringTable {
int getStringIndex(@NotNull String string); fun getStringIndex(string: String): Int
int getFqNameIndex(@NotNull ClassifierDescriptorWithTypeParameters descriptor); fun getFqNameIndex(descriptor: ClassifierDescriptorWithTypeParameters): Int
void serializeTo(@NotNull OutputStream output); fun serializeTo(output: OutputStream)
} }
@@ -14,131 +14,118 @@
* limitations under the License. * limitations under the License.
*/ */
package org.jetbrains.kotlin.serialization; package org.jetbrains.kotlin.serialization
import kotlin.Pair; import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.annotations.NotNull; import org.jetbrains.kotlin.descriptors.ClassifierDescriptorWithTypeParameters
import org.jetbrains.kotlin.descriptors.ClassDescriptor; import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import org.jetbrains.kotlin.descriptors.ClassifierDescriptorWithTypeParameters; import org.jetbrains.kotlin.descriptors.PackageFragmentDescriptor
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor; import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.descriptors.PackageFragmentDescriptor; import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.name.FqName; import org.jetbrains.kotlin.types.ErrorUtils
import org.jetbrains.kotlin.name.Name; import org.jetbrains.kotlin.utils.*
import org.jetbrains.kotlin.types.ErrorUtils; import org.jetbrains.kotlin.utils.Interner
import org.jetbrains.kotlin.utils.ExceptionUtilsKt;
import org.jetbrains.kotlin.utils.Interner;
import java.io.IOException; import java.io.IOException
import java.io.OutputStream; import java.io.OutputStream
import static org.jetbrains.kotlin.serialization.ProtoBuf.QualifiedNameTable.QualifiedName; import org.jetbrains.kotlin.serialization.ProtoBuf.QualifiedNameTable.QualifiedName
public class StringTableImpl implements StringTable { class StringTableImpl : StringTable {
private static final class FqNameProto { private class FqNameProto(val fqName: QualifiedName.Builder) {
public final QualifiedName.Builder fqName;
public FqNameProto(@NotNull QualifiedName.Builder fqName) { override fun hashCode(): Int {
this.fqName = fqName; var result = 13
result = 31 * result + fqName.parentQualifiedName
result = 31 * result + fqName.shortName
result = 31 * result + fqName.kind.hashCode()
return result
} }
@Override override fun equals(obj: Any?): Boolean {
public int hashCode() { if (obj == null || javaClass != obj.javaClass) return false
int result = 13;
result = 31 * result + fqName.getParentQualifiedName();
result = 31 * result + fqName.getShortName();
result = 31 * result + fqName.getKind().hashCode();
return result;
}
@Override val other = (obj as FqNameProto).fqName
public boolean equals(Object obj) { return fqName.parentQualifiedName == other.parentQualifiedName
if (obj == null || getClass() != obj.getClass()) return false; && fqName.shortName == other.shortName
&& fqName.kind == other.kind
QualifiedName.Builder other = ((FqNameProto) obj).fqName;
return fqName.getParentQualifiedName() == other.getParentQualifiedName()
&& fqName.getShortName() == other.getShortName()
&& fqName.getKind() == other.getKind();
} }
} }
private final Interner<String> strings = new Interner<String>(); private val strings = Interner<String>()
private final Interner<FqNameProto> qualifiedNames = new Interner<FqNameProto>(); private val qualifiedNames = Interner<FqNameProto>()
public int getSimpleNameIndex(@NotNull Name name) { fun getSimpleNameIndex(name: Name): Int {
return getStringIndex(name.asString()); return getStringIndex(name.asString())
} }
@Override override fun getStringIndex(string: String): Int {
public int getStringIndex(@NotNull String string) { return strings.intern(string)
return strings.intern(string);
} }
@Override override fun getFqNameIndex(descriptor: ClassifierDescriptorWithTypeParameters): Int {
public int getFqNameIndex(@NotNull ClassifierDescriptorWithTypeParameters descriptor) {
if (ErrorUtils.isError(descriptor)) { if (ErrorUtils.isError(descriptor)) {
throw new IllegalStateException("Cannot get FQ name of error class: " + descriptor); throw IllegalStateException("Cannot get FQ name of error class: " + descriptor)
} }
QualifiedName.Builder builder = QualifiedName.newBuilder(); val builder = QualifiedName.newBuilder()
builder.setKind(QualifiedName.Kind.CLASS); builder.kind = QualifiedName.Kind.CLASS
DeclarationDescriptor containingDeclaration = descriptor.getContainingDeclaration(); val containingDeclaration = descriptor.containingDeclaration
if (containingDeclaration instanceof PackageFragmentDescriptor) { if (containingDeclaration is PackageFragmentDescriptor) {
FqName packageFqName = ((PackageFragmentDescriptor) containingDeclaration).getFqName(); val packageFqName = containingDeclaration.fqName
if (!packageFqName.isRoot()) { if (!packageFqName.isRoot) {
builder.setParentQualifiedName(getPackageFqNameIndex(packageFqName)); builder.parentQualifiedName = getPackageFqNameIndex(packageFqName)
} }
} }
else if (containingDeclaration instanceof ClassDescriptor) { else if (containingDeclaration is ClassDescriptor) {
ClassDescriptor outerClass = (ClassDescriptor) containingDeclaration; builder.parentQualifiedName = getFqNameIndex(containingDeclaration)
builder.setParentQualifiedName(getFqNameIndex(outerClass));
} }
else { else {
throw new IllegalStateException("Cannot get FQ name of local class: " + descriptor); throw IllegalStateException("Cannot get FQ name of local class: " + descriptor)
} }
builder.setShortName(getStringIndex(descriptor.getName().asString())); builder.shortName = getStringIndex(descriptor.name.asString())
return qualifiedNames.intern(new FqNameProto(builder)); return qualifiedNames.intern(FqNameProto(builder))
} }
public int getPackageFqNameIndex(@NotNull FqName fqName) { fun getPackageFqNameIndex(fqName: FqName): Int {
int result = -1; var result = -1
for (Name segment : fqName.pathSegments()) { for (segment in fqName.pathSegments()) {
QualifiedName.Builder builder = QualifiedName.newBuilder(); val builder = QualifiedName.newBuilder()
builder.setShortName(getSimpleNameIndex(segment)); builder.shortName = getSimpleNameIndex(segment)
if (result != -1) { if (result != -1) {
builder.setParentQualifiedName(result); builder.parentQualifiedName = result
} }
result = qualifiedNames.intern(new FqNameProto(builder)); result = qualifiedNames.intern(FqNameProto(builder))
} }
return result; return result
} }
@NotNull fun buildProto(): Pair<ProtoBuf.StringTable, ProtoBuf.QualifiedNameTable> {
public Pair<ProtoBuf.StringTable, ProtoBuf.QualifiedNameTable> buildProto() { val strings = ProtoBuf.StringTable.newBuilder()
ProtoBuf.StringTable.Builder strings = ProtoBuf.StringTable.newBuilder(); for (simpleName in this.strings.allInternedObjects) {
for (String simpleName : this.strings.getAllInternedObjects()) { strings.addString(simpleName)
strings.addString(simpleName);
} }
ProtoBuf.QualifiedNameTable.Builder qualifiedNames = ProtoBuf.QualifiedNameTable.newBuilder(); val qualifiedNames = ProtoBuf.QualifiedNameTable.newBuilder()
for (FqNameProto fqName : this.qualifiedNames.getAllInternedObjects()) { for (fqName in this.qualifiedNames.allInternedObjects) {
qualifiedNames.addQualifiedName(fqName.fqName); qualifiedNames.addQualifiedName(fqName.fqName)
} }
return new Pair<ProtoBuf.StringTable, ProtoBuf.QualifiedNameTable>(strings.build(), qualifiedNames.build()); return Pair(strings.build(), qualifiedNames.build())
} }
@Override override fun serializeTo(output: OutputStream) {
public void serializeTo(@NotNull OutputStream output) {
try { try {
Pair<ProtoBuf.StringTable, ProtoBuf.QualifiedNameTable> protos = buildProto(); val protos = buildProto()
protos.getFirst().writeDelimitedTo(output); protos.first.writeDelimitedTo(output)
protos.getSecond().writeDelimitedTo(output); protos.second.writeDelimitedTo(output)
} }
catch (IOException e) { catch (e: IOException) {
throw ExceptionUtilsKt.rethrow(e); throw rethrow(e)
} }
} }
} }