Fix for KT-6424
Remove "SuppressWarnings" and "//noinspection" on java to kotlin conversion
This commit is contained in:
@@ -29,8 +29,8 @@ import java.lang.annotation.Target
|
|||||||
|
|
||||||
class AnnotationConverter(private val converter: Converter) {
|
class AnnotationConverter(private val converter: Converter) {
|
||||||
private val annotationsToRemove: Set<String> = (NullableNotNullManager.getInstance(converter.project).notNulls
|
private val annotationsToRemove: Set<String> = (NullableNotNullManager.getInstance(converter.project).notNulls
|
||||||
+ NullableNotNullManager.getInstance(converter.project).nullables
|
+ NullableNotNullManager.getInstance(converter.project).nullables
|
||||||
+ listOf(CommonClassNames.JAVA_LANG_OVERRIDE, ElementType::class.java.name)).toSet()
|
+ listOf(CommonClassNames.JAVA_LANG_OVERRIDE, ElementType::class.java.name, SuppressWarnings::class.java.name)).toSet()
|
||||||
|
|
||||||
fun isImportNotRequired(fqName: FqName): Boolean {
|
fun isImportNotRequired(fqName: FqName): Boolean {
|
||||||
val nameAsString = fqName.asString()
|
val nameAsString = fqName.asString()
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ import org.jetbrains.kotlin.name.FqName
|
|||||||
import org.jetbrains.kotlin.psi.psiUtil.isAncestor
|
import org.jetbrains.kotlin.psi.psiUtil.isAncestor
|
||||||
import java.util.*
|
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()) {
|
if (generators.isNotEmpty()) {
|
||||||
append(prefix)
|
append(prefix)
|
||||||
var first = true
|
var first = true
|
||||||
@@ -51,6 +51,8 @@ fun CodeBuilder.append(elements: Collection<Element>, separator: String, prefix:
|
|||||||
class ElementCreationStackTraceRequiredException : RuntimeException()
|
class ElementCreationStackTraceRequiredException : RuntimeException()
|
||||||
|
|
||||||
class CodeBuilder(private val topElement: PsiElement?, private var docConverter: DocCommentConverter) {
|
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 val builder = StringBuilder()
|
||||||
private var endOfLineCommentAtEnd = false
|
private var endOfLineCommentAtEnd = false
|
||||||
|
|
||||||
@@ -78,7 +80,9 @@ class CodeBuilder(private val topElement: PsiElement?, private var docConverter:
|
|||||||
append(docConverter.convertDocComment(element))
|
append(docConverter.convertDocComment(element))
|
||||||
}
|
}
|
||||||
else {
|
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) {
|
if (commentInfo.isComment) {
|
||||||
// Original comment was first in line, but there's no line break before the current one
|
// Original comment was first in line, but there's no line break before the current one
|
||||||
if (!commentInfo.isPostInsert && commentInfo.isFirstNonWhitespaceElementInLine &&
|
if (!commentInfo.isPostInsert && commentInfo.isFirstNonWhitespaceElementInLine &&
|
||||||
!builder.takeLastWhile { it.isWhitespace() }.contains('\n')) {
|
!builder.takeLastWhile { it.isWhitespace() }.contains('\n')) {
|
||||||
builder.append('\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
|
// 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!!) {
|
for ((prototype, inheritance) in element.prototypes!!) {
|
||||||
if (inheritance.commentsInside) {
|
if (inheritance.commentsInside) {
|
||||||
prototype.accept(object : JavaRecursiveElementVisitor(){
|
prototype.accept(object : JavaRecursiveElementVisitor() {
|
||||||
override fun visitComment(comment: PsiComment) {
|
override fun visitComment(comment: PsiComment) {
|
||||||
if (comment !in notInsideElements && commentsAndSpacesUsed.add(comment)) {
|
if (comment !in notInsideElements && commentsAndSpacesUsed.add(comment)) {
|
||||||
appendCommentOrWhiteSpace(comment, true)
|
appendCommentOrWhiteSpace(comment, true)
|
||||||
@@ -272,7 +276,7 @@ class CodeBuilder(private val topElement: PsiElement?, private var docConverter:
|
|||||||
collectCommentsAndSpacesBefore(prev)
|
collectCommentsAndSpacesBefore(prev)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (prev.isEmptyElement()){
|
else if (prev.isEmptyElement()) {
|
||||||
collectCommentsAndSpacesBefore(prev)
|
collectCommentsAndSpacesBefore(prev)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -294,7 +298,7 @@ class CodeBuilder(private val topElement: PsiElement?, private var docConverter:
|
|||||||
collectCommentsAndSpacesAfter(next)
|
collectCommentsAndSpacesAfter(next)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (next.isEmptyElement()){
|
else if (next.isEmptyElement()) {
|
||||||
collectCommentsAndSpacesAfter(next)
|
collectCommentsAndSpacesAfter(next)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -306,7 +310,7 @@ class CodeBuilder(private val topElement: PsiElement?, private var docConverter:
|
|||||||
|
|
||||||
private fun MutableList<PsiElement>.collectCommentsAndSpacesAtStart(element: PsiElement): MutableList<PsiElement> {
|
private fun MutableList<PsiElement>.collectCommentsAndSpacesAtStart(element: PsiElement): MutableList<PsiElement> {
|
||||||
var child = element.firstChild
|
var child = element.firstChild
|
||||||
while(child != null) {
|
while (child != null) {
|
||||||
if (child.isCommentOrSpace()) {
|
if (child.isCommentOrSpace()) {
|
||||||
if (child !in commentsAndSpacesUsed) add(child) else break
|
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> {
|
private fun MutableList<PsiElement>.collectCommentsAndSpacesAtEnd(element: PsiElement): MutableList<PsiElement> {
|
||||||
var child = element.lastChild
|
var child = element.lastChild
|
||||||
while(child != null) {
|
while (child != null) {
|
||||||
if (child.isCommentOrSpace()) {
|
if (child.isCommentOrSpace()) {
|
||||||
if (child !in commentsAndSpacesUsed) add(child) else break
|
if (child !in commentsAndSpacesUsed) add(child) else break
|
||||||
}
|
}
|
||||||
@@ -335,7 +339,7 @@ class CodeBuilder(private val topElement: PsiElement?, private var docConverter:
|
|||||||
}
|
}
|
||||||
|
|
||||||
private companion object {
|
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 {
|
when {
|
||||||
isEmpty() -> return other
|
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)
|
return if (size <= 1)
|
||||||
this
|
this
|
||||||
else
|
else
|
||||||
|
|||||||
@@ -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){
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
class A {
|
||||||
|
|
||||||
|
var b = 0
|
||||||
|
|
||||||
|
fun a(i: Int) {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
public class Check {
|
||||||
|
public void a() {
|
||||||
|
//noinspection RedundantStringConstructorCall
|
||||||
|
Integer d = (Integer) (Object)(new String());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
class Check {
|
||||||
|
fun a() {
|
||||||
|
|
||||||
|
val d = String() as Int
|
||||||
|
}
|
||||||
|
}
|
||||||
+1
-3
@@ -1,3 +1 @@
|
|||||||
import java.lang.SuppressWarnings
|
internal class C(@Deprecated("") private val p1: Int, @Deprecated("") private val myP2: Int, @Deprecated("") var p3: Int)
|
||||||
|
|
||||||
internal class C(@Deprecated("") private val p1: Int, @Deprecated("") private val myP2: Int, @Deprecated("") @SuppressWarnings("x") var p3: Int)
|
|
||||||
|
|||||||
@@ -126,6 +126,12 @@ public class JavaToKotlinConverterForWebDemoTestGenerated extends AbstractJavaTo
|
|||||||
String fileName = KotlinTestUtils.navigationMetadata("j2k/testData/fileOrElement/annotations/serialVersionUID.java");
|
String fileName = KotlinTestUtils.navigationMetadata("j2k/testData/fileOrElement/annotations/serialVersionUID.java");
|
||||||
doTest(fileName);
|
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")
|
@TestMetadata("j2k/testData/fileOrElement/anonymousBlock")
|
||||||
@@ -1047,6 +1053,12 @@ public class JavaToKotlinConverterForWebDemoTestGenerated extends AbstractJavaTo
|
|||||||
String fileName = KotlinTestUtils.navigationMetadata("j2k/testData/fileOrElement/comments/fieldsInitializedFromParams.java");
|
String fileName = KotlinTestUtils.navigationMetadata("j2k/testData/fileOrElement/comments/fieldsInitializedFromParams.java");
|
||||||
doTest(fileName);
|
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")
|
@TestMetadata("j2k/testData/fileOrElement/conditionalExpression")
|
||||||
|
|||||||
@@ -126,6 +126,12 @@ public class JavaToKotlinConverterSingleFileTestGenerated extends AbstractJavaTo
|
|||||||
String fileName = KotlinTestUtils.navigationMetadata("j2k/testData/fileOrElement/annotations/serialVersionUID.java");
|
String fileName = KotlinTestUtils.navigationMetadata("j2k/testData/fileOrElement/annotations/serialVersionUID.java");
|
||||||
doTest(fileName);
|
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")
|
@TestMetadata("j2k/testData/fileOrElement/anonymousBlock")
|
||||||
@@ -1047,6 +1053,12 @@ public class JavaToKotlinConverterSingleFileTestGenerated extends AbstractJavaTo
|
|||||||
String fileName = KotlinTestUtils.navigationMetadata("j2k/testData/fileOrElement/comments/fieldsInitializedFromParams.java");
|
String fileName = KotlinTestUtils.navigationMetadata("j2k/testData/fileOrElement/comments/fieldsInitializedFromParams.java");
|
||||||
doTest(fileName);
|
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")
|
@TestMetadata("j2k/testData/fileOrElement/conditionalExpression")
|
||||||
|
|||||||
Reference in New Issue
Block a user