View Javadoc

1   /*
2    * Copyright 2010-2013 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package de.smartics.testdoc.core.source;
17  
18  import javax.annotation.processing.ProcessingEnvironment;
19  
20  import com.sun.source.util.Trees;
21  
22  /**
23   * A factory to create instances of {@link SourceCodeHelper}s.
24   *
25   * @author <a href="mailto:robert.reiner@smartics.de">Robert Reiner</a>
26   * @version $Revision:591 $
27   */
28  public final class SourceCodeHelperFactory // NOPMD
29  {
30    // ********************************* Fields *********************************
31  
32    // --- constants ------------------------------------------------------------
33  
34    // --- members --------------------------------------------------------------
35  
36    /**
37     * The processing environment, required to be passed to the Sun
38     * implementation.
39     */
40    private final ProcessingEnvironment processingEnv;
41  
42    /**
43     * Internal flag to show if a dummy or the Sun implementation is to be
44     * returned.
45     */
46    private final boolean createDummies;
47  
48    // ****************************** Initializer *******************************
49  
50    // ****************************** Constructors ******************************
51  
52    /**
53     * Default constructor.
54     */
55    public SourceCodeHelperFactory(final ProcessingEnvironment processingEnv)
56    {
57      this.processingEnv = processingEnv;
58      this.createDummies = !isSunImplementationAvailable();
59    }
60  
61    // ****************************** Inner Classes *****************************
62  
63    // ********************************* Methods ********************************
64  
65    // --- init -----------------------------------------------------------------
66  
67    private static boolean isSunImplementationAvailable()
68    {
69      try
70      {
71        Class.forName("com.sun.source.util.Trees");
72        return true;
73      }
74      catch (final ClassNotFoundException e)
75      {
76        return false;
77      }
78    }
79  
80    // --- get&set --------------------------------------------------------------
81  
82    // --- business -------------------------------------------------------------
83  
84    /**
85     * Creates an instance of the source code helper.
86     *
87     * @return an instance of the source code helper.
88     */
89    public SourceCodeHelper create()
90    {
91      final SourceCodeHelper instance;
92      if (createDummies)
93      {
94        instance = new DummySourceCodeHelper();
95      }
96      else
97      {
98        final Trees treeUtils = Trees.instance(processingEnv);
99        instance = new SunSourceCodeHelper(treeUtils);
100     }
101 
102     return instance;
103   }
104 
105   // --- object basics --------------------------------------------------------
106 
107 }