ASTParser setEnvironment classpath problems [message #1064051] |
Mon, 17 June 2013 12:40 |
Francois Dufault Messages: 2 Registered: June 2013 |
Junior Member |
|
|
Hello everyone.
I readed blogs and tutorials to activate bindings on the JDT AST tree with "external" project (not eclipse plugin). But I have some difficulties with the classpath when passing the setEnvironment to the parser.
This is the simple class that I want to parse:
import org.apache.log4j.Logger;
public class LogTest {
private static Logger LOG = Logger.getLogger(LogTest.class);
}
But when I parse this code with ASTParser, I have those compilations problems:
Pb(390) The import org.apache cannot be resolved
Pb(2) Logger cannot be resolved to a type
Pb(50) Logger cannot be resolved
So, by those errors, the log4j jar library was not found by the compiler.
Here the method I used to load/parse the AST tree:
public static void testJDTParser() {
String javaFile = "C:\\workspace\\BasicProjet\\src\\LogTest.java";
ASTParser parser = ASTParser.newParser(AST.JLS4);
parser.setKind(ASTParser.K_COMPILATION_UNIT);
char[] content = ... //LOADING FILE javaFile INTO CHAR[].
parser.setSource(content);
@SuppressWarnings("unchecked")
Map<String, String> options = JavaCore.getOptions();
options.put(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_1_6);
options.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM,
JavaCore.VERSION_1_6);
options.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_6);
String unitName = "BasicProjet/LogTest.java";
parser.setUnitName(unitName);
String[] sources = { "C:\\workspace\\BasicProjet" };
String[] classPaths = { "C:\\workspace\\BasicProjet\\lib" };
parser.setEnvironment(classPaths, sources,
new String[] { "UTF-8" }, true);
parser.setBindingsRecovery(true);
parser.setResolveBindings(true);
parser.setCompilerOptions(options);
parser.setStatementsRecovery(true);
CompilationUnit result = (CompilationUnit) parser.createAST(null);
IProblem[] problems = result.getProblems();
for (IProblem problem : problems) {
LOG.debug(problem.toString());
}
if (result.getAST().hasBindingsRecovery()) {
LOG.warn("Binding activated.");
}
//Get the "LOG" field of the class LogTest parsed.
TypeDeclaration typeDeclaration =
(TypeDeclaration)result.types().get(0);
FieldDeclaration field = typeDeclaration.getFields()[0];
ITypeBinding binding = field.getType().resolveBinding();
//binding is null here.
LOG.debug("..");
}
The bindings are activated, the message "Binding activated." show up in the console.
The paths are valid. The librairy log4j is in the "lib" directory.
I tried with other librairies (JUnit, commons-io, commons-lang, etc) with the same result.
Dev environment:
Windows XP SP3 (32 bits)
Eclipse JDT Core 3.8.3
Librairies in buildpath of the project containing this testJDTParser() method:
<classpathentry kind="lib" path="lib/org.eclipse.jdt.core_3.8.3.v20130121-145325.jar"/>
<classpathentry kind="lib" path="lib/org.eclipse.core.runtime_3.8.0.v20120521-2346.jar"/>
<classpathentry kind="lib" path="lib/org.eclipse.equinox.common_3.6.100.v20120522-1841.jar"/>
<classpathentry kind="lib" path="lib/org.eclipse.core.resources_3.8.1.v20121114-124432.jar"/>
<classpathentry kind="lib" path="lib/org.eclipse.core.jobs_3.5.200.v20120521-2346.jar"/>
<classpathentry kind="lib" path="lib/org.eclipse.osgi_3.8.2.v20130124-134944.jar" sourcepath="lib/org.eclipse.osgi.source_3.8.2.v20130124-134944.jar"/>
<classpathentry kind="lib" path="lib/org.eclipse.core.contenttype_3.4.200.v20120523-2004.jar"/>
<classpathentry kind="lib" path="lib/org.eclipse.osgi.services_3.3.100.v20120522-1822.jar" sourcepath="lib/org.eclipse.osgi.services.source_3.3.100.v20120522-1822.jar"/>
<classpathentry kind="lib" path="lib/org.eclipse.osgi.util_3.2.300.v20120913-144807.jar"/>
<classpathentry kind="lib" path="lib/org.eclipse.equinox.preferences_3.5.1.v20121031-182809.jar"/>
How can I tell the ASTParser where to find the external librairies ?
Note: I can't use the IProject/IWorkspace, i'm not using a Eclipse Plugin.
Thank you.
Francois.
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03612 seconds