From b7aa627998c9046c7a9766683507bd0d225ebded Mon Sep 17 00:00:00 2001 From: Gabriel Darbord Date: Mon, 13 May 2024 19:49:35 +0200 Subject: [PATCH] Fix --- .../FamixUTJUnitExporter.class.st | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/src/Famix-UnitTest-Exporter/FamixUTJUnitExporter.class.st b/src/Famix-UnitTest-Exporter/FamixUTJUnitExporter.class.st index 5b652a0..7dbea75 100644 --- a/src/Famix-UnitTest-Exporter/FamixUTJUnitExporter.class.st +++ b/src/Famix-UnitTest-Exporter/FamixUTJUnitExporter.class.st @@ -57,6 +57,50 @@ FamixUTJUnitExporter >> currentCompilationUnit [ ^ currentCompilationUnit ] +{ #category : #exporting } +FamixUTJUnitExporter >> enableReflectionFor: aFamixJavaAttribute [ + "Give access to the given attribute using reflection. + An attribute of the test class will hold the Field object." + + | constructor fieldVarName | + fieldVarName := aFamixJavaAttribute name , 'Field'. + currentClass addDeclaration: (model newVarDeclStatement + type: (model newClassTypeExpression typeName: + (model newTypeName name: 'Field')); + addModifier: (model newModifier token: 'private'); + addDeclarator: (model newVariableDeclarator variable: + (model newVariableExpression name: fieldVarName)); + yourself). + + "Ensure a constructor exists for the test class" + constructor := self ensureClassConstructorForReflection. + + "Get the Field using reflection and make it accessible" + constructor statementBlock + addStatement: (model newExpressionStatement expression: + (model newAssignmentExpression + variable: (model newVariableExpression name: fieldVarName); + operator: '='; + expression: (model newMethodInvocation + receiver: (model newClassProperty + type: + (model newIdentifier name: + aFamixJavaAttribute parentType name); + fieldName: 'class'); + name: 'getDeclaredField'; + addArgument: + (model newStringLiteral primitiveValue: + aFamixJavaAttribute name); + yourself))); + addStatement: + (model newExpressionStatement expression: + (model newMethodInvocation + receiver: (model newVariableExpression name: fieldVarName); + name: 'setAccessible'; + addArgument: (model newBooleanLiteral primitiveValue: 'true'); + yourself)) +] + { #category : #exporting } FamixUTJUnitExporter >> ensureClassConstructorForReflection [ "To enable attribute reflection in Java, we need to get the Field and make it accessible"