]> source.dussan.org Git - poi.git/commitdiff
#64036 - Replace reflection calls in factories for Java 9+
authorAndreas Beeker <kiwiwings@apache.org>
Sun, 16 Aug 2020 14:32:54 +0000 (14:32 +0000)
committerAndreas Beeker <kiwiwings@apache.org>
Sun, 16 Aug 2020 14:32:54 +0000 (14:32 +0000)
migrated XDGF factories

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1880900 13f79535-47bb-0310-9956-ffa450edef68

src/ooxml/java/org/apache/poi/xdgf/usermodel/section/GeometryRowTypes.java [new file with mode: 0644]
src/ooxml/java/org/apache/poi/xdgf/usermodel/section/GeometrySection.java
src/ooxml/java/org/apache/poi/xdgf/usermodel/section/XDGFSection.java
src/ooxml/java/org/apache/poi/xdgf/usermodel/section/XDGFSectionTypes.java [new file with mode: 0644]
src/ooxml/java/org/apache/poi/xdgf/usermodel/section/geometry/GeometryRowFactory.java [deleted file]
src/ooxml/java/org/apache/poi/xdgf/util/ObjectFactory.java [deleted file]

diff --git a/src/ooxml/java/org/apache/poi/xdgf/usermodel/section/GeometryRowTypes.java b/src/ooxml/java/org/apache/poi/xdgf/usermodel/section/GeometryRowTypes.java
new file mode 100644 (file)
index 0000000..b841da4
--- /dev/null
@@ -0,0 +1,89 @@
+/* ====================================================================
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to You under the Apache License, Version 2.0
+   (the "License"); you may not use this file except in compliance with
+   the License.  You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+==================================================================== */
+
+package org.apache.poi.xdgf.usermodel.section;
+
+import java.util.Map;
+import java.util.function.Function;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+import com.microsoft.schemas.office.visio.x2012.main.RowType;
+import org.apache.poi.ooxml.POIXMLException;
+import org.apache.poi.util.Internal;
+import org.apache.poi.xdgf.usermodel.section.geometry.ArcTo;
+import org.apache.poi.xdgf.usermodel.section.geometry.Ellipse;
+import org.apache.poi.xdgf.usermodel.section.geometry.EllipticalArcTo;
+import org.apache.poi.xdgf.usermodel.section.geometry.GeometryRow;
+import org.apache.poi.xdgf.usermodel.section.geometry.InfiniteLine;
+import org.apache.poi.xdgf.usermodel.section.geometry.LineTo;
+import org.apache.poi.xdgf.usermodel.section.geometry.MoveTo;
+import org.apache.poi.xdgf.usermodel.section.geometry.NURBSTo;
+import org.apache.poi.xdgf.usermodel.section.geometry.PolyLineTo;
+import org.apache.poi.xdgf.usermodel.section.geometry.RelCubBezTo;
+import org.apache.poi.xdgf.usermodel.section.geometry.RelEllipticalArcTo;
+import org.apache.poi.xdgf.usermodel.section.geometry.RelLineTo;
+import org.apache.poi.xdgf.usermodel.section.geometry.RelMoveTo;
+import org.apache.poi.xdgf.usermodel.section.geometry.RelQuadBezTo;
+import org.apache.poi.xdgf.usermodel.section.geometry.SplineKnot;
+import org.apache.poi.xdgf.usermodel.section.geometry.SplineStart;
+
+@Internal
+enum GeometryRowTypes {
+    ARC_TO("ArcTo", ArcTo::new),
+    ELLIPSE("Ellipse", Ellipse::new),
+    ELLIPTICAL_ARC_TO("EllipticalArcTo", EllipticalArcTo::new),
+    INFINITE_LINE("InfiniteLine", InfiniteLine::new),
+    LINE_TO("LineTo", LineTo::new),
+    MOVE_TO("MoveTo", MoveTo::new),
+    NURBS_TO("NURBSTo", NURBSTo::new),
+    // Note - two different spellings depending on version used...!
+    POLYLINE_TO("PolylineTo", PolyLineTo::new),
+    REL_CUB_BEZ_TO("RelCubBezTo", RelCubBezTo::new),
+    REL_ELLIPTICAL_ARC_TO("RelEllipticalArcTo", RelEllipticalArcTo::new),
+    REL_LINE_TO("RelLineTo", RelLineTo::new),
+    REL_MOVE_TO("RelMoveTo", RelMoveTo::new),
+    REL_QUAD_BEZ_TO("RelQuadBezTo", RelQuadBezTo::new),
+    SPLINE_KNOT("SplineKnot", SplineKnot::new),
+    SPLINE_START("SplineStart", SplineStart::new)
+    ;
+
+    public String rowType;
+    public Function<RowType, ? extends GeometryRow> constructor;
+
+    GeometryRowTypes(String rowType, Function<RowType, ? extends GeometryRow> constructor) {
+        this.rowType = rowType;
+        this.constructor = constructor;
+    }
+
+    public String getRowType() {
+        return rowType;
+    }
+
+    public static GeometryRow load(RowType row) {
+        final String name = row.getT();
+        GeometryRowTypes l = LOOKUP.get(name);
+        if (l == null) {
+            final String typeName = row.schemaType().getName().getLocalPart();
+            throw new POIXMLException("Invalid '" + typeName + "' name '" + name + "'");
+        }
+        return l.constructor.apply(row);
+    }
+
+    private static final Map<String, GeometryRowTypes> LOOKUP =
+        Stream.of(values()).collect(Collectors.toMap(GeometryRowTypes::getRowType, Function.identity()));
+}
index cb212e343b1e2b19e3eb0b7e121cedaed24a85b3..b9da05f1f542409f48ddb3a6182e0d71cc97a8c7 100644 (file)
@@ -23,6 +23,8 @@ import java.util.Map.Entry;
 import java.util.SortedMap;
 import java.util.TreeMap;
 
