Skip to content

Commit caba5d1

Browse files
committed
Fix review comments #2
1 parent 0280e3d commit caba5d1

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/model/constructor/tree/CgCallableAccessManager.kt

+23-7
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@ import org.utbot.framework.codegen.Junit5
55
import org.utbot.framework.codegen.TestNg
66
import org.utbot.framework.codegen.model.constructor.builtin.any
77
import org.utbot.framework.codegen.model.constructor.builtin.anyOfClass
8-
import org.utbot.framework.codegen.model.constructor.builtin.forName
9-
import org.utbot.framework.codegen.model.constructor.builtin.getDeclaredConstructor
10-
import org.utbot.framework.codegen.model.constructor.builtin.getDeclaredMethod
118
import org.utbot.framework.codegen.model.constructor.builtin.getMethodId
129
import org.utbot.framework.codegen.model.constructor.builtin.getTargetException
1310
import org.utbot.framework.codegen.model.constructor.builtin.invoke
@@ -271,15 +268,15 @@ internal class CgCallableAccessManagerImpl(val context: CgContext) : CgCallableA
271268
}
272269

273270
if (this.isStatic && caller != null && codegenLanguage == CodegenLanguage.KOTLIN) {
274-
error("In Kotlin, unlike Java, static methods cannot be called on an object")
271+
error("In Kotlin, unlike Java, static methods cannot be called on an instance: $this")
275272
}
276273

277274
// If method is from current test class, then it may not have a caller.
278275
if (this.classId == currentTestClass && caller == null) {
279276
return true
280277
}
281278

282-
requireNotNull(caller) { "Method must have a caller, unless it is the method of the current test class or a static method" }
279+
requireNotNull(caller) { "Method must have a caller, unless it is the method of the current test class or a static method: $this" }
283280
return caller canBeReceiverOf this
284281
}
285282

@@ -295,7 +292,7 @@ internal class CgCallableAccessManagerImpl(val context: CgContext) : CgCallableA
295292
}
296293

297294
if (this.isStatic && accessor != null && codegenLanguage == CodegenLanguage.KOTLIN) {
298-
error("In Kotlin, unlike Java, static fields cannot be accessed by an object")
295+
error("In Kotlin, unlike Java, static fields cannot be accessed by an object: $this")
299296
}
300297

301298
// if field is declared in the current test class
@@ -313,7 +310,7 @@ internal class CgCallableAccessManagerImpl(val context: CgContext) : CgCallableA
313310
}
314311

315312
requireNotNull(accessor) {
316-
"Field access must have a non-null accessor, unless it is the field of the current test class or a static field"
313+
"Field access must have a non-null accessor, unless it is the field of the current test class or a static field: $this"
317314
}
318315

319316
if (this.declaringClass == accessor.type) {
@@ -359,6 +356,20 @@ internal class CgCallableAccessManagerImpl(val context: CgContext) : CgCallableA
359356
/**
360357
* Check if the field represented by @receiver is hidden when accessed from an instance of [subclass].
361358
*
359+
* Brief description: this method collects all types from hierarchy in between [subclass] (inclusive)
360+
* up to the class where the field is declared (exclusive) and checks if any of these classes declare
361+
* a field with the same name (thus hiding the given field). For examples and more details read documentation below.
362+
*
363+
* The **contract** of this method is as follows:
364+
* [subclass] must be a subclass of `this.declaringClass` (and they **must not** be the same class).
365+
* That is because if they are equal (we try to access field from instance of its own class),
366+
* then the field is not hidden. And if [subclass] is actually not a subclass, but a superclass of a class
367+
* where the field is declared, then this check makes no sense (superclass cannot hide field of a subclass).
368+
* Lastly, if these classes are not related in terms of inheritance, then there must be some error,
369+
* because such checks also make no sense.
370+
*
371+
* **Examples**.
372+
*
362373
* For example, given classes:
363374
* ```
364375
* class A { int x; }
@@ -406,6 +417,11 @@ internal class CgCallableAccessManagerImpl(val context: CgContext) : CgCallableA
406417
* If such field is found, then the field is hidden, otherwise - not.
407418
*/
408419
private infix fun FieldId.isFieldHiddenIn(subclass: Class<*>): Boolean {
420+
// see the documentation of this method for more details on this requirement
421+
require(subclass.id != this.declaringClass) {
422+
"A given subclass must not be equal to the declaring class of the field: $subclass"
423+
}
424+
409425
// supertypes (classes and interfaces) from subclass (inclusive) up to superclass (exclusive)
410426
val supertypes = sequence {
411427
var types = generateSequence(subclass) { it.superclass }

0 commit comments

Comments
 (0)