KotlinInfo data parameter is now String[]
This is done because byte arrays in annotation arguments cannot be longer than 65535 bytes, and serialized data's length for kotlin.KotlinPackage class from stdlib is already close to this number
This commit is contained in:
+63
-4
@@ -43,12 +43,71 @@ public class JavaProtoBufUtil {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static ClassData readClassDataFrom(@NotNull byte[] data) {
|
||||
return ClassData.read(data, getExtensionRegistry());
|
||||
public static ClassData readClassDataFrom(@NotNull String[] data) {
|
||||
return ClassData.read(decodeBytes(data), getExtensionRegistry());
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static PackageData readPackageDataFrom(@NotNull byte[] data) {
|
||||
return PackageData.read(data, getExtensionRegistry());
|
||||
public static PackageData readPackageDataFrom(@NotNull String[] data) {
|
||||
return PackageData.read(decodeBytes(data), getExtensionRegistry());
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a byte array of serialized data to an array of {@code String} satisfying JVM annotation value argument restrictions:
|
||||
* <ol>
|
||||
* <li>Each string's length should be no more than 65535</li>
|
||||
* <li>UTF-8 representation of each string cannot contain byte 0x0 or bytes in the range 0xf0..0xff</li>
|
||||
* </ol>
|
||||
*/
|
||||
@NotNull
|
||||
public static String[] encodeBytes(@NotNull byte[] data) {
|
||||
// Each byte of data is split into two 4-bit parts ('lo' and 'hi'), then lo + 1 and hi + 1 are appended to the string. Hence, each
|
||||
// byte of the string is in the range 0x01..0x10 and this guarantees there's no byte 0x0 and no bytes in the range 0xf0..0xff
|
||||
// TODO: use Scala's approach instead (break data into chunks of 7 bits)
|
||||
int m = 32766;
|
||||
assert 2 * m <= 65535 : m;
|
||||
|
||||
int n = data.length;
|
||||
String[] result = new String[(n + m - 1) / m];
|
||||
for (int offset = 0, resultIndex = 0; offset < n; offset += m, resultIndex++) {
|
||||
int length = Math.min(n - offset, m);
|
||||
byte[] a = new byte[length * 2];
|
||||
for (int i = 0; i < length; i++) {
|
||||
int lo = data[offset + i] & 0x0f;
|
||||
int hi = (data[offset + i] & 0xf0) >>> 4;
|
||||
a[2 * i] = (byte) (lo + 1);
|
||||
a[2 * i + 1] = (byte) (hi + 1);
|
||||
}
|
||||
result[resultIndex] = new String(a);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts encoded array of {@code String} obtained by {@link JavaProtoBufUtil#encodeBytes(byte[])} back to a byte array.
|
||||
*/
|
||||
@NotNull
|
||||
public static byte[] decodeBytes(@NotNull String[] data) {
|
||||
int length = 0;
|
||||
for (String s : data) {
|
||||
assert s.length() % 2 == 0 : s.length();
|
||||
length += s.length() / 2;
|
||||
}
|
||||
byte[] result = new byte[length];
|
||||
|
||||
int p = 0;
|
||||
for (String s : data) {
|
||||
for (int i = 0, n = s.length(); i < n; i += 2) {
|
||||
int lo = s.charAt(i) - 1;
|
||||
int hi = s.charAt(i + 1) - 1;
|
||||
assert 0 <= lo && lo < 0xf0 : lo;
|
||||
assert 0 <= hi && hi < 0xf0 : hi;
|
||||
result[p++] = (byte) (lo + (hi << 4));
|
||||
}
|
||||
}
|
||||
|
||||
assert p == length;
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
+29
-12
@@ -40,6 +40,8 @@ import org.jetbrains.jet.utils.ExceptionUtils;
|
||||
import javax.inject.Inject;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.jetbrains.asm4.ClassReader.*;
|
||||
import static org.jetbrains.jet.lang.resolve.java.DescriptorSearchRule.INCLUDE_KOTLIN;
|
||||
@@ -136,25 +138,25 @@ public final class DeserializedDescriptorResolver {
|
||||
|
||||
@Nullable
|
||||
private static ClassData readClassDataFromClassFile(@NotNull VirtualFile virtualFile) {
|
||||
byte[] data = getKotlinInfoDataFromClassFile(virtualFile);
|
||||
String[] data = getKotlinInfoDataFromClassFile(virtualFile);
|
||||
if (data == null) return null;
|
||||
return JavaProtoBufUtil.readClassDataFrom(data);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static PackageData readPackageDataFromClassFile(@NotNull VirtualFile virtualFile) {
|
||||
byte[] data = getKotlinInfoDataFromClassFile(virtualFile);
|
||||
String[] data = getKotlinInfoDataFromClassFile(virtualFile);
|
||||
if (data == null) return null;
|
||||
return JavaProtoBufUtil.readPackageDataFrom(data);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static byte[] getKotlinInfoDataFromClassFile(@NotNull VirtualFile virtualFile) {
|
||||
private static String[] getKotlinInfoDataFromClassFile(@NotNull VirtualFile virtualFile) {
|
||||
GetKotlinInfoDataVisitor visitor = visitClassFile(virtualFile);
|
||||
if (visitor == null) {
|
||||
return null;
|
||||
}
|
||||
byte[] data = visitor.getData();
|
||||
String[] data = visitor.getData();
|
||||
if (data == null) {
|
||||
return null;
|
||||
}
|
||||
@@ -186,28 +188,43 @@ public final class DeserializedDescriptorResolver {
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private byte[] data = null;
|
||||
private String[] data = null;
|
||||
|
||||
@Override
|
||||
public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
|
||||
if (!desc.equals(KOTLIN_INFO_TYPE)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return new AnnotationVisitor(Opcodes.ASM4) {
|
||||
@Override
|
||||
public void visit(String name, Object value) {
|
||||
if (name.equals("data")) {
|
||||
data = (byte[]) value;
|
||||
}
|
||||
else {
|
||||
throw new IllegalStateException("Unexpected property " + name + " for annotation " + KOTLIN_INFO_TYPE);
|
||||
public AnnotationVisitor visitArray(String name) {
|
||||
if (!name.equals("data")) {
|
||||
throw new IllegalStateException("Unexpected argument " + name + " for annotation " + KOTLIN_INFO_TYPE);
|
||||
}
|
||||
|
||||
final List<String> strings = new ArrayList<String>(1);
|
||||
return new AnnotationVisitor(Opcodes.ASM4) {
|
||||
@Override
|
||||
public void visit(String name, Object value) {
|
||||
if (!(value instanceof String)) {
|
||||
throw new IllegalStateException("Unexpected argument value: " + value);
|
||||
}
|
||||
|
||||
strings.add((String) value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitEnd() {
|
||||
data = strings.toArray(new String[strings.size()]);
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public byte[] getData() {
|
||||
public String[] getData() {
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user