/* * Copyright 2000-2020 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package jetbrains.teamcity.ui.actions; import jetbrains.teamcity.Activator; import jetbrains.teamcity.core.Debug; import jetbrains.teamcity.core.JdtUtil; import jetbrains.teamcity.core.test.IStackTraceElement; import jetbrains.teamcity.ui.UIUtil; import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.NullProgressMonitor; import org.eclipse.jdt.core.*; import org.eclipse.jdt.internal.ui.javaeditor.EditorUtility; import org.eclipse.jface.action.Action; import org.eclipse.jface.action.IAction; import org.eclipse.jface.text.IDocument; import org.eclipse.swt.widgets.Shell; import org.eclipse.ui.texteditor.ITextEditor; import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; public class OpenJavaTestAction extends Action { private static final String GO_TO_FILE = Messages.getString("OpenTestAction.name"); //$NON-NLS-1$ protected String myClassName; private final boolean myIsActivate; private final int myLineNumber; private String myCUName; private String myTestName; private ISourceRange myRange; public OpenJavaTestAction(Shell shell, String fqnTestName, boolean activate) { super(GO_TO_FILE); if (fqnTestName != null) { myTestName = fqnTestName.substring(fqnTestName.lastIndexOf('.') + 1); int lastIndexOf = fqnTestName.lastIndexOf('.'); if (lastIndexOf != -1) { myClassName = fqnTestName.substring(0, lastIndexOf); } } myIsActivate = activate; myLineNumber = -1; } public OpenJavaTestAction(final Shell shell, final IStackTraceElement element, boolean activate) { super(GO_TO_FILE); myClassName = element.getClassName(); myIsActivate = activate; myLineNumber = element.getLineNumber(); } private void openJavaElement() { try { final IJavaElement[] elements = findElement(myClassName, new NullProgressMonitor()); if (elements == null || elements.length == 0) { OpenTestAction.showNotFound(myClassName); } else if (elements.length == 1) { //found single final ITextEditor textEditor = (ITextEditor) EditorUtility.openInEditor(elements[0], myIsActivate); reveal(textEditor, elements[0]); } else { //found multiple final ArrayList actions = new ArrayList(); for (final IJavaElement element : elements) { Action action = new Action(JdtUtil.getElementName(element)) { @Override public void run() { try { final ITextEditor textEditor = (ITextEditor) EditorUtility.openInEditor(element, myIsActivate); reveal(textEditor, element); } catch (CoreException e) { Activator.getDefault().getLog().log(e.getStatus()); } } }; actions.add(action); } UIUtil.showChoice(actions); } } catch (CoreException e) { Activator.getDefault().getLog().log(e.getStatus()); } } protected IJavaElement[] findElement(String className, IProgressMonitor monitor) throws CoreException { final ArrayList elements = new ArrayList(); final IType[] types = findType(className); for (IType type : types) { if (myTestName != null && type != null && type.exists()) { final Collection declaredMethods = JdtUtil.findDeclaredMethod(type, myTestName); if (!declaredMethods.isEmpty()) { elements.addAll(declaredMethods); } else { elements.add(type); } // // IMethod method = type.getMethod(myTestName, new String[0]); // if (method != null){ // if(method.exists()){//TODO: the method can be inherited! // myRange = method.getNameRange(); // elements.add(method); // } else { // final IType[] allSuperTypes = JavaModelUtil.getAllSuperTypes(type, new SubProgressMonitor(monitor, 10)); // System.err.println(method.getDeclaringType()); // final IJavaElement element = type.getPrimaryElement(); // type.getFullyQualifiedParameterizedName(); // System.err.println(type.getFullyQualifiedName()/*type.getSuperclassName()*/); // System.err.println(type.getSuperclassName()/*type.getSuperclassName()*/); // elements.add(type); // } // } } else { elements.add(type); } } return elements.toArray(new IJavaElement[elements.size()]); } protected void reveal(final ITextEditor textEditor, final IJavaElement element) throws JavaModelException { try { if (textEditor != null) {//use line number from stack trace if (myLineNumber >= 0) { IDocument document = textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput()); textEditor.selectAndReveal(document.getLineOffset(myLineNumber - 1), document.getLineLength(myLineNumber - 1)); } else if (myRange != null) {//use predefined range textEditor.selectAndReveal(myRange.getOffset(), myRange.getLength()); } else {//use element range JdtUtil.revealInEditor(textEditor, element); } } } catch (Exception x) { Debug.getInstance().log(Debug.DEBUG_PLATFORM, x); } } protected IType[] findType(String className) throws JavaModelException { final ArrayList types = new ArrayList(); IJavaModel model = JavaCore.create(ResourcesPlugin.getWorkspace().getRoot()); if (model != null) { IJavaProject[] projects = model.getJavaProjects(); for (int i = 0; projects != null && i < projects.length; i++) { IType type = JdtUtil.internalFindType(projects[i], className, new HashSet()); if (type != null && (myCUName == null || type.getCompilationUnit() == null || type.getCompilationUnit().getResource() == null || type.getCompilationUnit().getResource() .getName().equals(myCUName))) { types.add(type); } } } return types.toArray(new IType[types.size()]); } @Override public void run() { openJavaElement(); } }