Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel-Darbord committed May 13, 2024
1 parent a2ae73e commit b7aa627
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/Famix-UnitTest-Exporter/FamixUTJUnitExporter.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit b7aa627

Please sign in to comment.