replace String with JvmClassName

This commit is contained in:
Stepan Koltsov
2012-05-11 01:08:33 +04:00
parent 9908791094
commit ded824468a
9 changed files with 33 additions and 22 deletions
@@ -64,7 +64,7 @@ public class ClassFileFactory {
FqName fqName = JetPsiUtil.getFQName(file); FqName fqName = JetPsiUtil.getFQName(file);
NamespaceCodegen codegen = ns2codegen.get(fqName); NamespaceCodegen codegen = ns2codegen.get(fqName);
if (codegen == null) { if (codegen == null) {
final ClassBuilder builder = newVisitor(NamespaceCodegen.getJVMClassNameForKotlinNs(fqName) + ".class"); final ClassBuilder builder = newVisitor(NamespaceCodegen.getJVMClassNameForKotlinNs(fqName).getInternalName() + ".class");
codegen = new NamespaceCodegen(builder, fqName, state, file.getContainingFile()); codegen = new NamespaceCodegen(builder, fqName, state, file.getContainingFile());
ns2codegen.put(fqName, codegen); ns2codegen.put(fqName, codegen);
} }
@@ -113,7 +113,7 @@ public abstract class CodegenContext {
while(!(descriptor instanceof NamespaceDescriptor)) { while(!(descriptor instanceof NamespaceDescriptor)) {
descriptor = descriptor.getContainingDeclaration(); descriptor = descriptor.getContainingDeclaration();
} }
return NamespaceCodegen.getJVMClassNameForKotlinNs(DescriptorUtils.getFQName(descriptor).toSafe()); return NamespaceCodegen.getJVMClassNameForKotlinNs(DescriptorUtils.getFQName(descriptor).toSafe()).getInternalName();
} }
public OwnerKind getContextKind() { public OwnerKind getContextKind() {
@@ -28,6 +28,7 @@ import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.lang.resolve.BindingContextUtils; import org.jetbrains.jet.lang.resolve.BindingContextUtils;
import org.jetbrains.jet.lang.resolve.DescriptorUtils; import org.jetbrains.jet.lang.resolve.DescriptorUtils;
import org.jetbrains.jet.lang.resolve.java.JvmAbi; import org.jetbrains.jet.lang.resolve.java.JvmAbi;
import org.jetbrains.jet.lang.resolve.java.JvmClassName;
import org.jetbrains.jet.lang.resolve.java.JvmStdlibNames; import org.jetbrains.jet.lang.resolve.java.JvmStdlibNames;
import org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverDescriptor; import org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverDescriptor;
import org.objectweb.asm.Label; import org.objectweb.asm.Label;
@@ -331,18 +332,18 @@ public class FunctionCodegen {
int flags = ACC_PUBLIC | ACC_SYNTHETIC; // TODO. int flags = ACC_PUBLIC | ACC_SYNTHETIC; // TODO.
String ownerInternalName; JvmClassName ownerInternalName;
if (contextClass instanceof NamespaceDescriptor) { if (contextClass instanceof NamespaceDescriptor) {
ownerInternalName = NamespaceCodegen.getJVMClassNameForKotlinNs(DescriptorUtils.getFQName(contextClass).toSafe()); ownerInternalName = NamespaceCodegen.getJVMClassNameForKotlinNs(DescriptorUtils.getFQName(contextClass).toSafe());
} }
else { else {
ownerInternalName = state.getInjector().getJetTypeMapper().mapType(((ClassDescriptor) contextClass).getDefaultType(), MapTypeMode.IMPL).getInternalName(); ownerInternalName = JvmClassName.byType(state.getInjector().getJetTypeMapper().mapType(((ClassDescriptor) contextClass).getDefaultType(), MapTypeMode.IMPL));
} }
String descriptor = jvmSignature.getDescriptor().replace(")","I)"); String descriptor = jvmSignature.getDescriptor().replace(")","I)");
boolean isConstructor = "<init>".equals(jvmSignature.getName()); boolean isConstructor = "<init>".equals(jvmSignature.getName());
if(!isStatic && !isConstructor) if(!isStatic && !isConstructor)
descriptor = descriptor.replace("(","(L" + ownerInternalName + ";"); descriptor = descriptor.replace("(", "(" + ownerInternalName.getDescriptor());
final MethodVisitor mv = v.newMethod(null, flags | (isConstructor ? 0 : ACC_STATIC), isConstructor ? "<init>" : jvmSignature.getName() + JvmAbi.DEFAULT_PARAMS_IMPL_SUFFIX, descriptor, null, null); final MethodVisitor mv = v.newMethod(null, flags | (isConstructor ? 0 : ACC_STATIC), isConstructor ? "<init>" : jvmSignature.getName() + JvmAbi.DEFAULT_PARAMS_IMPL_SUFFIX, descriptor, null, null);
InstructionAdapter iv = new InstructionAdapter(mv); InstructionAdapter iv = new InstructionAdapter(mv);
if (state.getClassBuilderMode() == ClassBuilderMode.STUBS) { if (state.getClassBuilderMode() == ClassBuilderMode.STUBS) {
@@ -425,17 +426,17 @@ public class FunctionCodegen {
if(!isStatic) { if(!isStatic) {
if(kind == OwnerKind.TRAIT_IMPL) { if(kind == OwnerKind.TRAIT_IMPL) {
iv.invokeinterface(ownerInternalName, jvmSignature.getName(), jvmSignature.getDescriptor()); iv.invokeinterface(ownerInternalName.getInternalName(), jvmSignature.getName(), jvmSignature.getDescriptor());
} }
else { else {
if(!isConstructor) if(!isConstructor)
iv.invokevirtual(ownerInternalName, jvmSignature.getName(), jvmSignature.getDescriptor()); iv.invokevirtual(ownerInternalName.getInternalName(), jvmSignature.getName(), jvmSignature.getDescriptor());
else else
iv.invokespecial(ownerInternalName, jvmSignature.getName(), jvmSignature.getDescriptor()); iv.invokespecial(ownerInternalName.getInternalName(), jvmSignature.getName(), jvmSignature.getDescriptor());
} }
} }
else { else {
iv.invokestatic(ownerInternalName, jvmSignature.getName(), jvmSignature.getDescriptor()); iv.invokestatic(ownerInternalName.getInternalName(), jvmSignature.getName(), jvmSignature.getDescriptor());
} }
iv.areturn(jvmSignature.getReturnType()); iv.areturn(jvmSignature.getReturnType());
@@ -149,7 +149,7 @@ public class JetTypeMapper {
String owner; String owner;
DeclarationDescriptor containingDeclaration = descriptor.getContainingDeclaration(); DeclarationDescriptor containingDeclaration = descriptor.getContainingDeclaration();
if (containingDeclaration instanceof NamespaceDescriptor) { if (containingDeclaration instanceof NamespaceDescriptor) {
owner = jvmClassNameForNamespace((NamespaceDescriptor) containingDeclaration); owner = jvmClassNameForNamespace((NamespaceDescriptor) containingDeclaration).getInternalName();
} }
else if (containingDeclaration instanceof ClassDescriptor) { else if (containingDeclaration instanceof ClassDescriptor) {
ClassDescriptor classDescriptor = (ClassDescriptor) containingDeclaration; ClassDescriptor classDescriptor = (ClassDescriptor) containingDeclaration;
@@ -207,7 +207,7 @@ public class JetTypeMapper {
} }
@NotNull @NotNull
private String jvmClassNameForNamespace(@NotNull NamespaceDescriptor namespace) { private JvmClassName jvmClassNameForNamespace(@NotNull NamespaceDescriptor namespace) {
StringBuilder r = new StringBuilder(); StringBuilder r = new StringBuilder();
@@ -242,7 +242,7 @@ public class JetTypeMapper {
throw new IllegalStateException("internal error: failed to generate classname for " + namespace); throw new IllegalStateException("internal error: failed to generate classname for " + namespace);
} }
return r.toString(); return JvmClassName.byInternalName(r.toString());
} }
@NotNull public Type mapReturnType(@NotNull final JetType jetType) { @NotNull public Type mapReturnType(@NotNull final JetType jetType) {
@@ -541,7 +541,7 @@ public class JetTypeMapper {
ClassDescriptor thisClass; ClassDescriptor thisClass;
if (functionParent instanceof NamespaceDescriptor) { if (functionParent instanceof NamespaceDescriptor) {
assert !superCall; assert !superCall;
owner = jvmClassNameForNamespace((NamespaceDescriptor) functionParent); owner = jvmClassNameForNamespace((NamespaceDescriptor) functionParent).getInternalName();
ownerForDefaultImpl = ownerForDefaultParam = owner; ownerForDefaultImpl = ownerForDefaultParam = owner;
invokeOpcode = INVOKESTATIC; invokeOpcode = INVOKESTATIC;
thisClass = null; thisClass = null;
@@ -25,6 +25,7 @@ import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.lang.resolve.FqName; import org.jetbrains.jet.lang.resolve.FqName;
import org.jetbrains.jet.lang.resolve.java.JavaDescriptorResolver; import org.jetbrains.jet.lang.resolve.java.JavaDescriptorResolver;
import org.jetbrains.jet.lang.resolve.java.JvmAbi; import org.jetbrains.jet.lang.resolve.java.JvmAbi;
import org.jetbrains.jet.lang.resolve.java.JvmClassName;
import org.objectweb.asm.MethodVisitor; import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Type; import org.objectweb.asm.Type;
import org.objectweb.asm.commons.InstructionAdapter; import org.objectweb.asm.commons.InstructionAdapter;
@@ -44,7 +45,7 @@ public class NamespaceCodegen {
v.defineClass(sourceFile, V1_6, v.defineClass(sourceFile, V1_6,
ACC_PUBLIC/*|ACC_SUPER*/, ACC_PUBLIC/*|ACC_SUPER*/,
getJVMClassNameForKotlinNs(fqName), getJVMClassNameForKotlinNs(fqName).getInternalName(),
null, null,
//"jet/lang/Namespace", //"jet/lang/Namespace",
"java/lang/Object", "java/lang/Object",
@@ -137,9 +138,10 @@ public class NamespaceCodegen {
v.done(); v.done();
} }
public static String getJVMClassNameForKotlinNs(@NotNull FqName fqName) { @NotNull
public static JvmClassName getJVMClassNameForKotlinNs(@NotNull FqName fqName) {
if (fqName.isRoot()) { if (fqName.isRoot()) {
return JvmAbi.PACKAGE_CLASS; return JvmClassName.byInternalName(JvmAbi.PACKAGE_CLASS);
} }
String name = fqName.getFqName().replace('.', '/'); String name = fqName.getFqName().replace('.', '/');
@@ -147,6 +149,6 @@ public class NamespaceCodegen {
name = name.substring(JavaDescriptorResolver.JAVA_ROOT.length() + 1, name.length()); name = name.substring(JavaDescriptorResolver.JAVA_ROOT.length() + 1, name.length());
} }
name += "/" + JvmAbi.PACKAGE_CLASS; name += "/" + JvmAbi.PACKAGE_CLASS;
return name; return JvmClassName.byInternalName(name);
} }
} }
@@ -42,6 +42,14 @@ public class JvmClassName {
return r; return r;
} }
public static JvmClassName byType(@NotNull Type type) {
if (type.getSort() != Type.OBJECT) {
throw new IllegalArgumentException(
"must be an object to be converted to " + JvmClassName.class.getSimpleName());
}
return byInternalName(type.getInternalName());
}
@NotNull @NotNull
public FqName getFqName() { public FqName getFqName() {
return fqName; return fqName;
@@ -119,7 +119,7 @@ public abstract class CodegenTestCase extends JetLiteFixture {
GeneratedClassLoader loader = createClassLoader(codegens); GeneratedClassLoader loader = createClassLoader(codegens);
try { try {
String fqName = NamespaceCodegen.getJVMClassNameForKotlinNs(JetPsiUtil.getFQName(myFile)).replace("/", "."); String fqName = NamespaceCodegen.getJVMClassNameForKotlinNs(JetPsiUtil.getFQName(myFile)).getFqName().getFqName();
Class<?> namespaceClass = loader.loadClass(fqName); Class<?> namespaceClass = loader.loadClass(fqName);
Method method = namespaceClass.getMethod("box"); Method method = namespaceClass.getMethod("box");
return (String) method.invoke(null); return (String) method.invoke(null);
@@ -163,7 +163,7 @@ public abstract class CodegenTestCase extends JetLiteFixture {
} }
protected Class loadRootNamespaceClass(@NotNull ClassFileFactory state) { protected Class loadRootNamespaceClass(@NotNull ClassFileFactory state) {
String fqName = NamespaceCodegen.getJVMClassNameForKotlinNs(JetPsiUtil.getFQName(myFile)).replace("/", "."); String fqName = NamespaceCodegen.getJVMClassNameForKotlinNs(JetPsiUtil.getFQName(myFile)).getFqName().getFqName();
try { try {
return createClassLoader(state).loadClass(fqName); return createClassLoader(state).loadClass(fqName);
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
@@ -146,7 +146,7 @@ public class StdlibTest extends CodegenTestCase {
GeneratedClassLoader loader = createClassLoader(codegens); GeneratedClassLoader loader = createClassLoader(codegens);
try { try {
String fqName = NamespaceCodegen.getJVMClassNameForKotlinNs(JetPsiUtil.getFQName(myFile)).replace("/", "."); String fqName = NamespaceCodegen.getJVMClassNameForKotlinNs(JetPsiUtil.getFQName(myFile)).getFqName().getFqName();
Class<?> namespaceClass = loader.loadClass(fqName); Class<?> namespaceClass = loader.loadClass(fqName);
Method method = namespaceClass.getMethod("box", Method.class); Method method = namespaceClass.getMethod("box", Method.class);
method.setAccessible(true); method.setAccessible(true);
@@ -137,10 +137,10 @@ public class JetPositionManager implements PositionManager {
else { else {
JetFile namespace = PsiTreeUtil.getParentOfType(sourcePosition.getElementAt(), JetFile.class); JetFile namespace = PsiTreeUtil.getParentOfType(sourcePosition.getElementAt(), JetFile.class);
if (namespace != null) { if (namespace != null) {
names.add(NamespaceCodegen.getJVMClassNameForKotlinNs(JetPsiUtil.getFQName(namespace))); names.add(NamespaceCodegen.getJVMClassNameForKotlinNs(JetPsiUtil.getFQName(namespace)).getInternalName());
} }
else { else {
names.add(NamespaceCodegen.getJVMClassNameForKotlinNs(JetPsiUtil.getFQName(file))); names.add(NamespaceCodegen.getJVMClassNameForKotlinNs(JetPsiUtil.getFQName(file)).getInternalName());
} }
} }
} }