Skip to content
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
d80463c
fixed prematurely read issue
roterEmil Feb 1, 2024
52754a0
Merge branch 'develop' into fix/assignability-prematurelyRead
Jun 9, 2024
84281b7
Merge branch 'develop' into fix/assignability-prematurelyRead
roterEmil Jul 30, 2024
624d671
deleted sandbox files
roterEmil Jul 30, 2024
8244423
Merge branch 'fix/assignability-prematurelyRead' of https://github.co…
roterEmil Jul 30, 2024
fef5c6d
removed debug code
roterEmil Aug 1, 2024
0d76243
Update PrematurelyReadOfFinalField.java due to Dominiks review
roterEmil Sep 20, 2024
253c4fc
Update PrematurelyReadOfFinalField.java inserted line break
roterEmil Sep 20, 2024
2f1d9de
refactoring for more readability
roterEmil Oct 2, 2024
62aed1f
Merge branch 'fix/assignability-prematurelyRead' of https://github.co…
roterEmil Oct 2, 2024
9cce192
fixed multiple assignments of final fields in different branches and …
roterEmil Oct 18, 2024
4675ef7
formatting
roterEmil Oct 18, 2024
f3ba6d7
fixed grammar
roterEmil Oct 22, 2024
5fa99d8
removed commented out code
roterEmil Oct 22, 2024
7a0e320
Merge branch 'develop' into fix/assignability-prematurelyRead
roterEmil Oct 22, 2024
c261849
Merge branch 'develop' into fix/assignability-prematurelyRead
roterEmil Nov 19, 2024
e5df666
Merge branch 'develop' into fix/assignability-prematurelyRead
Aug 5, 2025
1fb4123
Fix typos and improve style
maximilianruesch Sep 16, 2025
11da1b4
Revert minimizing tests
maximilianruesch Sep 16, 2025
8cbab1f
Improve clarity on assignability analysis
maximilianruesch Sep 17, 2025
ebcb918
Merge branch 'develop' into fix/assignability-prematurelyRead
maximilianruesch Sep 17, 2025
4ed186b
Improve test case style
maximilianruesch Sep 17, 2025
adde086
Improve clarity
maximilianruesch Sep 17, 2025
13bb641
Fix formatting
maximilianruesch Sep 17, 2025
f62d034
Add missing semicolon
maximilianruesch Sep 17, 2025
13c4f87
Encapsulate assignability analysis
maximilianruesch Sep 24, 2025
df793f7
Simplify mental overhead in assignability
maximilianruesch Sep 24, 2025
9ce7d9e
Move state up
maximilianruesch Oct 2, 2025
ae0fc47
Large refactor of assignability analysis
maximilianruesch Oct 2, 2025
9126fed
Shuffle around logic
maximilianruesch Oct 3, 2025
79a8ad3
Fix typos
maximilianruesch Oct 3, 2025
518a1ce
Fix logic in meeting field assignability
maximilianruesch Oct 3, 2025
bc49ab5
More shuffling
maximilianruesch Oct 3, 2025
c5c2f5a
Fix receiver var of field domination
maximilianruesch Oct 3, 2025
891e92a
Handle domination of multiple branches
maximilianruesch Oct 3, 2025
d277036
Cleanup
maximilianruesch Oct 3, 2025
3873fe0
Analyze suspicious usages again
maximilianruesch Oct 3, 2025
05e77cf
Refactor field access structure
maximilianruesch Oct 3, 2025
22a1e18
Refine suspicious uses
maximilianruesch Oct 3, 2025
a985a4b
Enable proper updateability of field access
maximilianruesch Oct 4, 2025
c644d97
Format code
maximilianruesch Oct 4, 2025
ca9ddb2
Merge branch 'refs/heads/develop' into fix/assignability-prematurelyRead
maximilianruesch Oct 4, 2025
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/* BSD 2-Clause License - see OPAL/LICENSE for details. */
package org.opalj.fpcf.fixtures.immutability.openworld.assignability.advanced_counter_examples;

