From 0bea9726df0c7a51257165226de61c26c7798c58 Mon Sep 17 00:00:00 2001 From: Markus Keller Date: Wed, 20 Jan 2016 17:31:26 +0100 Subject: Fixes for bug 483340: ListenerList should be parameterized --- .../org/eclipse/jface/text/AbstractDocument.java | 74 ++++++++-------------- .../org/eclipse/text/undo/DocumentUndoManager.java | 11 ++-- 2 files changed, 33 insertions(+), 52 deletions(-) (limited to 'org.eclipse.text') diff --git a/org.eclipse.text/src/org/eclipse/jface/text/AbstractDocument.java b/org.eclipse.text/src/org/eclipse/jface/text/AbstractDocument.java index 2b1bd31b999..45e6e9cccb5 100644 --- a/org.eclipse.text/src/org/eclipse/jface/text/AbstractDocument.java +++ b/org.eclipse.text/src/org/eclipse/jface/text/AbstractDocument.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2015 IBM Corporation and others. + * Copyright (c) 2000, 2016 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -12,8 +12,8 @@ package org.eclipse.jface.text; -import java.util.AbstractList; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; import java.util.Iterator; import java.util.List; @@ -92,11 +92,11 @@ public abstract class AbstractDocument implements IDocument, IDocumentExtension, /** The document's line tracker */ private ILineTracker fTracker; /** The registered document listeners */ - private ListenerList fDocumentListeners; + private ListenerList fDocumentListeners; /** The registered pre-notified document listeners */ - private ListenerList fPrenotifiedDocumentListeners; + private ListenerList fPrenotifiedDocumentListeners; /** The registered document partitioning listeners */ - private ListenerList fDocumentPartitioningListeners; + private ListenerList fDocumentPartitioningListeners; /** All positions managed by the document ordered by their start positions. */ private Map> fPositions; /** @@ -211,21 +211,11 @@ public abstract class AbstractDocument implements IDocument, IDocumentExtension, return fTracker; } - private static List asList(Object[] listeners) { - // Workaround for Bug 483340: ListenerList should be parameterized - // Use Arrays.asList(..) once that bug is fixed. - return new AbstractList() { - @SuppressWarnings("unchecked") - @Override - public T get(int index) { - return (T) listeners[index]; - } - - @Override - public int size() { - return listeners.length; - } - }; + private static List asList(ListenerList listenerList) { + List list= Arrays.asList(listenerList.getListeners()); + @SuppressWarnings("unchecked") + List castList= (List) list; + return castList; } @@ -235,7 +225,7 @@ public abstract class AbstractDocument implements IDocument, IDocumentExtension, * @return the document's document listeners */ protected List getDocumentListeners() { - return asList(fDocumentListeners.getListeners()); + return asList(fDocumentListeners); } /** @@ -244,7 +234,7 @@ public abstract class AbstractDocument implements IDocument, IDocumentExtension, * @return the document's partitioning listeners */ protected List getDocumentPartitioningListeners() { - return asList(fDocumentPartitioningListeners.getListeners()); + return asList(fDocumentPartitioningListeners); } /** @@ -300,9 +290,9 @@ public abstract class AbstractDocument implements IDocument, IDocumentExtension, fPositions= new HashMap<>(); fEndPositions= new HashMap<>(); fPositionUpdaters= new ArrayList<>(); - fDocumentListeners= new ListenerList(ListenerList.IDENTITY); - fPrenotifiedDocumentListeners= new ListenerList(ListenerList.IDENTITY); - fDocumentPartitioningListeners= new ListenerList(ListenerList.IDENTITY); + fDocumentListeners= new ListenerList<>(ListenerList.IDENTITY); + fPrenotifiedDocumentListeners= new ListenerList<>(ListenerList.IDENTITY); + fDocumentPartitioningListeners= new ListenerList<>(ListenerList.IDENTITY); fDocumentRewriteSessionListeners= new ArrayList<>(); addPositionCategory(DEFAULT_CATEGORY); @@ -544,9 +534,9 @@ public abstract class AbstractDocument implements IDocument, IDocumentExtension, if (fDocumentPartitioningListeners == null) return; - Object[] listeners= fDocumentPartitioningListeners.getListeners(); - for (int i= 0; i < listeners.length; i++) - ((IDocumentPartitioningListener)listeners[i]).documentPartitioningChanged(this); + for (IDocumentPartitioningListener listener : fDocumentPartitioningListeners) { + listener.documentPartitioningChanged(this); + } } /** @@ -566,9 +556,7 @@ public abstract class AbstractDocument implements IDocument, IDocumentExtension, if (fDocumentPartitioningListeners == null) return; - Object[] listeners= fDocumentPartitioningListeners.getListeners(); - for (int i= 0; i < listeners.length; i++) { - IDocumentPartitioningListener l= (IDocumentPartitioningListener)listeners[i]; + for (IDocumentPartitioningListener l : fDocumentPartitioningListeners) { try { if (l instanceof IDocumentPartitioningListenerExtension) ((IDocumentPartitioningListenerExtension)l).documentPartitioningChanged(this, region); @@ -593,9 +581,7 @@ public abstract class AbstractDocument implements IDocument, IDocumentExtension, if (fDocumentPartitioningListeners == null) return; - Object[] listeners= fDocumentPartitioningListeners.getListeners(); - for (int i= 0; i < listeners.length; i++) { - IDocumentPartitioningListener l= (IDocumentPartitioningListener)listeners[i]; + for (IDocumentPartitioningListener l : fDocumentPartitioningListeners) { try { if (l instanceof IDocumentPartitioningListenerExtension2) { IDocumentPartitioningListenerExtension2 extension2= (IDocumentPartitioningListenerExtension2)l; @@ -641,19 +627,17 @@ public abstract class AbstractDocument implements IDocument, IDocumentExtension, } } - Object[] listeners= fPrenotifiedDocumentListeners.getListeners(); - for (int i= 0; i < listeners.length; i++) { + for (IDocumentListener listener : fPrenotifiedDocumentListeners) { try { - ((IDocumentListener)listeners[i]).documentAboutToBeChanged(event); + listener.documentAboutToBeChanged(event); } catch (Exception ex) { log(ex); } } - listeners= fDocumentListeners.getListeners(); - for (int i= 0; i < listeners.length; i++) { + for (IDocumentListener listener : fDocumentListeners) { try { - ((IDocumentListener)listeners[i]).documentAboutToBeChanged(event); + listener.documentAboutToBeChanged(event); } catch (Exception ex) { log(ex); } @@ -746,19 +730,17 @@ public abstract class AbstractDocument implements IDocument, IDocumentExtension, if (p != null && !p.isEmpty()) fireDocumentPartitioningChanged(p); - Object[] listeners= fPrenotifiedDocumentListeners.getListeners(); - for (int i= 0; i < listeners.length; i++) { + for (IDocumentListener listener : fPrenotifiedDocumentListeners) { try { - ((IDocumentListener)listeners[i]).documentChanged(event); + listener.documentChanged(event); } catch (Exception ex) { log(ex); } } - listeners= fDocumentListeners.getListeners(); - for (int i= 0; i < listeners.length; i++) { + for (IDocumentListener listener : fDocumentListeners) { try { - ((IDocumentListener)listeners[i]).documentChanged(event); + listener.documentChanged(event); } catch (Exception ex) { log(ex); } diff --git a/org.eclipse.text/src/org/eclipse/text/undo/DocumentUndoManager.java b/org.eclipse.text/src/org/eclipse/text/undo/DocumentUndoManager.java index 4ead31215b1..aef594799a0 100644 --- a/org.eclipse.text/src/org/eclipse/text/undo/DocumentUndoManager.java +++ b/org.eclipse.text/src/org/eclipse/text/undo/DocumentUndoManager.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2006, 2015 IBM Corporation and others. + * Copyright (c) 2006, 2016 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -735,7 +735,7 @@ public class DocumentUndoManager implements IDocumentUndoManager { private boolean fOverwriting= false; /** The registered document listeners. */ - private ListenerList fDocumentUndoListeners; + private ListenerList fDocumentUndoListeners; /** The list of clients connected. */ private List fConnected; @@ -753,7 +753,7 @@ public class DocumentUndoManager implements IDocumentUndoManager { fHistory= OperationHistoryFactory.getOperationHistory(); fUndoContext= new ObjectUndoContext(fDocument); fConnected= new ArrayList<>(); - fDocumentUndoListeners= new ListenerList(ListenerList.IDENTITY); + fDocumentUndoListeners= new ListenerList<>(ListenerList.IDENTITY); } @Override @@ -873,9 +873,8 @@ public class DocumentUndoManager implements IDocumentUndoManager { void fireDocumentUndo(int offset, String text, String preservedText, Object source, int eventType, boolean isCompound) { eventType= isCompound ? eventType | DocumentUndoEvent.COMPOUND : eventType; DocumentUndoEvent event= new DocumentUndoEvent(fDocument, offset, text, preservedText, eventType, source); - Object[] listeners= fDocumentUndoListeners.getListeners(); - for (int i= 0; i < listeners.length; i++) { - ((IDocumentUndoListener)listeners[i]).documentUndoNotification(event); + for (IDocumentUndoListener listener : fDocumentUndoListeners) { + listener.documentUndoNotification(event); } } -- cgit v1.2.3