Skip to content

Commit

Permalink
code formatting and details on reflection methods
Browse files Browse the repository at this point in the history
  • Loading branch information
jpstotz committed Mar 23, 2024
1 parent 0f78679 commit 43b05d0
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -243,13 +243,16 @@ protected void updateClassDefaultEditor() {
IEditorDescriptor editor = mapping.getEditors()[j];
if (editor.getId().equals(JavaDecompilerConstants.EDITOR_ID)) {
try {
// org.eclipse.ui.internal.registry.FileEditorMapping.setDefaultEditor(IEditorDescriptor)
// Eclipse Photon: method exists
ReflectionUtils.invokeMethod(mapping, "setDefaultEditor", //$NON-NLS-1$
new Class[] { Class.forName("org.eclipse.ui.IEditorDescriptor") //$NON-NLS-1$
}, new Object[] { editor });
} catch (ClassNotFoundException e) {
}

try {
// Unknown - may be required on old Eclipse versions?
ReflectionUtils.invokeMethod(mapping, "setDefaultEditor", //$NON-NLS-1$
new Class[] { Class.forName("org.eclipse.ui.internal.registry.EditorDescriptor") //$NON-NLS-1$
}, new Object[] { editor });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,9 @@ public Composite createControl(Composite parent) {
ReflectionUtils.invokeMethod(this, "initializeSourceViewer", new Class[] { //$NON-NLS-1$
IEditorInput.class }, new Object[] { getEditorInput() });

if (fSourceViewerDecorationSupport != null)
if (fSourceViewerDecorationSupport != null) {
fSourceViewerDecorationSupport.install(getPreferenceStore());
}

StyledText styledText = fSourceViewer.getTextWidget();
styledText.addMouseListener(getCursorListener());
Expand Down Expand Up @@ -207,9 +208,9 @@ public Composite createControl(Composite parent) {

RulerColumnDescriptor lineNumberColumnDescriptor = RulerColumnRegistry.getDefault()
.getColumnDescriptor(LineNumberColumn.ID);
if (lineNumberColumnDescriptor != null)
if (lineNumberColumnDescriptor != null) {
columnSupport.setColumnVisible(lineNumberColumnDescriptor, isLineNumberRulerVisible());

}
IPropertyChangeListener fFontPropertyChangeListener = (IPropertyChangeListener) ReflectionUtils
.getFieldValue(this, "fFontPropertyChangeListener"); //$NON-NLS-1$
JFaceResources.getFontRegistry().addListener(fFontPropertyChangeListener);
Expand Down Expand Up @@ -239,17 +240,19 @@ class JdtSelectionProvider extends SelectionProvider {
@Override
public void addSelectionChangedListener(ISelectionChangedListener listener) {
super.addSelectionChangedListener(listener);
if (getSourceViewer() != null)
if (getSourceViewer() != null) {
fSelectionListeners.add(listener);
}
}

/*
* @see org.eclipse.jface.viewers.ISelectionProvider#getSelection()
*/
@Override
public ISelection getSelection() {
if (fInvalidSelection != null)
if (fInvalidSelection != null) {
return fInvalidSelection;
}
return super.getSelection();
}

Expand All @@ -260,8 +263,9 @@ public ISelection getSelection() {
@Override
public void removeSelectionChangedListener(ISelectionChangedListener listener) {
super.removeSelectionChangedListener(listener);
if (getSourceViewer() != null)
if (getSourceViewer() != null) {
fSelectionListeners.remove(listener);
}
}

/*
Expand Down Expand Up @@ -299,8 +303,10 @@ public void setSelection(ISelection selection) {
@Override
public void addPostSelectionChangedListener(ISelectionChangedListener listener) {
super.addPostSelectionChangedListener(listener);
if (getSourceViewer() != null && getSourceViewer().getSelectionProvider() instanceof IPostSelectionProvider)
if (getSourceViewer() != null
&& getSourceViewer().getSelectionProvider() instanceof IPostSelectionProvider) {
fPostSelectionListeners.add(listener);
}
}

/*
Expand All @@ -311,8 +317,9 @@ public void addPostSelectionChangedListener(ISelectionChangedListener listener)
@Override
public void removePostSelectionChangedListener(ISelectionChangedListener listener) {
super.removePostSelectionChangedListener(listener);
if (getSourceViewer() != null)
if (getSourceViewer() != null) {
fPostSelectionListeners.remove(listener);
}
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,14 @@ public Object getHoverInfo2(ITextViewer textViewer, IRegion hoverRegion) {
return null;
}

return ReflectionUtils.invokeMethod(this, "getHoverInfo", new Class[] { //$NON-NLS-1$
IJavaElement[].class, ITypeRoot.class, IRegion.class,
JavadocBrowserInformationControlInput.class },
new Object[] { elements, getEditorInputJavaElement(), hoverRegion, null });
// Method is directly accessible, no need to use reflection
return JavadocHover.getHoverInfo(elements, getEditorInputJavaElement(), hoverRegion, null);
/*
* return ReflectionUtils.invokeMethod(this, "getHoverInfo", new Class[] {
* //$NON-NLS-1$ IJavaElement[].class, ITypeRoot.class, IRegion.class,
* JavadocBrowserInformationControlInput.class }, new Object[] { elements,
* getEditorInputJavaElement(), hoverRegion, null });
*/
} else if (textViewer.getDocument() instanceof ByteCodeDocument) {
int offset = -1;
String text = textViewer.getDocument().get(hoverRegion.getOffset(), hoverRegion.getLength());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -440,11 +440,14 @@ private void doHandleCursorPositionChanged() {

protected void editorContextMenuAboutToShow(IMenuManager menu) {
super.editorContextMenuAboutToShow(menu);
// private method
// org.eclipse.ui.texteditor.AbstractDecoratedTextEditor.getShowInMenuLabel()
String text = (String) ReflectionUtils.invokeMethod(this, "getShowInMenuLabel"); //$NON-NLS-1$
for (int i = 0; i < menu.getItems().length; i++) {
if (menu.getItems()[i] instanceof MenuManager) {
if (((MenuManager) menu.getItems()[i]).getMenuText().equals(text)) {
menu.remove(menu.getItems()[i]);
IContributionItem item = menu.getItems()[i];
if (item instanceof MenuManager) {
if (((MenuManager) item).getMenuText().equals(text)) {
menu.remove(item);
}
}
}
Expand Down Expand Up @@ -714,6 +717,7 @@ private OutlineElement searchElement(OutlineElement element, ISourceReference re
return null;
}

@Override
protected IConfigurationElement getConfigurationElement() {
return (IConfigurationElement) ReflectionUtils.invokeMethod(editor, "getConfigurationElement"); //$NON-NLS-1$
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -636,8 +636,7 @@ private void updateMatchAnnonation() {
if (amgr == null) {
return;
}
ReflectionUtils.invokeMethod(amgr, "removeAllAnnotations", //$NON-NLS-1$
new Class[0], new Object[0]);
ReflectionUtils.invokeMethod(amgr, "removeAllAnnotations");
}

@Override
Expand Down Expand Up @@ -670,7 +669,8 @@ protected void createActions() {
public void run() {
if (JavaDecompilerPlugin.getDefault().getSourceMode() == JavaDecompilerConstants.SOURCE_MODE) {
((SourceViewer) JavaDecompilerClassFileEditor.this.getSourceViewer()).getTextWidget().copy();
} else if (JavaDecompilerPlugin.getDefault().getSourceMode() == JavaDecompilerConstants.DISASSEMBLER_MODE
} else if (JavaDecompilerPlugin.getDefault()
.getSourceMode() == JavaDecompilerConstants.DISASSEMBLER_MODE
&& fDisassemblerSourceViewer != null && fDisassemblerSourceViewer.getTextWidget() != null) {
JavaDecompilerClassFileEditor.this.fDisassemblerSourceViewer.getTextWidget().copy();
} else if (JavaDecompilerPlugin.getDefault().getSourceMode() == JavaDecompilerConstants.BYTE_CODE_MODE
Expand All @@ -689,7 +689,8 @@ public void run() {
public void run() {
if (JavaDecompilerPlugin.getDefault().getSourceMode() == JavaDecompilerConstants.SOURCE_MODE) {
((SourceViewer) JavaDecompilerClassFileEditor.this.getSourceViewer()).getTextWidget().selectAll();
} else if (JavaDecompilerPlugin.getDefault().getSourceMode() == JavaDecompilerConstants.DISASSEMBLER_MODE
} else if (JavaDecompilerPlugin.getDefault()
.getSourceMode() == JavaDecompilerConstants.DISASSEMBLER_MODE
&& fDisassemblerSourceViewer != null && fDisassemblerSourceViewer.getTextWidget() != null) {
JavaDecompilerClassFileEditor.this.fDisassemblerSourceViewer.getTextWidget().selectAll();
} else if (JavaDecompilerPlugin.getDefault().getSourceMode() == JavaDecompilerConstants.BYTE_CODE_MODE
Expand All @@ -711,7 +712,8 @@ public void run() {
(IFindReplaceTarget) JavaDecompilerClassFileEditor.this
.getAdapter(IFindReplaceTarget.class));

} else if (JavaDecompilerPlugin.getDefault().getSourceMode() == JavaDecompilerConstants.DISASSEMBLER_MODE
} else if (JavaDecompilerPlugin.getDefault()
.getSourceMode() == JavaDecompilerConstants.DISASSEMBLER_MODE
&& fDisassemblerSourceViewer != null) {
ReflectionUtils.setFieldValue(this, "fTarget", //$NON-NLS-1$
(IFindReplaceTarget) fDisassemblerSourceViewer.getAdapter(IFindReplaceTarget.class));
Expand All @@ -738,7 +740,8 @@ public void run() {
(IFindReplaceTarget) JavaDecompilerClassFileEditor.this
.getAdapter(IFindReplaceTarget.class));

} else if (JavaDecompilerPlugin.getDefault().getSourceMode() == JavaDecompilerConstants.DISASSEMBLER_MODE
} else if (JavaDecompilerPlugin.getDefault()
.getSourceMode() == JavaDecompilerConstants.DISASSEMBLER_MODE
&& fDisassemblerSourceViewer != null) {
ReflectionUtils.setFieldValue(this, "fTarget", //$NON-NLS-1$
(IFindReplaceTarget) fDisassemblerSourceViewer.getAdapter(IFindReplaceTarget.class));
Expand All @@ -765,7 +768,8 @@ public void run() {
(IFindReplaceTarget) JavaDecompilerClassFileEditor.this
.getAdapter(IFindReplaceTarget.class));

} else if (JavaDecompilerPlugin.getDefault().getSourceMode() == JavaDecompilerConstants.DISASSEMBLER_MODE
} else if (JavaDecompilerPlugin.getDefault()
.getSourceMode() == JavaDecompilerConstants.DISASSEMBLER_MODE
&& fDisassemblerSourceViewer != null) {
ReflectionUtils.setFieldValue(this, "fTarget", //$NON-NLS-1$
(IFindReplaceTarget) fDisassemblerSourceViewer.getAdapter(IFindReplaceTarget.class));
Expand Down Expand Up @@ -798,7 +802,8 @@ public void run() {
.getSourceMode() == JavaDecompilerConstants.DISASSEMBLER_MODE
&& fDisassemblerSourceViewer != null) {
ReflectionUtils.setFieldValue(this, "fTarget", fDisassemblerSourceViewer.getAdapter(clazz)); //$NON-NLS-1$
} else if (JavaDecompilerPlugin.getDefault().getSourceMode() == JavaDecompilerConstants.BYTE_CODE_MODE
} else if (JavaDecompilerPlugin.getDefault()
.getSourceMode() == JavaDecompilerConstants.BYTE_CODE_MODE
&& fByteCodeSourceViewer != null) {
ReflectionUtils.setFieldValue(this, "fTarget", fByteCodeSourceViewer.getAdapter(clazz)); //$NON-NLS-1$
}
Expand Down Expand Up @@ -829,7 +834,8 @@ public void run() {
.getSourceMode() == JavaDecompilerConstants.DISASSEMBLER_MODE
&& fDisassemblerSourceViewer != null) {
ReflectionUtils.setFieldValue(this, "fTarget", fDisassemblerSourceViewer.getAdapter(clazz)); //$NON-NLS-1$
} else if (JavaDecompilerPlugin.getDefault().getSourceMode() == JavaDecompilerConstants.BYTE_CODE_MODE
} else if (JavaDecompilerPlugin.getDefault()
.getSourceMode() == JavaDecompilerConstants.BYTE_CODE_MODE
&& fByteCodeSourceViewer != null) {
ReflectionUtils.setFieldValue(this, "fTarget", fByteCodeSourceViewer.getAdapter(clazz)); //$NON-NLS-1$
}
Expand All @@ -851,7 +857,8 @@ public void run() {
protected ITextEditor getTextEditor() {
if (JavaDecompilerPlugin.getDefault().getSourceMode() == JavaDecompilerConstants.DISASSEMBLER_MODE) {
return fDisassemblerSourceViewer;
} else if (JavaDecompilerPlugin.getDefault().getSourceMode() == JavaDecompilerConstants.BYTE_CODE_MODE) {
} else if (JavaDecompilerPlugin.getDefault()
.getSourceMode() == JavaDecompilerConstants.BYTE_CODE_MODE) {
return fByteCodeSourceViewer;
}
return JavaDecompilerClassFileEditor.this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,9 @@ public static void updateSourceRanges(IClassFile cf, String contents) throws Jav
Object info = EclipseCompatibilityHelper.javaElementGetElementInfo(classFile);
IBinaryType typeInfo = info instanceof IBinaryType ? (IBinaryType) info : null;
SourceMapper mapper = classFile.getSourceMapper();
IType type = (IType) ReflectionUtils.invokeMethod(classFile, "getOuterMostEnclosingType", //$NON-NLS-1$
new Class[0], new Object[0]);
// private org.eclipse.jdt.internal.core.ClassFile.getOuterMostEnclosingType()
IType type = (IType) ReflectionUtils.invokeMethod(classFile, "getOuterMostEnclosingType");

HashMap sourceRange = (HashMap) ReflectionUtils.getFieldValue(mapper, "sourceRanges"); //$NON-NLS-1$
sourceRange.remove(type);
SourceMapperUtil.mapSource(mapper, type, contents.toCharArray(), typeInfo);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public static void mapSource(SourceMapper sourceMapper, IType type, char[] sourc
}

private static Method getMapSourceMethod(SourceMapper sourceMapper) {
// Available in Eclipse Photon / jdt 3.14.0
return ReflectionUtils.getDeclaredMethod(sourceMapper, MAP_SOURCE_METHOD_NAME,
new Class[] { NamedMember.class, char[].class, IBinaryType.class, IJavaElement.class });
}
Expand Down

0 comments on commit 43b05d0

Please sign in to comment.