11package com .microsoft .java .debug .plugin .internal ;
22
3+ import java .net .URI ;
4+ import java .net .URISyntaxException ;
35import java .util .ArrayList ;
46import java .util .HashMap ;
57import java .util .Map ;
2830import org .eclipse .jdt .core .search .SearchPattern ;
2931import org .eclipse .jdt .core .search .SearchRequestor ;
3032import org .eclipse .jdt .internal .debug .core .breakpoints .ValidBreakpointLocationLocator ;
31- // import org.eclipse.jdt.ls.core.internal.JDTUtils;
33+ import org .eclipse .jdt .ls .core .internal .JDTUtils ;
3234
3335import com .microsoft .java .debug .core .DebugException ;
3436import com .microsoft .java .debug .core .Logger ;
3537import com .microsoft .java .debug .core .adapter .AdapterUtils ;
3638import com .microsoft .java .debug .core .adapter .Constants ;
3739import com .microsoft .java .debug .core .adapter .ISourceLookUpProvider ;
38- import com .microsoft .java .debug .plugin .internal .JavaDebuggerServerPlugin ;
3940
4041public class JdtSourceLookUpProvider implements ISourceLookUpProvider {
4142 private HashMap <String , Object > context = new HashMap <String , Object >();
43+ private static final String JDT_SCHEME = "jdt" ;
44+ private static final String PATH_SEPARATOR = "/" ;
4245
4346 @ Override
4447 public void initialize (Map <String , Object > props ) {
@@ -72,35 +75,35 @@ public String[] getFullyQualifiedName(String uri, int[] lines, int[] columns) th
7275 }
7376
7477 String [] fqns = new String [lines .length ];
75- // ITypeRoot typeRoot = JDTUtils.resolveCompilationUnit(uri);
76- // if (typeRoot == null) {
77- // typeRoot = JDTUtils.resolveClassFile(uri);
78- // }
79- //
80- // if (typeRoot != null && lines.length > 0) {
81- // // Currently we only support Java SE 8 Edition (JLS8).
82- // final ASTParser parser = ASTParser.newParser(AST.JLS8);
83- // parser.setResolveBindings(true);
84- // parser.setBindingsRecovery(true);
85- // parser.setStatementsRecovery(true);
86- // parser.setSource(typeRoot);
87- // CompilationUnit cunit = (CompilationUnit) parser.createAST(null);
88- // for (int i = 0; i < lines.length; i++) {
89- // // TODO
90- // // The ValidBreakpointLocationLocator will verify if the current line is a valid location or not.
91- // // If so, it will return the fully qualified name of the class type that contains the current line.
92- // // Otherwise, it will try to find a valid location from the next lines and return it's fully qualified name.
93- // // In current stage, we don't support to move the invalid breakpoint down to the next valid location, and just
94- // // mark it as "unverified".
95- // // In future, we could consider supporting to update the breakpoint to a valid location.
96- // ValidBreakpointLocationLocator locator = new ValidBreakpointLocationLocator(cunit, lines[i], true, true);
97- // cunit.accept(locator);
98- // // When the final valid line location is same as the original line, that represents it's a valid breakpoint.
99- // if (lines[i] == locator.getLineLocation()) {
100- // fqns[i] = locator.getFullyQualifiedTypeName();
101- // }
102- // }
103- // }
78+ ITypeRoot typeRoot = JDTUtils .resolveCompilationUnit (uri );
79+ if (typeRoot == null ) {
80+ typeRoot = JDTUtils .resolveClassFile (uri );
81+ }
82+
83+ if (typeRoot != null && lines .length > 0 ) {
84+ // Currently we only support Java SE 8 Edition (JLS8).
85+ final ASTParser parser = ASTParser .newParser (AST .JLS8 );
86+ parser .setResolveBindings (true );
87+ parser .setBindingsRecovery (true );
88+ parser .setStatementsRecovery (true );
89+ parser .setSource (typeRoot );
90+ CompilationUnit cunit = (CompilationUnit ) parser .createAST (null );
91+ for (int i = 0 ; i < lines .length ; i ++) {
92+ // TODO
93+ // The ValidBreakpointLocationLocator will verify if the current line is a valid location or not.
94+ // If so, it will return the fully qualified name of the class type that contains the current line.
95+ // Otherwise, it will try to find a valid location from the next lines and return it's fully qualified name.
96+ // In current stage, we don't support to move the invalid breakpoint down to the next valid location, and just
97+ // mark it as "unverified".
98+ // In future, we could consider supporting to update the breakpoint to a valid location.
99+ ValidBreakpointLocationLocator locator = new ValidBreakpointLocationLocator (cunit , lines [i ], true , true );
100+ cunit .accept (locator );
101+ // When the final valid line location is same as the original line, that represents it's a valid breakpoint.
102+ if (lines [i ] == locator .getLineLocation ()) {
103+ fqns [i ] = locator .getFullyQualifiedTypeName ();
104+ }
105+ }
106+ }
104107 return fqns ;
105108 }
106109
@@ -124,9 +127,8 @@ public String getSourceContents(String uri) {
124127 if (uri == null ) {
125128 throw new IllegalArgumentException ("uri is null" );
126129 }
127- // IClassFile cf = JDTUtils.resolveClassFile(uri);
128- // return getContents(cf);
129- return "" ;
130+ IClassFile cf = JDTUtils .resolveClassFile (uri );
131+ return getContents (cf );
130132 }
131133
132134 private String getContents (IClassFile cf ) {
@@ -138,7 +140,7 @@ private String getContents(IClassFile cf) {
138140 source = buffer .getContents ();
139141 }
140142 if (source == null ) {
141- // source = JDTUtils.disassemble(cf);
143+ source = JDTUtils .disassemble (cf );
142144 }
143145 } catch (JavaModelException e ) {
144146 Logger .logException ("Failed to parse the source contents of the class file" , e );
@@ -164,40 +166,52 @@ private IJavaProject getJavaProjectFromName(String projectName) throws CoreExcep
164166 }
165167
166168 private String searchDeclarationFileByFqn (String fullyQualifiedName ) {
167- // String projectName = (String) context.get(Constants.PROJECTNAME);
168- // try {
169- // IJavaSearchScope searchScope = projectName != null
170- // ? JDTUtils.createSearchScope(getJavaProjectFromName(projectName))
171- // : SearchEngine.createWorkspaceScope();
172- // SearchPattern pattern = SearchPattern.createPattern(
173- // fullyQualifiedName,
174- // IJavaSearchConstants.TYPE,
175- // IJavaSearchConstants.DECLARATIONS,
176- // SearchPattern.R_EXACT_MATCH);
177- // ArrayList<String> uris = new ArrayList<String>();
178- // SearchRequestor requestor = new SearchRequestor() {
179- // @Override
180- // public void acceptSearchMatch(SearchMatch match) {
181- // Object element = match.getElement();
182- // if (element instanceof IType) {
183- // IType type = (IType) element;
184- // uris.add(type.isBinary() ? JDTUtils. getFileURI(type.getClassFile()) : JDTUtils.getFileURI(type.getResource()));
185- // }
186- // }
187- // };
188- // SearchEngine searchEngine = new SearchEngine();
189- // searchEngine.search(
190- // pattern,
191- // new SearchParticipant[]{
192- // SearchEngine.getDefaultSearchParticipant()
193- // },
194- // searchScope,
195- // requestor,
196- // null /* progress monitor */);
197- // return uris.size() == 0 ? null : uris.get(0);
198- // } catch (CoreException e) {
199- // Logger.logException("Failed to parse java project", e);
200- // }
169+ String projectName = (String ) context .get (Constants .PROJECTNAME );
170+ try {
171+ IJavaSearchScope searchScope = projectName != null
172+ ? JDTUtils .createSearchScope (getJavaProjectFromName (projectName ))
173+ : SearchEngine .createWorkspaceScope ();
174+ SearchPattern pattern = SearchPattern .createPattern (
175+ fullyQualifiedName ,
176+ IJavaSearchConstants .TYPE ,
177+ IJavaSearchConstants .DECLARATIONS ,
178+ SearchPattern .R_EXACT_MATCH );
179+ ArrayList <String > uris = new ArrayList <String >();
180+ SearchRequestor requestor = new SearchRequestor () {
181+ @ Override
182+ public void acceptSearchMatch (SearchMatch match ) {
183+ Object element = match .getElement ();
184+ if (element instanceof IType ) {
185+ IType type = (IType ) element ;
186+ uris .add (type .isBinary () ? getFileURI (type .getClassFile ()) : JDTUtils .getFileURI (type .getResource ()));
187+ }
188+ }
189+ };
190+ SearchEngine searchEngine = new SearchEngine ();
191+ searchEngine .search (
192+ pattern ,
193+ new SearchParticipant []{
194+ SearchEngine .getDefaultSearchParticipant ()
195+ },
196+ searchScope ,
197+ requestor ,
198+ null /* progress monitor */ );
199+ return uris .size () == 0 ? null : uris .get (0 );
200+ } catch (CoreException e ) {
201+ Logger .logException ("Failed to parse java project" , e );
202+ }
201203 return null ;
202204 }
205+
206+ private static String getFileURI (IClassFile classFile ) {
207+ String packageName = classFile .getParent ().getElementName ();
208+ String jarName = classFile .getParent ().getParent ().getElementName ();
209+ try {
210+ return new URI (JDT_SCHEME , "contents" , PATH_SEPARATOR + jarName + PATH_SEPARATOR + packageName + PATH_SEPARATOR + classFile .getElementName (), classFile .getHandleIdentifier (), null ).toASCIIString ();
211+ } catch (URISyntaxException e ) {
212+ e .printStackTrace ();
213+ return null ;
214+ }
215+ }
216+
203217}
0 commit comments