Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

don't retrieve the ImportManager by reflection #2889

Merged
merged 1 commit into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
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) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Should the XbaseCompiler work with TreeAppendable instead of ITreeAppendable on its APIs?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@szarnekow from what I see and understand, the XbaseCompiler does not need anything more than what the ITreeAppendable interface provides and doesn't seem to make further assumptions.
On the contrary, JvmModelGenerator completely relies on TreeAppendable.

Copy link
Contributor

Choose a reason for hiding this comment

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

Meh sadness. We cannot easily change signatures here. Casting seems the best we can do.

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
Loading