Skip to content

Commit

Permalink
Static inner class returns false when isStatic is used #192 (#204)
Browse files Browse the repository at this point in the history
* Static inner class returns false when isStatic is used #192

* fix tests
  • Loading branch information
lehvolk authored Nov 27, 2023
1 parent 00e2a3d commit 0d51aaf
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class JcClassOrInterfaceImpl(

override val innerClasses: List<JcClassOrInterface>
get() {
return info.innerClasses.map {
return info.innerClasses.filter { it != name }.map {
classpath.findClass(it)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,16 @@ package org.jacodb.impl.fs
import kotlinx.collections.immutable.toImmutableList
import org.jacodb.api.ClassSource
import org.jacodb.impl.storage.AnnotationValueKind
import org.jacodb.impl.types.AnnotationInfo
import org.jacodb.impl.types.AnnotationValue
import org.jacodb.impl.types.AnnotationValueList
import org.jacodb.impl.types.ClassInfo
import org.jacodb.impl.types.ClassRef
import org.jacodb.impl.types.EnumRef
import org.jacodb.impl.types.FieldInfo
import org.jacodb.impl.types.MethodInfo
import org.jacodb.impl.types.OuterClassRef
import org.jacodb.impl.types.ParameterInfo
import org.jacodb.impl.types.PrimitiveValue
import org.jacodb.impl.types.*
import org.objectweb.asm.ClassReader
import org.objectweb.asm.Opcodes
import org.objectweb.asm.Type
import org.objectweb.asm.tree.AnnotationNode
import org.objectweb.asm.tree.ClassNode
import org.objectweb.asm.tree.FieldNode
import org.objectweb.asm.tree.MethodNode
import org.objectweb.asm.tree.TypeAnnotationNode
import org.objectweb.asm.tree.*

fun ClassNode.asClassInfo(bytecode: ByteArray) = ClassInfo(
name = Type.getObjectType(name).className,
signature = signature,
access = access,
access = innerClasses?.firstOrNull { it.name == name }?.access ?: access,

outerClass = outerClassRef(),
innerClasses = innerClasses.map {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ fun JcClassOrInterface.directTypeParameters(): List<JvmTypeParameterDeclaration>
*/
fun JcClassOrInterface.allVisibleTypeParameters(): Map<String, JvmTypeParameterDeclaration> {
val direct = typeParameters.associateBy { it.symbol }
val fromMethod = outerMethod?.allVisibleTypeParameters().orEmpty()
if (!isStatic) {
val fromOuter = outerClass?.allVisibleTypeParameters()
val fromMethod = outerMethod?.allVisibleTypeParameters()
return ((fromMethod ?: fromOuter).orEmpty() + direct).toPersistentMap()
val fromOuter = outerClass?.allVisibleTypeParameters().orEmpty()
return (direct + fromOuter + fromMethod).toPersistentMap()
}
return direct
return (direct + fromMethod).toPersistentMap()
}

fun JcMethod.allVisibleTypeParameters(): Map<String, JvmTypeParameterDeclaration> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2022 UnitTestBot contributors (utbot.org)
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.jacodb.testing.types;

public class AAA {

public class BBB {

}

static public class CCC {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import org.jacodb.testing.*
import org.jacodb.testing.hierarchies.Creature
import org.jacodb.testing.structure.FieldsAndMethods
import org.jacodb.testing.structure.HiddenFieldSuperClass.HiddenFieldSuccClass
import org.jacodb.testing.types.AAA
import org.jacodb.testing.types.AAA.CCC
import org.jacodb.testing.usages.Generics
import org.jacodb.testing.usages.HelloWorldAnonymousClasses
import org.jacodb.testing.usages.WithInner
Expand Down Expand Up @@ -601,6 +603,24 @@ abstract class DatabaseEnvTest {
assertTrue(hiddenFieldSuccClass.toType().fields.size == hiddenFieldSuccClass.fields.size)
}

@Test
fun `static flag on classes`() {
val aaa = cp.findClass<AAA>()

val bbb = cp.findClass<AAA.BBB>()
val ccc = cp.findClass<CCC>()
assertFalse(bbb.isStatic)
assertTrue(ccc.isStatic)

assertTrue(ccc.innerClasses.isEmpty())
assertTrue(bbb.innerClasses.isEmpty())

val inners = aaa.innerClasses.toList()
assertEquals(2, inners.size)
assertTrue(inners.first { it.name.contains("CCC") }.isStatic)
assertFalse(inners.first { it.name.contains("BBB") }.isStatic)
}

private inline fun <reified T> findSubClasses(allHierarchy: Boolean = false): Sequence<JcClassOrInterface> {
return hierarchyExt.findSubClasses(T::class.java.name, allHierarchy)
}
Expand Down

0 comments on commit 0d51aaf

Please sign in to comment.