use of org.eclipse.wst.jsdt.core.IJavaScriptUnit in project webtools.sourceediting by eclipse.
the class JsTranslation method createCompilationUnit.
/**
* Originally from ReconcileStepForJava. Creates an IJavaScriptUnit from
* the contents of the JSP document.
*
* @return an IJavaScriptUnit from the contents of the JSP document
*/
private IJavaScriptUnit createCompilationUnit() throws JavaScriptModelException {
IPackageFragmentRoot root = getDocScope(true);
// $NON-NLS-1$
IJavaScriptUnit cu = root.getPackageFragment("").getJavaScriptUnit(getMangledName() + JsDataTypes.BASE_FILE_EXTENSION).getWorkingCopy(getWorkingCopyOwner(), getProgressMonitor());
IBuffer buffer;
try {
buffer = cu.getBuffer();
} catch (JavaScriptModelException e) {
e.printStackTrace();
buffer = null;
}
if (buffer != null) {
getTranslator().setBuffer(buffer);
}
return cu;
}
use of org.eclipse.wst.jsdt.core.IJavaScriptUnit in project webtools.sourceediting by eclipse.
the class RunCodeHandler method doExecute.
protected void doExecute(IFile jsFile) {
IProject project = jsFile.getProject();
// String namespace = CodeGenUtil.getNameSpace(project);
// final String className = IPCCommandUtil.getPortletHelperClassName(jsFile, namespace);
final IJavaScriptUnit jsUnit = (IJavaScriptUnit) JavaScriptCore.create(jsFile);
if (jsUnit == null)
return;
IProgressMonitor monitor = null;
if (monitor == null) {
monitor = new NullProgressMonitor();
}
// //////////////////////////////////////////////////////////////////////////////
// this part may not be of interest for what you're trying to do
// IType t = null;
// try{
// for (org.eclipse.wst.jsdt.core.IFunction function : jsUnit.getFunctions()) {
// // functions declared in the file outside of any types
// org.eclipse.wst.jsdt.core.ISourceRange sourceRange = function.getSourceRange();
// String source = jsUnit.getBuffer().getText(sourceRange.getOffset(), sourceRange.getLength());
// }
//
// // start with the types in the file
// IType[] types = jsUnit.getTypes();
//
// for (IType type: types) {
// // if (type.getDisplayName().equals(className)) {
// t = type;
// for (IFunction function : type.getFunctions()) {
// // functions declared in a type
// if(function.getDisplayName().equals("sayHello") && !function.isConstructor()) {
// ISourceRange sourceRange = function.getSourceRange();
// String source = jsUnit.getBuffer().getText(sourceRange.getOffset(), sourceRange.getLength());
// }
// }
// // }
// }
// }catch (JavaScriptModelException e) {
// // TODO: handle exception
// //;
// }
// It would be more accurate to walk the AST of the file instead of searching just the top-level children:
// SourceType st = null;
// if(t instanceof SourceType)
// st = (SourceType) t;
// try {
// if(st!=null)
// st.createMethod("function hello(args){}", null, false, null);
//
// } catch (JavaScriptModelException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
ASTParser parser = org.eclipse.wst.jsdt.core.dom.ASTParser.newParser(AST.JLS3);
parser.setSource(jsUnit);
ASTNode ast = parser.createAST(null);
ASTVisitor visitor = new MyNodeVisitor(jsUnit, /*className*/
null, ast);
ast.accept(visitor);
}
Aggregations