use of org.eclipse.jst.jsp.core.internal.java.JSPTranslationAdapter in project webtools.sourceediting by eclipse.
the class JSPTranslationTest method verifyTranslationHasNoSessionVariables.
private void verifyTranslationHasNoSessionVariables(IFile file) throws JavaModelException {
IDOMModel model = null;
try {
model = (IDOMModel) getStructuredModelForRead(file);
setupAdapterFactory(model);
JSPTranslationAdapter adapter = (JSPTranslationAdapter) model.getDocument().getAdapterFor(IJSPTranslation.class);
ICompilationUnit cu = adapter.getJSPTranslation().getCompilationUnit();
cu.makeConsistent(new NullProgressMonitor());
IType[] types = cu.getAllTypes();
for (int i = 0; i < types.length; i++) {
IJavaElement[] members = types[i].getChildren();
for (int k = 0; k < members.length; k++) {
// check fields for name "session"
if (members[k].getElementType() == IJavaElement.FIELD) {
assertFalse("field named \"session\" exists", members[k].getElementName().equals(JSP11Namespace.ATTR_NAME_SESSION));
} else /*
* check "public void
* _jspService(javax.servlet.http.HttpServletRequest
* request, javax.servlet.http.HttpServletResponse
* response)" for local variables named "session"
*/
if (members[k].getElementType() == IJavaElement.METHOD && members[k].getElementName().startsWith("_jspService")) {
ICompilationUnit compilationUnit = ((IMethod) members[k]).getCompilationUnit();
compilationUnit.makeConsistent(new NullProgressMonitor());
ASTParser parser = ASTParser.newParser(AST.JLS3);
parser.setSource(cu);
ASTNode node = parser.createAST(null);
node.accept(new ASTVisitor() {
public boolean visit(VariableDeclarationStatement node) {
Iterator fragments = node.fragments().iterator();
while (fragments.hasNext()) {
VariableDeclarationFragment fragment = (VariableDeclarationFragment) fragments.next();
if (fragment.getName().getFullyQualifiedName().equals(JSP11Namespace.ATTR_NAME_SESSION)) {
String typeName = ((SimpleType) node.getType()).getName().getFullyQualifiedName();
assertFalse("local variable of type \"javax.servlet.http.HttpSession\" and named \"session\" exists", typeName.equals("javax.servlet.http.HttpSession"));
}
}
return super.visit(node);
}
});
}
}
}
} finally {
if (model != null)
model.releaseFromRead();
}
}
use of org.eclipse.jst.jsp.core.internal.java.JSPTranslationAdapter in project webtools.sourceediting by eclipse.
the class AddImportHandler method execute.
/* (non-Javadoc)
* @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
*/
public Object execute(ExecutionEvent event) throws ExecutionException {
final IEditorSite site = HandlerUtil.getActiveEditor(event).getEditorSite();
final ISelectionProvider provider = site.getSelectionProvider();
final ISelection selection = provider != null ? provider.getSelection() : null;
if (selection instanceof IStructuredSelection && selection instanceof ITextSelection) {
final IStructuredSelection structuredSelection = (IStructuredSelection) selection;
final int offset = ((ITextSelection) selection).getOffset();
final Object firstElement = structuredSelection.getFirstElement();
if (firstElement instanceof IDOMNode) {
final IDOMModel model = ((IDOMNode) firstElement).getModel();
INodeAdapter adapter = model.getDocument().getAdapterFor(IJSPTranslation.class);
if (adapter != null) {
JSPTranslationAdapter translationAdapter = (JSPTranslationAdapter) model.getDocument().getAdapterFor(IJSPTranslation.class);
final JSPTranslationExtension translation = translationAdapter.getJSPTranslation();
translation.reconcileCompilationUnit();
final ICompilationUnit cu = translation.getCompilationUnit();
CompilationUnit astRoot = SharedASTProvider.getAST(cu, SharedASTProvider.WAIT_YES, null);
if (astRoot != null) {
final ASTNode node = NodeFinder.perform(astRoot, translation.getJavaOffset(offset), 0);
if (node != null) {
SimpleName name = null;
if (node.getNodeType() == ASTNode.SIMPLE_NAME) {
name = (SimpleName) node;
} else if (node.getNodeType() == ASTNode.QUALIFIED_NAME) {
name = ((QualifiedName) node).getName();
}
if (name != null) {
IBinding binding = name.resolveBinding();
if (binding instanceof ITypeBinding && (binding.getJavaElement() == null || !binding.getJavaElement().exists())) {
// Look it up!
ITypeBinding typeBinding = (ITypeBinding) binding;
final IImportContainer importContainer = cu.getImportContainer();
if (!importContainer.getImport(typeBinding.getQualifiedName()).exists()) {
final List typesFound = new ArrayList();
final TypeNameMatchRequestor collector = new TypeNameMatcher(typesFound);
IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] { cu.getJavaProject() });
final int mode = SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE;
try {
new SearchEngine().searchAllTypeNames(null, mode, name.getIdentifier().toCharArray(), mode, IJavaSearchConstants.CLASS_AND_INTERFACE, scope, collector, IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH, null);
final int length = typesFound.size();
final List elements = new ArrayList();
for (int i = 0; i < length; i++) {
final TypeNameMatch match = (TypeNameMatch) typesFound.get(i);
final int modifiers = match.getModifiers();
if (!Flags.isPrivate(modifiers) && !Flags.isPackageDefault(modifiers)) {
elements.add(match);
}
}
TypeNameMatch match = null;
// If there's only one match, insert it; otherwise, open the dialog to choose from the list
if (elements.size() == 1) {
match = (TypeNameMatch) elements.get(0);
} else if (elements.size() > 1) {
ElementListSelectionDialog dialog = new ElementListSelectionDialog(site.getShell(), LABEL_PROVIDER);
dialog.setElements(elements.toArray(new TypeNameMatch[elements.size()]));
dialog.setTitle(JSPUIMessages.AddImportHandler_title);
dialog.setMessage(JSPUIMessages.AddImportHandler_label);
if (dialog.open() == Window.OK) {
final Object result = dialog.getFirstResult();
if (result instanceof TypeNameMatch) {
match = (TypeNameMatch) result;
}
}
}
addImport(match, model.getStructuredDocument());
} catch (JavaModelException e) {
// $NON-NLS-1$
Logger.logException("Exception while determining import.", e);
}
}
}
}
}
}
}
}
}
return null;
}
use of org.eclipse.jst.jsp.core.internal.java.JSPTranslationAdapter in project webtools.sourceediting by eclipse.
the class XMLJavaHyperlinkDetector method isJspJavaContent.
private boolean isJspJavaContent(IDocument document, IRegion region) {
JSPTranslation translation = null;
IStructuredModel model = null;
try {
model = StructuredModelManager.getModelManager().getExistingModelForRead(document);
if (model instanceof IDOMModel) {
IDOMModel xmlModel = (IDOMModel) model;
if (xmlModel != null) {
final IDOMDocument xmlDoc = xmlModel.getDocument();
final JSPTranslationAdapter adapter = (JSPTranslationAdapter) xmlDoc.getAdapterFor(IJSPTranslation.class);
if (adapter != null) {
translation = adapter.getJSPTranslation();
if (translation != null) {
int javaOffset = translation.getJavaOffset(region.getOffset());
if (javaOffset > -1) {
return true;
}
}
}
}
}
} finally {
if (model != null) {
model.releaseFromRead();
}
}
return false;
}
use of org.eclipse.jst.jsp.core.internal.java.JSPTranslationAdapter in project webtools.sourceediting by eclipse.
the class JSPJavaSelectionProvider method getSelection.
static IJavaElement[] getSelection(ITextEditor textEditor) {
IJavaElement[] elements = null;
IDocument document = textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput());
ISelection selection = textEditor.getSelectionProvider().getSelection();
if (selection instanceof ITextSelection) {
ITextSelection textSelection = (ITextSelection) selection;
// get the JSP translation object for this editor's document
IStructuredModel model = StructuredModelManager.getModelManager().getExistingModelForRead(document);
try {
if (model instanceof IDOMModel) {
IDOMModel xmlModel = (IDOMModel) model;
IDOMDocument xmlDoc = xmlModel.getDocument();
JSPTranslationAdapter adapter = (JSPTranslationAdapter) xmlDoc.getAdapterFor(IJSPTranslation.class);
if (adapter != null) {
JSPTranslation translation = adapter.getJSPTranslation();
elements = translation.getElementsFromJspRange(textSelection.getOffset(), textSelection.getOffset() + textSelection.getLength());
}
}
} finally {
if (model != null)
model.releaseFromRead();
}
}
if (elements == null) {
elements = new IJavaElement[0];
}
return elements;
}
use of org.eclipse.jst.jsp.core.internal.java.JSPTranslationAdapter in project webtools.sourceediting by eclipse.
the class JSPJavaHyperlinkDetector method getJSPTranslation.
/**
* Get JSP translation object
*
* @return JSPTranslation if one exists, null otherwise
*/
private JSPTranslation getJSPTranslation(IDocument document) {
JSPTranslation translation = null;
IDOMModel xmlModel = null;
try {
xmlModel = (IDOMModel) StructuredModelManager.getModelManager().getExistingModelForRead(document);
if (xmlModel != null) {
IDOMDocument xmlDoc = xmlModel.getDocument();
JSPTranslationAdapter adapter = (JSPTranslationAdapter) xmlDoc.getAdapterFor(IJSPTranslation.class);
if (adapter != null) {
translation = adapter.getJSPTranslation();
}
}
} finally {
if (xmlModel != null)
xmlModel.releaseFromRead();
}
return translation;
}
Aggregations