+import com.microsoft.schemas.office.visio.x2012.main.RowType;
+import com.microsoft.schemas.office.visio.x2012.main.SectionType;
 import org.apache.poi.ooxml.POIXMLException;
 import org.apache.poi.xdgf.geom.SplineCollector;
 import org.apache.poi.xdgf.usermodel.XDGFCell;
@@ -30,14 +32,10 @@ import org.apache.poi.xdgf.usermodel.XDGFShape;
 import org.apache.poi.xdgf.usermodel.XDGFSheet;
 import org.apache.poi.xdgf.usermodel.section.geometry.Ellipse;
 import org.apache.poi.xdgf.usermodel.section.geometry.GeometryRow;
-import org.apache.poi.xdgf.usermodel.section.geometry.GeometryRowFactory;
 import org.apache.poi.xdgf.usermodel.section.geometry.InfiniteLine;
 import org.apache.poi.xdgf.usermodel.section.geometry.SplineKnot;
 import org.apache.poi.xdgf.usermodel.section.geometry.SplineStart;
 
-import com.microsoft.schemas.office.visio.x2012.main.RowType;
-import com.microsoft.schemas.office.visio.x2012.main.SectionType;
-
 public class GeometrySection extends XDGFSection {
 
     GeometrySection _master;
@@ -52,7 +50,7 @@ public class GeometrySection extends XDGFSection {
             if (_rows.containsKey(row.getIX()))
                 throw new POIXMLException("Index element '" + row.getIX() + "' already exists");
 
-            _rows.put(row.getIX(), GeometryRowFactory.load(row));
+            _rows.put(row.getIX(), GeometryRowTypes.load(row));
         }
     }
 
index d3cc1a9d166028ba2dd02cc7ea629825242a987d..c6340456a232b1f38f49d11d3a2158f3436dafd8 100644 (file)
@@ -20,49 +20,18 @@ package org.apache.poi.xdgf.usermodel.section;
 import java.util.HashMap;
 import java.util.Map;
 
-import org.apache.poi.ooxml.POIXMLException;
+import com.microsoft.schemas.office.visio.x2012.main.CellType;
+import com.microsoft.schemas.office.visio.x2012.main.SectionType;
 import org.apache.poi.util.Internal;
 import org.apache.poi.xdgf.usermodel.XDGFCell;
 import org.apache.poi.xdgf.usermodel.XDGFSheet;
