summaryrefslogtreecommitdiff
path: root/src/cuchaz/enigma/analysis/SourcedAst.java
diff options
context:
space:
mode:
authorGravatar jeff2014-07-26 19:24:00 -0400
committerGravatar jeff2014-07-26 19:24:00 -0400
commit295eceece371b516e771de93b6127bf728999483 (patch)
tree20ff6a35de79997fc338d922cae934b8872b11a9 /src/cuchaz/enigma/analysis/SourcedAst.java
downloadenigma-fork-295eceece371b516e771de93b6127bf728999483.tar.gz
enigma-fork-295eceece371b516e771de93b6127bf728999483.tar.xz
enigma-fork-295eceece371b516e771de93b6127bf728999483.zip
initial commit
so far source analysis is working. =)
Diffstat (limited to 'src/cuchaz/enigma/analysis/SourcedAst.java')
-rw-r--r--src/cuchaz/enigma/analysis/SourcedAst.java111
1 files changed, 111 insertions, 0 deletions
diff --git a/src/cuchaz/enigma/analysis/SourcedAst.java b/src/cuchaz/enigma/analysis/SourcedAst.java
new file mode 100644
index 0000000..04c6f03
--- /dev/null
+++ b/src/cuchaz/enigma/analysis/SourcedAst.java
@@ -0,0 +1,111 @@
1/*******************************************************************************
2 * Copyright (c) 2014 Jeff Martin.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the GNU Public License v3.0
5 * which accompanies this distribution, and is available at
6 * http://www.gnu.org/licenses/gpl.html
7 *
8 * Contributors:
9 * Jeff Martin - initial API and implementation
10 ******************************************************************************/
11package cuchaz.enigma.analysis;
12
13import java.io.IOException;
14import java.util.HashMap;
15
16import com.google.common.collect.Maps;
17import com.sun.source.tree.CompilationUnitTree;
18import com.sun.source.tree.ImportTree;
19import com.sun.source.tree.Tree;
20import com.sun.source.util.SourcePositions;
21import com.sun.source.util.Trees;
22
23public class SourcedAst
24{
25 private CompilationUnitTree m_tree;
26 private Trees m_trees;
27 private SourcePositions m_positions;
28 private HashMap<String,String> m_classNameIndex;
29
30 public SourcedAst( CompilationUnitTree tree, Trees trees )
31 {
32 m_tree = tree;
33 m_trees = trees;
34 m_positions = m_trees.getSourcePositions();
35 m_classNameIndex = Maps.newHashMap();
36
37 // index all the class names
38 for( ImportTree importTree : m_tree.getImports() )
39 {
40 // ignore static imports for now
41 if( importTree.isStatic() )
42 {
43 continue;
44 }
45
46 // get the full and simple class names
47 String fullName = importTree.getQualifiedIdentifier().toString();
48 String simpleName = fullName;
49 String[] parts = fullName.split( "\\." );
50 if( parts.length > 0 )
51 {
52 simpleName = parts[parts.length - 1];
53 }
54
55 m_classNameIndex.put( simpleName, fullName );
56 }
57 }
58
59 public int getStart( Tree node )
60 {
61 return (int)m_positions.getStartPosition( m_tree, node );
62 }
63
64 public int getEnd( Tree node )
65 {
66 return (int)m_positions.getEndPosition( m_tree, node );
67 }
68
69 public int getLine( Tree node )
70 {
71 return getLine( getStart( node ) );
72 }
73
74 public int getLine( int pos )
75 {
76 return (int)m_tree.getLineMap().getLineNumber( pos );
77 }
78
79 public CharSequence getSource( )
80 {
81 try
82 {
83 return m_tree.getSourceFile().getCharContent( true );
84 }
85 catch( IOException ex )
86 {
87 throw new Error( ex );
88 }
89 }
90
91 public CharSequence getSource( Tree node )
92 {
93 return getSource().subSequence( getStart( node ), getEnd( node ) );
94 }
95
96 public void visit( TreeVisitor visitor )
97 {
98 m_tree.accept( visitor, this );
99 }
100
101 public String getFullClassName( String simpleClassName )
102 {
103 String fullClassName = m_classNameIndex.get( simpleClassName );
104 if( fullClassName == null )
105 {
106 // no mapping was found, the name is probably already fully-qualified
107 fullClassName = simpleClassName;
108 }
109 return fullClassName;
110 }
111}