# HG changeset patch # User Dirk Olmes # Date 1602756306 -7200 # Node ID 935df68696c07ad6c36ad98b8b314e37fd2f25c4 # Parent 778c251baa66b8cd48af1499847c621eb3e85b2d Reflection Code in eigene Klasse verschoben. diff -r 778c251baa66 -r 935df68696c0 .classpath --- a/.classpath Thu Oct 15 09:43:34 2020 +0200 +++ b/.classpath Thu Oct 15 12:05:06 2020 +0200 @@ -1,7 +1,7 @@ - - - - - - - + + + + + + + diff -r 778c251baa66 -r 935df68696c0 .settings/org.eclipse.jdt.core.prefs --- a/.settings/org.eclipse.jdt.core.prefs Thu Oct 15 09:43:34 2020 +0200 +++ b/.settings/org.eclipse.jdt.core.prefs Thu Oct 15 12:05:06 2020 +0200 @@ -6,8 +6,8 @@ org.eclipse.jdt.core.compiler.annotation.nullable=org.eclipse.jdt.annotation.Nullable org.eclipse.jdt.core.compiler.annotation.nullanalysis=disabled org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled -org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 -org.eclipse.jdt.core.compiler.compliance=1.7 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=11 +org.eclipse.jdt.core.compiler.compliance=11 org.eclipse.jdt.core.compiler.problem.annotationSuperInterface=warning org.eclipse.jdt.core.compiler.problem.assertIdentifier=error org.eclipse.jdt.core.compiler.problem.autoboxing=ignore @@ -18,6 +18,7 @@ org.eclipse.jdt.core.compiler.problem.deprecationWhenOverridingDeprecatedMethod=disabled org.eclipse.jdt.core.compiler.problem.discouragedReference=ignore org.eclipse.jdt.core.compiler.problem.emptyStatement=ignore +org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled org.eclipse.jdt.core.compiler.problem.enumIdentifier=error org.eclipse.jdt.core.compiler.problem.explicitlyClosedAutoCloseable=ignore org.eclipse.jdt.core.compiler.problem.fallthroughCase=ignore @@ -61,6 +62,7 @@ org.eclipse.jdt.core.compiler.problem.redundantSuperinterface=ignore org.eclipse.jdt.core.compiler.problem.reportMethodCanBePotentiallyStatic=ignore org.eclipse.jdt.core.compiler.problem.reportMethodCanBeStatic=ignore +org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning org.eclipse.jdt.core.compiler.problem.specialParameterHidingField=disabled org.eclipse.jdt.core.compiler.problem.staticAccessReceiver=warning org.eclipse.jdt.core.compiler.problem.suppressOptionalErrors=disabled @@ -92,4 +94,5 @@ org.eclipse.jdt.core.compiler.problem.unusedTypeParameter=ignore org.eclipse.jdt.core.compiler.problem.unusedWarningToken=warning org.eclipse.jdt.core.compiler.problem.varargsArgumentNeedCast=warning -org.eclipse.jdt.core.compiler.source=1.7 +org.eclipse.jdt.core.compiler.release=enabled +org.eclipse.jdt.core.compiler.source=11 diff -r 778c251baa66 -r 935df68696c0 META-INF/MANIFEST.MF --- a/META-INF/MANIFEST.MF Thu Oct 15 09:43:34 2020 +0200 +++ b/META-INF/MANIFEST.MF Thu Oct 15 12:05:06 2020 +0200 @@ -3,7 +3,7 @@ Bundle-ManifestVersion: 2 Bundle-Name: Java Editor with extra icon Bundle-SymbolicName: de.codedo.java.editor;singleton:=true -Bundle-Version: 1.0.1.qualifier +Bundle-Version: 1.0.2.qualifier Bundle-Activator: de.codedo.java.editor.Activator Bundle-Vendor: Dirk Olmes Require-Bundle: org.eclipse.ui, diff -r 778c251baa66 -r 935df68696c0 src/de/codedo/java/editor/CodedoJavaEditor.java --- a/src/de/codedo/java/editor/CodedoJavaEditor.java Thu Oct 15 09:43:34 2020 +0200 +++ b/src/de/codedo/java/editor/CodedoJavaEditor.java Thu Oct 15 12:05:06 2020 +0200 @@ -1,6 +1,7 @@ package de.codedo.java.editor; -import java.lang.reflect.Field; +import static de.codedo.java.editor.ReflectionUtils.getFieldValue; +import static de.codedo.java.editor.ReflectionUtils.setFieldValue; import org.eclipse.jdt.core.IJavaElement; import org.eclipse.jdt.internal.ui.javaeditor.CompilationUnitEditor; @@ -21,8 +22,11 @@ { try { - JavaUILabelProvider labelProvider = getJavaUILabelProvider(); - setupJavaElementImageProvider(labelProvider); + JavaEditorErrorTickUpdater tickUpdater = getFieldValue("fJavaEditorErrorTickUpdater", this); + JavaUILabelProvider labelProvider = getFieldValue("fLabelProvider", tickUpdater); + + CodedoJavaElementImageProvider imageProvider = Activator.getDefault().getJavaElementImageProvider(); + setFieldValue(labelProvider, "fImageLabelProvider", imageProvider); } catch (ReflectiveOperationException e) { @@ -30,31 +34,6 @@ } } - private JavaUILabelProvider getJavaUILabelProvider() throws ReflectiveOperationException - { - JavaEditorErrorTickUpdater tickUpdater = getJavaEditorErrorTickUpdater(); - - Field field = tickUpdater.getClass().getDeclaredField("fLabelProvider"); - field.setAccessible(true); - return (JavaUILabelProvider)field.get(tickUpdater); - } - - private JavaEditorErrorTickUpdater getJavaEditorErrorTickUpdater() throws ReflectiveOperationException - { - Field field = getClass().getSuperclass().getDeclaredField("fJavaEditorErrorTickUpdater"); - field.setAccessible(true); - return (JavaEditorErrorTickUpdater)field.get(this); - } - - private void setupJavaElementImageProvider(JavaUILabelProvider labelProvider) throws ReflectiveOperationException - { - CodedoJavaElementImageProvider imageProvider = Activator.getDefault().getJavaElementImageProvider(); - - Field field = labelProvider.getClass().getDeclaredField("fImageLabelProvider"); - field.setAccessible(true); - field.set(labelProvider, imageProvider); - } - public void toggleTitleImage() { IJavaElement javaElement = getJavaElement(); diff -r 778c251baa66 -r 935df68696c0 src/de/codedo/java/editor/CodedoJavaElementImageProvider.java --- a/src/de/codedo/java/editor/CodedoJavaElementImageProvider.java Thu Oct 15 09:43:34 2020 +0200 +++ b/src/de/codedo/java/editor/CodedoJavaElementImageProvider.java Thu Oct 15 12:05:06 2020 +0200 @@ -52,7 +52,7 @@ } } - protected Image getCustomTitleImage() + private Image getCustomTitleImage() { if (_customTitleImage == null) { diff -r 778c251baa66 -r 935df68696c0 src/de/codedo/java/editor/ReflectionUtils.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/de/codedo/java/editor/ReflectionUtils.java Thu Oct 15 12:05:06 2020 +0200 @@ -0,0 +1,45 @@ +package de.codedo.java.editor; + +import java.lang.reflect.Field; + +public class ReflectionUtils +{ + public static Field getField(String fieldName, Object object) throws NoSuchFieldException + { + Field field = findField(fieldName, object.getClass()); + field.setAccessible(true); + return field; + } + + @SuppressWarnings("unchecked") + public static T getFieldValue(String fieldName, Object object) throws NoSuchFieldException, IllegalAccessException + { + Field field = getField(fieldName, object); + return (T)field.get(object); + } + + public static void setFieldValue(Object object, String fieldName, Object value) throws NoSuchFieldException, IllegalAccessException + { + Field field = getField(fieldName, object); + field.set(object, value); + } + + private static Field findField(String fieldName, Class clazz) throws NoSuchFieldException + { + try + { + return clazz.getDeclaredField(fieldName); + } + catch (NoSuchFieldException ex) + { + // OK, this class does not declare the field. Continue searching the superclass below. + } + + Class superClass = clazz.getSuperclass(); + if (superClass == null) + { + throw new IllegalStateException("Field " + fieldName + " could not be found in object's type hierarchy."); + } + return findField(fieldName, superClass); + } +}