Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Class, method, and constructor declarations #307

Merged
merged 23 commits into from
Jan 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
74a9538
Function declarations
sschr15 Sep 5, 2023
f5106b9
Fix top-level functions
sschr15 Sep 5, 2023
c369e45
Constructor support + some code cleanup
sschr15 Sep 6, 2023
a81b766
Commit test changes
sschr15 Sep 6, 2023
e327efc
fix duplicated super calls in default constructors
sschr15 Sep 7, 2023
a0cd95d
Generify InvocationExprent parameter removal
sschr15 Sep 7, 2023
da45b13
Completely redo kotlin annotation declarations (again)
sschr15 Sep 9, 2023
f1d724b
Update metadata
sschr15 Sep 12, 2023
cdaab01
Add `override` guessing and context receivers
sschr15 Sep 12, 2023
6c9e798
Kotlin's Contracts!
sschr15 Sep 14, 2023
cf02fb6
Minor code cleanup
sschr15 Sep 14, 2023
951c4ef
Update from upstream changes
sschr15 Sep 14, 2023
e6fe906
Update contract test
sschr15 Sep 14, 2023
bf31699
Fix constructor oddities
sschr15 Sep 14, 2023
b25c7fa
Commit constructor test changes
sschr15 Sep 14, 2023
2754365
Import InvocationKind
sschr15 Sep 14, 2023
e833d9e
Fix some bugs with suspend functions and odd constructor oddities
sschr15 Oct 1, 2023
6dc4b37
Merge remote-tracking branch 'origin/develop/1.10.0' into feature/kot…
sschr15 Nov 13, 2023
619caba
Trim metadata jar + fix Kotlin test
sschr15 Nov 13, 2023
4a6c391
Fix a handful of missing method issues and clarify error
sschr15 Nov 13, 2023
7f4ce94
Fix a contract issue with receiver types
sschr15 Nov 13, 2023
79836ee
Merge remote-tracking branch 'origin/develop/1.10.0' into feature/kot…
sschr15 Nov 17, 2023
ce4c798
Merge branch 'develop/1.10.0' into feature/kotlin/declarations
sschr15 Jan 21, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified plugins/kotlin/libs/metadata.jar
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
package org.vineflower.kotlin;

import kotlin.reflect.jvm.internal.impl.metadata.ProtoBuf;
import kotlin.reflect.jvm.internal.impl.metadata.jvm.JvmProtoBuf;
import kotlin.reflect.jvm.internal.impl.protobuf.ExtensionRegistryLite;
import kotlinx.metadata.internal.metadata.ProtoBuf;
import kotlinx.metadata.internal.metadata.jvm.JvmProtoBuf;
import kotlinx.metadata.internal.protobuf.ExtensionRegistryLite;
import org.jetbrains.java.decompiler.api.plugin.LanguageChooser;
import org.jetbrains.java.decompiler.main.DecompilerContext;
import org.jetbrains.java.decompiler.main.extern.IFernflowerLogger;
import org.jetbrains.java.decompiler.util.Key;
import org.vineflower.kotlin.metadata.BitEncoding;
import org.jetbrains.java.decompiler.modules.decompiler.exps.AnnotationExprent;
import org.jetbrains.java.decompiler.modules.decompiler.exps.ConstExprent;
import org.jetbrains.java.decompiler.modules.decompiler.exps.Exprent;
import org.jetbrains.java.decompiler.modules.decompiler.exps.NewExprent;
import org.jetbrains.java.decompiler.struct.StructClass;
import org.jetbrains.java.decompiler.struct.attr.StructAnnotationAttribute;
import org.jetbrains.java.decompiler.struct.attr.StructGeneralAttribute;
import org.jetbrains.java.decompiler.util.Key;
import org.vineflower.kotlin.metadata.BitEncoding;
import org.vineflower.kotlin.metadata.MetadataNameResolver;

import java.io.ByteArrayInputStream;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.vineflower.kotlin;