import org.opalj.fpcf.properties.immutability.field_assignability.AssignableField;

/**
* The default value of the field x is assigned to another field n during construction and as
* a result seen with two different values.
*/
public class PrematurelyReadOfFinalField {

@AssignableField("Field n is assigned with different values.")
static int n = 5;

public static void main(String[] args) {
System.out.println("Value A.X before constructor:" + PrematurelyReadOfFinalField.n);
C c = new C();
System.out.println("Value A.X after constructor:" + PrematurelyReadOfFinalField.n);
System.out.println("Value C.x after constructor:" + c.x );
}

}
class B {

B() {
PrematurelyReadOfFinalField.n = ((C) this).x;
}

void b(C c) {
PrematurelyReadOfFinalField.n = c.x;
}

}

class C extends B{

@AssignableField("Is seen with two different values during construction.")
public final int x;

C() {
super();
//this.b(this);
x = 3;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/* BSD 2-Clause License - see OPAL/LICENSE for details. */
package org.opalj.fpcf.fixtures.immutability.openworld.assignability.advanced_counter_examples;

import org.opalj.fpcf.properties.immutability.field_assignability.AssignableField;

/**
* This test case simulates the fact that the this object escapes in the constructor before (final) fields
* are assigned.
*/
public class ThisEscapesDuringConstruction {

@AssignableField("The this object escapes in the constructor before the field is assigned.")
final int n;

public ThisEscapesDuringConstruction(){
C2.m(this);
n = 7;
}
}

class C2{
public static void m(ThisEscapesDuringConstruction c){}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* BSD 2-Clause License - see OPAL/LICENSE for details. */
package org.opalj.fpcf.fixtures.immutability.openworld.assignability.advanced_counter_examples;

import org.opalj.fpcf.properties.immutability.field_assignability.AssignableField;

/**
* The value of the field x is read with its default value (0)
* in the constructor before assignment and assigned to a public field.
* Thus, the value can be accessed from everywhere.
*/
public class ValueReadBeforeAssignment {
@AssignableField("Field value is read before assignment.")
private int x;
@AssignableField("Field y is public and not final.")
public int y;

public ValueReadBeforeAssignment() {
y = x;
x = 42;
}

public ValueReadBeforeAssignment foo() {
return new ValueReadBeforeAssignment();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -128,29 +128,34 @@ trait AbstractFieldAssignabilityAnalysis extends FPCFAnalysis {

implicit val state: AnalysisState = createState(field)

if (field.isFinal)
return Result(field, NonAssignable);
else
state.fieldAssignability = EffectivelyNonAssignable
val fieldName = field.name
println(fieldName)

state.fieldAssignability =
if (field.isFinal)
NonAssignable
else
EffectivelyNonAssignable

if (field.isPublic)
if (field.isPublic && !field.isFinal)
return Result(field, Assignable);

val thisType = field.classFile.thisType

if (field.isPublic) {
if (field.isPublic && !field.isFinal) {
if (typeExtensibility(ObjectType.Object).isYesOrUnknown) {
return Result(field, Assignable);
}
} else if (field.isProtected) {
} else if (field.isProtected && !field.isFinal) {
if (typeExtensibility(thisType).isYesOrUnknown) {
return Result(field, Assignable);
}
if (!closedPackages(thisType.packageName)) {
return Result(field, Assignable);
}
}
if (field.isPackagePrivate) {

if (field.isPackagePrivate && !field.isFinal) {
if (!closedPackages(thisType.packageName)) {
return Result(field, Assignable);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,18 +92,17 @@ class L2FieldAssignabilityAnalysis private[analyses] (val project: SomeProject)
pc: PC,
receiver: AccessReceiver
)(implicit state: AnalysisState): Boolean = {

val field = state.field
val method = definedMethod.definedMethod
val stmts = taCode.stmts
val receiverVar = receiver.map(uVarForDefSites(_, taCode.pcToIndex))

val index = taCode.pcToIndex(pc)
if (method.isInitializer) {
if (field.isStatic) {
method.isConstructor
} else {
receiverVar.isDefined && receiverVar.get.definedBy != SelfReferenceParameter
}
if (method.isInitializer && method.classFile == field.classFile) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this part of the code needs documentation to understand what all of the different conditions do

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still not documented

field.isStatic && method.isConstructor ||
receiverVar.isDefined && receiverVar.get.definedBy != SelfReferenceParameter ||
checkWriteDominance(definedMethod, taCode, receiverVar, index)
} else {
if (field.isStatic || receiverVar.isDefined && receiverVar.get.definedBy == SelfReferenceParameter) {
// We consider lazy initialization if there is only single write
Expand Down Expand Up @@ -152,24 +151,45 @@ class L2FieldAssignabilityAnalysis private[analyses] (val project: SomeProject)
if (writesInMethod.distinctBy(_._2).size > 1)
return true; // Field is written in multiple locations, thus must be assignable

// If we have no information about the receiver, we soundly return
if (receiverVar.isEmpty)
// If we have no information about the receiver, we soundly return true
// However, a static field has no receiver
if (receiverVar.isEmpty && !state.field.isStatic)
return true;

val assignedValueObject = receiverVar.get
if (assignedValueObject.definedBy.exists(_ < 0))
val assignedValueObject =
if (index > 0 && stmts(index).isPutStatic) {
stmts(index).asPutStatic.value.asVar
} else
receiverVar.get

// When there are more than 1 definitionsite, we soundly return true
if (assignedValueObject.definedBy.size != 1)
return true;

val definitionSite = assignedValueObject.definedBy.head

if (definitionSite < -1 ||
(definitionSite == -1 && !definedMethod.definedMethod.isConstructor)
)
return true;

val assignedValueObjectVar = stmts(assignedValueObject.definedBy.head).asAssignment.targetVar.asVar
val uses = if (definitionSite == -1)
taCode.params.thisParameter.useSites
else {
val assignedValueObjectVar = stmts(definitionSite).asAssignment.targetVar.asVar
if (assignedValueObjectVar != null)
assignedValueObjectVar.usedBy
else IntTrieSet.empty
}

val fieldWriteInMethodIndex = taCode.pcToIndex(writesInMethod.head._2)
if (assignedValueObjectVar != null && !assignedValueObjectVar.usedBy.forall { index =>
if (!uses.forall { index =>
val stmt = stmts(index)

fieldWriteInMethodIndex == index || // The value is itself written to another object
// IMPROVE: Can we use field access information to care about reflective accesses here?
stmt.isPutField && stmt.asPutField.name != state.field.name ||
stmt.isAssignment && stmt.asAssignment.targetVar == assignedValueObjectVar ||
// stmt.isAssignment && stmt.asAssignment.targetVar == assignedValueObjectVar ||
stmt.isMethodCall && stmt.asMethodCall.name == "<init>" ||
// CHECK do we really need the taCode here?
dominates(fieldWriteInMethodIndex, index, taCode)
Expand Down Expand Up @@ -256,15 +276,17 @@ class L2FieldAssignabilityAnalysis private[analyses] (val project: SomeProject)
fieldReadAccessInformation.numIndirectAccesses - seenIndirectAccesses
).exists { readAccess =>
val method = contextProvider.contextFromId(readAccess._1).method
(writeAccess._1 eq method) && {
val taCode = state.tacDependees(method.asDefinedMethod).ub.tac.get

if (readAccess._3.isDefined && readAccess._3.get._2.forall(isFormalParameter)) {
false
} else {
!dominates(writeAccess._4, taCode.pcToIndex(readAccess._2), taCode)
method.definedMethod.classFile != state.field.classFile ||
(writeAccess._1 eq method) && {
val taCode = state.tacDependees(method.asDefinedMethod).ub.tac.get

if (readAccess._3.isDefined && readAccess._3.get._2.forall(isFormalParameter)) {
false
} else {
!dominates(writeAccess._4, taCode.pcToIndex(readAccess._2), taCode)
}
}
}
}
}
}
Expand Down