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.lang.model.element.Element;
19  
20  import com.sun.source.tree.CompilationUnitTree;
21  import com.sun.source.tree.LineMap;
22  import com.sun.source.tree.Tree;
23  import com.sun.source.util.SourcePositions;
24  import com.sun.source.util.TreePath;
25  import com.sun.source.util.Trees;
26  
27  import de.smartics.util.source.SourceCodeLocation;
28  import de.smartics.util.source.SourceCodeLocation.Position;
29  
30  /**
31   * Implementation of the {@link SourceCodeHelper} interface that uses the AST
32   * implementation of <code>com.sun.source.*</code>.
33   *
34   * @author <a href="mailto:robert.reiner@smartics.de">Robert Reiner</a>
35   * @version $Revision:591 $
36   */
37  public class SunSourceCodeHelper implements SourceCodeHelper
38  {
39    // ********************************* Fields *********************************
40  
41    // --- constants ------------------------------------------------------------
42  
43    // --- members --------------------------------------------------------------
44  
45    /**
46     * The tree utilities instance provided by the Sun API.
47     */
48    private final Trees treeUtils;
49  
50    // ****************************** Initializer *******************************
51  
52    // ****************************** Constructors ******************************
53  
54    /**
55     * Default constructor.
56     */
57    public SunSourceCodeHelper(final Trees treeUtils)
58    {
59      this.treeUtils = treeUtils;
60    }
61  
62    // ****************************** Inner Classes *****************************
63  
64    // ********************************* Methods ********************************
65  
66    // --- init -----------------------------------------------------------------
67  
68    // --- get&set --------------------------------------------------------------
69  
70    // --- business -------------------------------------------------------------
71  
72    /**
73     * Returns the location of the element in the source code.
74     *
75     * @param element the element whose location in the source code is requested.
76     * @return the requested location.
77     */
78    public SourceCodeLocation getSourceCodeLocation(final Element element)
79    {
80      final TreePath treePath = treeUtils.getPath(element);
81      if (treePath != null)
82      {
83        final CompilationUnitTree compileTree = treePath.getCompilationUnit();
84        final SourcePositions positions = treeUtils.getSourcePositions();
85        final Tree elementTree = treeUtils.getTree(element);
86        final long startPos =
87            positions.getStartPosition(compileTree, elementTree);
88        final long endPos = positions.getEndPosition(compileTree, elementTree);
89        final LineMap lines = compileTree.getLineMap();
90        return createLocation(lines, startPos, endPos);
91      }
92      else
93      {
94        return SourceCodeLocation.UNKNOWN_LOCATION;
95      }
96    }
97  
98    private static SourceCodeLocation createLocation(final LineMap lines,
99        final long startPos, final long endPos)
100   {
101     final Position start = createPosition(lines, startPos);
102     final Position end = createPosition(lines, endPos);
103     final SourceCodeLocation location = new SourceCodeLocation(start, end);
104     return location;
105   }
106 
107   private static Position createPosition(final LineMap lines, final long pos)
108   {
109     final long line = lines.getLineNumber(pos);
110     final long column = lines.getColumnNumber(pos);
111     return new Position(line, column);
112   }
113 
114   // --- object basics --------------------------------------------------------
115 
116 }