Class TestBuilderProxy

 1 /*
 2  * Copyright (c) 2019 Martin Davis.
 3  *
 4  * All rights reserved. This program and the accompanying materials
 5  * are made available under the terms of the Eclipse Public License 2.0
 6  * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
 7  * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v20.html
 8  * and the Eclipse Distribution License is available at
 9  *
10  * http://www.eclipse.org/org/documents/edl-v10.php.
11  */
12 package org.locationtech.jts.util;
13  
14 import java.lang.reflect.Method;
15  
16 import org.locationtech.jts.geom.Geometry;
17  
18 /**
19  * A proxy to call TestBuilder functions dynamically.
20  * If TestBuilder is not present, functions act as a no-op.
21  * <p>
22  * This class is somewhat experimental at the moment, so
23  * is not recommended for production use.
24  * 
25  * @author Martin Davis
26  *
27  */
28 public class TestBuilderProxy {
29   
30   private static final String CLASS_FUNCTIONS_UTIL = "org.locationtech.jtstest.function.FunctionsUtil";
31   private static Class<?> tbClass;
32   private static Method methodShowIndicator;
33  
34   private static void init() {
35     if (tbClass != nullreturn;
36     try {
37       tbClass = TestBuilderProxy.class.getClassLoader().loadClass(CLASS_FUNCTIONS_UTIL);
38       methodShowIndicator = tbClass.getMethod("showIndicator", Geometry.class);
39     }
40     catch (Exception ex) {
41       // Fail silently to avoid unexpected output in production
42       //System.err.println("TestBuilderProxy: Can't init");
43     }
44   }
45  
46   /**
47    * Tests whether the proxy is active (i.e. the TestBuilder is available).
48    * This allows avoiding expensive geometry materialization if not needed.
49    * 
50    * @return true if the proxy is active
51    */
52   public static boolean isActive() {
53     init();
54     return tbClass != null;
55   }
56   
57   // TODO: expose an option in the TestBuilder to make this inactive
58   // This will avoid a huge performance hit if the visualization is not needed
59  
60   public static void showIndicator(Geometry geom) {
61     init();
62     if (methodShowIndicator == nullreturn;
63     
64     try {
65       methodShowIndicator.invoke(null, geom);
66     } catch (Exception e) {
67       // Fail silently to avoid unexpected output in production
68       // Or perhaps should fail noisy, since at this point the function should be working?
69     }
70   }
71 }
72