-import org.apache.poi.xdgf.util.ObjectFactory;
-
-import com.microsoft.schemas.office.visio.x2012.main.CellType;
-import com.microsoft.schemas.office.visio.x2012.main.SectionType;
 
 public abstract class XDGFSection {
 
-    static final ObjectFactory<XDGFSection, SectionType> _sectionTypes;
-
-    static {
-        _sectionTypes = new ObjectFactory<>();
-        try {
-            _sectionTypes.put("LineGradient",  GenericSection.class, SectionType.class, XDGFSheet.class);
-            _sectionTypes.put("FillGradient",  GenericSection.class, SectionType.class, XDGFSheet.class);
-            _sectionTypes.put("Character",             CharacterSection.class, SectionType.class, XDGFSheet.class);
-            _sectionTypes.put("Paragraph",             GenericSection.class, SectionType.class, XDGFSheet.class);
-            _sectionTypes.put("Tabs",                  GenericSection.class, SectionType.class, XDGFSheet.class);
-            _sectionTypes.put("Scratch",               GenericSection.class, SectionType.class, XDGFSheet.class);
-            _sectionTypes.put("Connection",    GenericSection.class, SectionType.class, XDGFSheet.class);
-            _sectionTypes.put("ConnectionABCD", GenericSection.class, SectionType.class, XDGFSheet.class);
-            _sectionTypes.put("Field",                         GenericSection.class, SectionType.class, XDGFSheet.class);
-            _sectionTypes.put("Control",               GenericSection.class, SectionType.class, XDGFSheet.class);
-            _sectionTypes.put("Geometry",              GeometrySection.class, SectionType.class, XDGFSheet.class);
-            _sectionTypes.put("Actions",               GenericSection.class, SectionType.class, XDGFSheet.class);
-            _sectionTypes.put("Layer",                         GenericSection.class, SectionType.class, XDGFSheet.class);
-            _sectionTypes.put("User",                  GenericSection.class, SectionType.class, XDGFSheet.class);
-            _sectionTypes.put("Property",              GenericSection.class, SectionType.class, XDGFSheet.class);
-            _sectionTypes.put("Hyperlink",             GenericSection.class, SectionType.class, XDGFSheet.class);
-            _sectionTypes.put("Reviewer",              GenericSection.class, SectionType.class, XDGFSheet.class);
-            _sectionTypes.put("Annotation",    GenericSection.class, SectionType.class, XDGFSheet.class);
-            _sectionTypes.put("ActionTag",             GenericSection.class, SectionType.class, XDGFSheet.class);
-        } catch (NoSuchMethodException | SecurityException e) {
-            throw new POIXMLException("Internal error");
-        }
 
-    }
 
     public static XDGFSection load(SectionType section, XDGFSheet containingSheet) {
-        return _sectionTypes.load(section.getN(), section, containingSheet);
+        return XDGFSectionTypes.load(section, containingSheet);
     }
 
 
@@ -72,7 +41,7 @@ public abstract class XDGFSection {
     protected Map<String, XDGFCell> _cells = new HashMap<>();
 
 
-    public XDGFSection(SectionType section, XDGFSheet containingSheet) {
+    protected XDGFSection(SectionType section, XDGFSheet containingSheet) {
         _section = section;
         _containingSheet = containingSheet;
 
diff --git a/src/ooxml/java/org/apache/poi/xdgf/usermodel/section/XDGFSectionTypes.java b/src/ooxml/java/org/apache/poi/xdgf/usermodel/section/XDGFSectionTypes.java
new file mode 100644 (file)
index 0000000..8aa0bdc
--- /dev/null
@@ -0,0 +1,77 @@
+/* ====================================================================
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to You under the Apache License, Version 2.0
+   (the "License"); you may not use this file except in compliance with
+   the License.  You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+==================================================================== */
+
+package org.apache.poi.xdgf.usermodel.section;
+
+import java.util.Map;
+import java.util.function.BiFunction;
+import java.util.function.Function;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+import com.microsoft.schemas.office.visio.x2012.main.SectionType;
+import org.apache.poi.ooxml.POIXMLException;
+import org.apache.poi.util.Internal;
+import org.apache.poi.xdgf.usermodel.XDGFSheet;
+
+@Internal
+enum XDGFSectionTypes {
+    LINE_GRADIENT("LineGradient", GenericSection::new),
+    FILL_GRADIENT("FillGradient", GenericSection::new),
+    CHARACTER("Character", CharacterSection::new),
+    PARAGRAPH("Paragraph", GenericSection::new),
+    TABS("Tabs", GenericSection::new),
+    SCRATCH("Scratch", GenericSection::new),
+    CONNECTION("Connection", GenericSection::new),
+    CONNECTION_ABCD("ConnectionABCD", GenericSection::new),
+    FIELD("Field", GenericSection::new),
+    CONTROL("Control", GenericSection::new),
+    GEOMETRY("Geometry", GeometrySection::new),
+    ACTIONS("Actions", GenericSection::new),
+    LAYER("Layer", GenericSection::new),
+    USER("User", GenericSection::new),
+    PROPERTY("Property", GenericSection::new),
+    HYPERLINK("Hyperlink", GenericSection::new),
+    REVIEWER("Reviewer", GenericSection::new),
+    ANNOTATION("Annotation", GenericSection::new),
+    ACTION_TAG("ActionTag", GenericSection::new);
+
+    public String sectionType;
+    public BiFunction<SectionType, XDGFSheet, ? extends XDGFSection> constructor;
+
+    XDGFSectionTypes(String sectionType, BiFunction<SectionType, XDGFSheet, ? extends XDGFSection> constructor) {
+        this.sectionType = sectionType;
+        this.constructor = constructor;
+    }
+
+    public String getSectionType() {
+        return sectionType;
+    }
+
+    public static XDGFSection load(SectionType section, XDGFSheet containingSheet) {
+        final String name = section.getN();
+        XDGFSectionTypes l = LOOKUP.get(name);
+        if (l == null) {
+            final String typeName = section.schemaType().getName().getLocalPart();
+            throw new POIXMLException("Invalid '" + typeName + "' name '" + name + "'");
+        }
+        return l.constructor.apply(section, containingSheet);
+    }
+
+    private static final Map<String, XDGFSectionTypes> LOOKUP =
+        Stream.of(values()).collect(Collectors.toMap(XDGFSectionTypes::getSectionType, Function.identity()));
+}
diff --git a/src/ooxml/java/org/apache/poi/xdgf/usermodel/section/geometry/GeometryRowFactory.java b/src/ooxml/java/org/apache/poi/xdgf/usermodel/section/geometry/GeometryRowFactory.java
deleted file mode 100644 (file)
index eb9eefd..0000000
+++ /dev/null
@@ -1,61 +0,0 @@
-/* ====================================================================
-   Licensed to the Apache Software Foundation (ASF) under one or more
-   contributor license agreements.  See the NOTICE file distributed with
-   this work for additional information regarding copyright ownership.
-   The ASF licenses this file to You under the Apache License, Version 2.0
-   (the "License"); you may not use this file except in compliance with
-   the License.  You may obtain a copy of the License at
-
-       http://www.apache.org/licenses/LICENSE-2.0
-
-   Unless required by applicable law or agreed to in writing, software
-   distributed under the License is distributed on an "AS IS" BASIS,
-   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-   See the License for the specific language governing permissions and
-   limitations under the License.
-==================================================================== */
-
-package org.apache.poi.xdgf.usermodel.section.geometry;
-
-import org.apache.poi.ooxml.POIXMLException;
-import org.apache.poi.xdgf.util.ObjectFactory;
-
-import com.microsoft.schemas.office.visio.x2012.main.RowType;
-
-public class GeometryRowFactory {
-
-    static final ObjectFactory<GeometryRow, RowType> _rowTypes;
-
-    static {
-        _rowTypes = new ObjectFactory<>();
-        try {
-            _rowTypes.put("ArcTo", ArcTo.class, RowType.class);
-            _rowTypes.put("Ellipse", Ellipse.class, RowType.class);
-            _rowTypes.put("EllipticalArcTo", EllipticalArcTo.class,
-                    RowType.class);
-            _rowTypes.put("InfiniteLine", InfiniteLine.class, RowType.class);
-            _rowTypes.put("LineTo", LineTo.class, RowType.class);
-            _rowTypes.put("MoveTo", MoveTo.class, RowType.class);
-            _rowTypes.put("NURBSTo", NURBSTo.class, RowType.class);
-            // Note - two different spellings depending on version used...!
-            _rowTypes.put("PolylineTo", PolyLineTo.class, RowType.class);
-            _rowTypes.put("PolyLineTo", PolyLineTo.class, RowType.class);
-            _rowTypes.put("RelCubBezTo", RelCubBezTo.class, RowType.class);
-            _rowTypes.put("RelEllipticalArcTo", RelEllipticalArcTo.class,
-                    RowType.class);
-            _rowTypes.put("RelLineTo", RelLineTo.class, RowType.class);
-            _rowTypes.put("RelMoveTo", RelMoveTo.class, RowType.class);
-            _rowTypes.put("RelQuadBezTo", RelQuadBezTo.class, RowType.class);
-            _rowTypes.put("SplineKnot", SplineKnot.class, RowType.class);
-            _rowTypes.put("SplineStart", SplineStart.class, RowType.class);
-        } catch (NoSuchMethodException | SecurityException e) {
-            throw new POIXMLException("Internal error", e);
-        }
-
-    }
-
-    public static GeometryRow load(RowType row) {
-        return _rowTypes.load(row.getT(), row);
-    }
-
-}
diff --git a/src/ooxml/java/org/apache/poi/xdgf/util/ObjectFactory.java b/src/ooxml/java/org/apache/poi/xdgf/util/ObjectFactory.java
deleted file mode 100644 (file)
index 237d3d1..0000000
+++ /dev/null
@@ -1,57 +0,0 @@
-/* ====================================================================
-   Licensed to the Apache Software Foundation (ASF) under one or more
-   contributor license agreements.  See the NOTICE file distributed with
-   this work for additional information regarding copyright ownership.
-   The ASF licenses this file to You under the Apache License, Version 2.0
-   (the "License"); you may not use this file except in compliance with
-   the License.  You may obtain a copy of the License at
-
-       http://www.apache.org/licenses/LICENSE-2.0
-
-   Unless required by applicable law or agreed to in writing, software
-   distributed under the License is distributed on an "AS IS" BASIS,
-   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-   See the License for the specific language governing permissions and
-   limitations under the License.
-==================================================================== */
-
-package org.apache.poi.xdgf.util;
-
-import java.lang.reflect.Constructor;
-import java.lang.reflect.InvocationTargetException;
-import java.util.HashMap;
-import java.util.Map;
-
-import org.apache.poi.ooxml.POIXMLException;
-import org.apache.xmlbeans.XmlObject;
-
-
-public class ObjectFactory<T, X extends XmlObject> {
-
-    Map<String, Constructor<? extends T>> _types = new HashMap<>();
-
-    public void put(String typeName, Class<? extends T> cls, Class<?>... varargs) throws NoSuchMethodException, SecurityException {
-        _types.put(typeName, cls.getDeclaredConstructor(varargs));
-    }
-
-    public T load(String name, Object... varargs) {
-        Constructor<? extends T> constructor = _types.get(name);
-        if (constructor == null) {
-
-            @SuppressWarnings("unchecked")
-            X xmlObject = (X) varargs[0];
-
-            String typeName = xmlObject.schemaType().getName().getLocalPart();
-            throw new POIXMLException("Invalid '" + typeName + "' name '" + name + "'");
-        }
-
-        try {
-            return constructor.newInstance(varargs);
-        } catch (InvocationTargetException e) {
-            throw new POIXMLException(e.getCause());
-        } catch (Exception e) {
-            throw new POIXMLException(e);
-        }
-    }
-
-}