changeset 9:935df68696c0 default tip

Reflection Code in eigene Klasse verschoben.
author Dirk Olmes <dirk.olmes@codedo.de>
date Thu, 15 Oct 2020 12:05:06 +0200
parents 778c251baa66
children
files .classpath .settings/org.eclipse.jdt.core.prefs META-INF/MANIFEST.MF src/de/codedo/java/editor/CodedoJavaEditor.java src/de/codedo/java/editor/CodedoJavaElementImageProvider.java src/de/codedo/java/editor/ReflectionUtils.java
diffstat 6 files changed, 67 insertions(+), 40 deletions(-) [+]
line wrap: on
line diff
--- a/.classpath	Thu Oct 15 09:43:34 2020 +0200
+++ b/.classpath	Thu Oct 15 12:05:06 2020 +0200
@@ -1,7 +1,7 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<classpath>
-	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
-	<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
-	<classpathentry kind="src" path="src"/>
-	<classpathentry kind="output" path="bin"/>
-</classpath>
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11"/>
+	<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
+	<classpathentry kind="src" path="src"/>
+	<classpathentry kind="output" path="bin"/>
+</classpath>
--- 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
--- 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,
--- 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();
--- 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)
 		{
--- /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> 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);
+	}
+}