import kotlin.reflect.jvm.internal.impl.metadata.ProtoBuf;
import kotlinx.metadata.internal.metadata.ProtoBuf;
import org.jetbrains.java.decompiler.main.DecompilerContext;
import org.jetbrains.java.decompiler.util.Key;
import org.vineflower.kotlin.metadata.MetadataNameResolver;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
package org.vineflower.kotlin;

import org.jetbrains.java.decompiler.main.ClassesProcessor;
import org.jetbrains.java.decompiler.main.DecompilerContext;
import org.jetbrains.java.decompiler.main.collectors.ImportCollector;
import org.jetbrains.java.decompiler.main.extern.IFernflowerPreferences;
import org.jetbrains.java.decompiler.modules.decompiler.ExprProcessor;
import org.jetbrains.java.decompiler.struct.StructContext;
import org.jetbrains.java.decompiler.util.TextBuffer;
import org.vineflower.kotlin.util.KTypes;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class KotlinImportCollector {
private static final String[] AUTO_KOTLIN_IMPORTS = {
public class KotlinImportCollector extends ImportCollector {
public static final String[] AUTO_KOTLIN_IMPORTS = {
"annotation",
"collections",
"comparisons",
Expand All @@ -17,45 +26,48 @@ public class KotlinImportCollector {
"text",
};

private final ImportCollector parent;

public KotlinImportCollector(ImportCollector parent) {
this.parent = parent;
}
super(parent);

public void writeImports(TextBuffer buffer, boolean addSeparator) {
TextBuffer buf = new TextBuffer();
parent.writeImports(buf, false);
String[] imports = buf.convertToStringAndAllowDataDiscard().split("\n");
boolean imported = false;
for (String line : imports) {
if (line.isBlank()) {
continue;
}
line = line.trim();
String importLine = line.substring(7, line.length() - 1);
String[] parts = importLine.split("\\.");

// Don't include automatic kotlin imports
if (parts.length == 2 && parts[0].equals("kotlin")) {
continue;
} else if (parts.length == 3 && parts[0].equals("kotlin") && Arrays.binarySearch(AUTO_KOTLIN_IMPORTS, parts[1]) >= 0) {
continue;
// Any class that Kotlin "overrides" requires explicit non-imported references
for (String className : KTypes.KOTLIN_TO_JAVA_LANG.keySet()) {
String simpleName = className.substring(className.lastIndexOf('/') + 1);
String packageName = className.substring(0, className.lastIndexOf('/')).replace('/', '.');
if (!mapSimpleNames.containsKey(simpleName)) {
mapSimpleNames.put(simpleName, packageName);
}
}

buffer.append("import ");
boolean first = true;
for (String part : parts) {
if (!first) {
buffer.append(".");
}
first = false;
buffer.append(KotlinWriter.toValidKotlinIdentifier(part));
for (String className : KTypes.KOTLIN_TO_JAVA_UTIL.keySet()) {
String simpleName = className.substring(className.lastIndexOf('/') + 1);
String packageName = className.substring(0, className.lastIndexOf('/')).replace('/', '.');
if (!mapSimpleNames.containsKey(simpleName)) {
mapSimpleNames.put(simpleName, packageName);
}
buffer.appendLineSeparator();
imported = true;
}
if (imported && addSeparator) {
}

@Override
protected boolean keepImport(Map.Entry<String, String> ent) {
if (!super.keepImport(ent)) return false;
if (ent.getValue().equals("kotlin")) return false;
for (String autoImport : AUTO_KOTLIN_IMPORTS) {
if (ent.getValue().equals("kotlin." + autoImport)) return false;
}
return true;
}

@Override
public void writeImports(TextBuffer buffer, boolean addSeparator) {
if (DecompilerContext.getOption(IFernflowerPreferences.REMOVE_IMPORTS)) {
return;
}

List<String> imports = packImports();
for (String imp : imports) {
buffer.append("import ").append(imp).appendLineSeparator();
}
if (addSeparator && !imports.isEmpty()) {
buffer.appendLineSeparator();
}
}
Expand Down
Loading