Skip to content

Commit

Permalink
don't retrieve the ImportManager by reflection
Browse files Browse the repository at this point in the history
Since we already assume that the import manager is part of the
appendable, which is a TreeAppendable, I think it's better to do the
case and expose getImportManager in TreeAppendable than to call that
method with two reflective calls.
This would also allow clients to provide a custom TreeAppendable; with
the reflective call this latter use-case would not be supported
  • Loading branch information
LorenzoBettini committed Jan 2, 2024
1 parent da2e5b8 commit 30c2d66
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -804,7 +804,7 @@ class JvmModelGenerator implements IGenerator {
generateDocumentation(adapter.documentation, emptyList, appendable, config)
}
}
}
}

def addJavaDocImports(EObject it, ITreeAppendable appendable,List<INode> documentationNodes) {
for(node : documentationNodes){
Expand Down Expand Up @@ -839,12 +839,7 @@ class JvmModelGenerator implements IGenerator {
}

def getImportManager(ITreeAppendable appendable) {
val stateField = appendable.getClass.getDeclaredField("state")
stateField.setAccessible(true)
val stateValue = stateField.get(appendable)
val importManagerField = stateValue.getClass.getDeclaredField("importManager")
importManagerField.setAccessible(true)
importManagerField.get(stateValue) as ImportManager
(appendable as TreeAppendable).getImportManager()
}

def protected generateDocumentation(String text, List<INode> documentationNodes, ITreeAppendable appendable, GeneratorConfig config) {
Expand Down Expand Up @@ -886,7 +881,7 @@ class JvmModelGenerator implements IGenerator {
toJava(appendable, config)
])
}

def void toJava(JvmAnnotationValue it, ITreeAppendable appendable, GeneratorConfig config) {
if (operation !== null) {
if (operation.simpleName === null) {
Expand All @@ -899,11 +894,11 @@ class JvmModelGenerator implements IGenerator {
}
toJavaLiteral(appendable, config)
}

def dispatch void toJavaLiteral(JvmAnnotationAnnotationValue value, ITreeAppendable appendable, GeneratorConfig config) {
appendable.forEachWithShortcut(value.values, [generateAnnotation(appendable, config)])
}

def dispatch void toJavaLiteral(JvmShortAnnotationValue it, ITreeAppendable appendable, GeneratorConfig config) {
appendable.forEachWithShortcut(values, [appendable.append(toString)])
}
Expand Down Expand Up @@ -955,26 +950,26 @@ class JvmModelGenerator implements IGenerator {
appendable.append('"' + doConvertToJavaString(toString) + '"')
])
}

def dispatch void toJavaLiteral(JvmTypeAnnotationValue it, ITreeAppendable appendable, GeneratorConfig config) {
appendable.forEachWithShortcut(values, [
appendable.append(type).append(".class")
])
}
}

def dispatch void toJavaLiteral(JvmEnumAnnotationValue it, ITreeAppendable appendable, GeneratorConfig config) {
appendable.forEachWithShortcut(values, [
appendable.append(declaringType)
appendable.append(".")
appendable.append(simpleName.makeJavaIdentifier)
])
}
}

def dispatch void toJavaLiteral(JvmBooleanAnnotationValue it, ITreeAppendable appendable, GeneratorConfig config) {
appendable.forEachWithShortcut(values, [
appendable.append(toString)
])
}
}

def dispatch void toJavaLiteral(JvmCustomAnnotationValue it, ITreeAppendable appendable, GeneratorConfig config) {
if(values.isEmpty)
Expand All @@ -984,7 +979,7 @@ class JvmModelGenerator implements IGenerator {
compiler.toJavaExpression(it, appendable)
])
}

def TreeAppendable createAppendable(EObject context, ImportManager importManager, GeneratorConfig config) {
val cachingConverter = new ITraceURIConverter() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,10 @@ public SharedAppendableState getState() {
return state;
}

ImportManager getImportManager() {
/**
* @since 2.34
*/
public ImportManager getImportManager() {
return state.getImportManager();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import com.google.common.collect.Iterables;
import com.google.common.collect.Maps;
import com.google.inject.Inject;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashMap;
Expand Down Expand Up @@ -1226,21 +1225,7 @@ public void addJavaDocImports(final EObject it, final ITreeAppendable appendable
}

public ImportManager getImportManager(final ITreeAppendable appendable) {
try {
ImportManager _xblockexpression = null;
{
final Field stateField = appendable.getClass().getDeclaredField("state");
stateField.setAccessible(true);
final Object stateValue = stateField.get(appendable);
final Field importManagerField = stateValue.getClass().getDeclaredField("importManager");
importManagerField.setAccessible(true);
Object _get = importManagerField.get(stateValue);
_xblockexpression = ((ImportManager) _get);
}
return _xblockexpression;
} catch (Throwable _e) {
throw Exceptions.sneakyThrow(_e);
}
return ((TreeAppendable) appendable).getImportManager();
}

protected ITreeAppendable generateDocumentation(final String text, final List<INode> documentationNodes, final ITreeAppendable appendable, final GeneratorConfig config) {
Expand Down

0 comments on commit 30c2d66

Please sign in to comment.