Fix for KT-6424

Remove "SuppressWarnings" and "//noinspection" on java to kotlin conversion
This commit is contained in:
Simon Ogorodnik
2016-10-12 17:49:32 +03:00
parent 47b3420a65
commit 155cfcb1f8
9 changed files with 77 additions and 15 deletions

View File

@@ -29,8 +29,8 @@ import java.lang.annotation.Target
class AnnotationConverter(private val converter: Converter) {
private val annotationsToRemove: Set<String> = (NullableNotNullManager.getInstance(converter.project).notNulls
+ NullableNotNullManager.getInstance(converter.project).nullables
+ listOf(CommonClassNames.JAVA_LANG_OVERRIDE, ElementType::class.java.name)).toSet()
+ NullableNotNullManager.getInstance(converter.project).nullables
+ listOf(CommonClassNames.JAVA_LANG_OVERRIDE, ElementType::class.java.name, SuppressWarnings::class.java.name)).toSet()
fun isImportNotRequired(fqName: FqName): Boolean {
val nameAsString = fqName.asString()

View File

@@ -27,7 +27,7 @@ import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.psi.psiUtil.isAncestor
import java.util.*
fun<T> CodeBuilder.buildList(generators: Collection<() -> T>, separator: String, prefix: String = "", suffix: String = ""): CodeBuilder {
fun <T> CodeBuilder.buildList(generators: Collection<() -> T>, separator: String, prefix: String = "", suffix: String = ""): CodeBuilder {
if (generators.isNotEmpty()) {
append(prefix)
var first = true
@@ -51,6 +51,8 @@ fun CodeBuilder.append(elements: Collection<Element>, separator: String, prefix:
class ElementCreationStackTraceRequiredException : RuntimeException()
class CodeBuilder(private val topElement: PsiElement?, private var docConverter: DocCommentConverter) {
private val commentPatternsToDrop = listOf("^//[ ]*noinspection[ ]+[A-Za-z][A-Za-z0-9_]*([ ].*?)?$".toRegex())
private val builder = StringBuilder()
private var endOfLineCommentAtEnd = false
@@ -78,7 +80,9 @@ class CodeBuilder(private val topElement: PsiElement?, private var docConverter:
append(docConverter.convertDocComment(element))
}
else {
append(element.text!!, CommentInfo(element, postInsert))
if (element !is PsiComment || !commentPatternsToDrop.any { it.matches(element.text) }) {
append(element.text!!, CommentInfo(element, postInsert))
}
}
}
@@ -98,7 +102,7 @@ class CodeBuilder(private val topElement: PsiElement?, private var docConverter:
if (commentInfo.isComment) {
// Original comment was first in line, but there's no line break before the current one
if (!commentInfo.isPostInsert && commentInfo.isFirstNonWhitespaceElementInLine &&
!builder.takeLastWhile { it.isWhitespace() }.contains('\n')) {
!builder.takeLastWhile { it.isWhitespace() }.contains('\n')) {
builder.append('\n')
}
@@ -165,7 +169,7 @@ class CodeBuilder(private val topElement: PsiElement?, private var docConverter:
// scan for all comments inside which are not yet used in the text and put them here to not loose any comment from code
for ((prototype, inheritance) in element.prototypes!!) {
if (inheritance.commentsInside) {
prototype.accept(object : JavaRecursiveElementVisitor(){
prototype.accept(object : JavaRecursiveElementVisitor() {
override fun visitComment(comment: PsiComment) {
if (comment !in notInsideElements && commentsAndSpacesUsed.add(comment)) {
appendCommentOrWhiteSpace(comment, true)
@@ -272,7 +276,7 @@ class CodeBuilder(private val topElement: PsiElement?, private var docConverter:
collectCommentsAndSpacesBefore(prev)
}
}
else if (prev.isEmptyElement()){
else if (prev.isEmptyElement()) {
collectCommentsAndSpacesBefore(prev)
}
}
@@ -294,7 +298,7 @@ class CodeBuilder(private val topElement: PsiElement?, private var docConverter:
collectCommentsAndSpacesAfter(next)
}
}
else if (next.isEmptyElement()){
else if (next.isEmptyElement()) {
collectCommentsAndSpacesAfter(next)
}
}
@@ -306,7 +310,7 @@ class CodeBuilder(private val topElement: PsiElement?, private var docConverter:
private fun MutableList<PsiElement>.collectCommentsAndSpacesAtStart(element: PsiElement): MutableList<PsiElement> {
var child = element.firstChild
while(child != null) {
while (child != null) {
if (child.isCommentOrSpace()) {
if (child !in commentsAndSpacesUsed) add(child) else break
}
@@ -321,7 +325,7 @@ class CodeBuilder(private val topElement: PsiElement?, private var docConverter:
private fun MutableList<PsiElement>.collectCommentsAndSpacesAtEnd(element: PsiElement): MutableList<PsiElement> {
var child = element.lastChild
while(child != null) {
while (child != null) {
if (child.isCommentOrSpace()) {
if (child !in commentsAndSpacesUsed) add(child) else break
}
@@ -335,7 +339,7 @@ class CodeBuilder(private val topElement: PsiElement?, private var docConverter:
}
private companion object {
operator fun<T> List<T>.plus(other: List<T>): List<T> {
operator fun <T> List<T>.plus(other: List<T>): List<T> {
when {
isEmpty() -> return other
@@ -350,7 +354,7 @@ class CodeBuilder(private val topElement: PsiElement?, private var docConverter:
}
}
fun<T> List<T>.reversed(): List<T> {
fun <T> List<T>.reversed(): List<T> {
return if (size <= 1)
this
else

View File

@@ -0,0 +1,16 @@
@SuppressWarnings("ALL")
public class A{
@SuppressWarnings("ALL")
public A(){
}
@SuppressWarnings("ALL")
public int b = 0;
@SuppressWarnings("ALL")
public void a(@SuppressWarnings("ALL") int i){
}
}

View File

@@ -0,0 +1,8 @@
class A {
var b = 0
fun a(i: Int) {
}
}

View File

@@ -0,0 +1,6 @@
public class Check {
public void a() {
//noinspection RedundantStringConstructorCall
Integer d = (Integer) (Object)(new String());
}
}

View File

@@ -0,0 +1,6 @@
class Check {
fun a() {
val d = String() as Int
}
}

View File

@@ -1,3 +1 @@
import java.lang.SuppressWarnings
internal class C(@Deprecated("") private val p1: Int, @Deprecated("") private val myP2: Int, @Deprecated("") @SuppressWarnings("x") var p3: Int)
internal class C(@Deprecated("") private val p1: Int, @Deprecated("") private val myP2: Int, @Deprecated("") var p3: Int)

View File

@@ -126,6 +126,12 @@ public class JavaToKotlinConverterForWebDemoTestGenerated extends AbstractJavaTo
String fileName = KotlinTestUtils.navigationMetadata("j2k/testData/fileOrElement/annotations/serialVersionUID.java");
doTest(fileName);
}
@TestMetadata("suppressWarnings.java")
public void testSuppressWarnings() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("j2k/testData/fileOrElement/annotations/suppressWarnings.java");
doTest(fileName);
}
}
@TestMetadata("j2k/testData/fileOrElement/anonymousBlock")
@@ -1047,6 +1053,12 @@ public class JavaToKotlinConverterForWebDemoTestGenerated extends AbstractJavaTo
String fileName = KotlinTestUtils.navigationMetadata("j2k/testData/fileOrElement/comments/fieldsInitializedFromParams.java");
doTest(fileName);
}
@TestMetadata("removeNoInspectionComment.java")
public void testRemoveNoInspectionComment() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("j2k/testData/fileOrElement/comments/removeNoInspectionComment.java");
doTest(fileName);
}
}
@TestMetadata("j2k/testData/fileOrElement/conditionalExpression")

View File

@@ -126,6 +126,12 @@ public class JavaToKotlinConverterSingleFileTestGenerated extends AbstractJavaTo
String fileName = KotlinTestUtils.navigationMetadata("j2k/testData/fileOrElement/annotations/serialVersionUID.java");
doTest(fileName);
}
@TestMetadata("suppressWarnings.java")
public void testSuppressWarnings() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("j2k/testData/fileOrElement/annotations/suppressWarnings.java");
doTest(fileName);
}
}
@TestMetadata("j2k/testData/fileOrElement/anonymousBlock")
@@ -1047,6 +1053,12 @@ public class JavaToKotlinConverterSingleFileTestGenerated extends AbstractJavaTo
String fileName = KotlinTestUtils.navigationMetadata("j2k/testData/fileOrElement/comments/fieldsInitializedFromParams.java");
doTest(fileName);
}
@TestMetadata("removeNoInspectionComment.java")
public void testRemoveNoInspectionComment() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("j2k/testData/fileOrElement/comments/removeNoInspectionComment.java");
doTest(fileName);
}
}
@TestMetadata("j2k/testData/fileOrElement/conditionalExpression")