|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2026, Daniel Schmid and others. |
| 3 | + * |
| 4 | + * This program and the accompanying materials |
| 5 | + * are made available under the terms of the Eclipse Public License 2.0 |
| 6 | + * which accompanies this distribution, and is available at |
| 7 | + * https://www.eclipse.org/legal/epl-2.0/ |
| 8 | + * |
| 9 | + * SPDX-License-Identifier: EPL-2.0 |
| 10 | + * |
| 11 | + * Contributors: |
| 12 | + * Daniel Schmid - initial API and implementation |
| 13 | + *******************************************************************************/ |
| 14 | +package org.eclipse.jdt.ui.tests.editor; |
| 15 | + |
| 16 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 17 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 18 | + |
| 19 | +import org.junit.jupiter.api.AfterEach; |
| 20 | +import org.junit.jupiter.api.BeforeEach; |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | + |
| 23 | +import org.eclipse.jdt.testplugin.JavaProjectHelper; |
| 24 | + |
| 25 | +import org.eclipse.swt.custom.StyleRange; |
| 26 | +import org.eclipse.swt.custom.StyledText; |
| 27 | +import org.eclipse.swt.custom.StyledTextContent; |
| 28 | + |
| 29 | +import org.eclipse.core.runtime.CoreException; |
| 30 | +import org.eclipse.core.runtime.NullProgressMonitor; |
| 31 | + |
| 32 | +import org.eclipse.core.resources.IncrementalProjectBuilder; |
| 33 | + |
| 34 | +import org.eclipse.jdt.core.IJavaProject; |
| 35 | +import org.eclipse.jdt.core.IPackageFragment; |
| 36 | + |
| 37 | +import org.eclipse.jdt.internal.ui.javaeditor.ClassFileEditor; |
| 38 | +import org.eclipse.jdt.internal.ui.javaeditor.EditorUtility; |
| 39 | +import org.eclipse.jdt.internal.ui.util.CoreUtility; |
| 40 | + |
| 41 | +public class ClassFileEditorTests { |
| 42 | + private static final String TYPE_NAME= "HelloWorld"; |
| 43 | + |
| 44 | + private IJavaProject javaProject; |
| 45 | + private boolean wasAutoBuilding; |
| 46 | + |
| 47 | + @BeforeEach |
| 48 | + void setUp() throws Exception { |
| 49 | + javaProject = setUpProject(); |
| 50 | + |
| 51 | + wasAutoBuilding = CoreUtility.setAutoBuilding(false); |
| 52 | + } |
| 53 | + |
| 54 | + @AfterEach |
| 55 | + void tearDown() throws Exception { |
| 56 | + if (javaProject != null) { |
| 57 | + JavaProjectHelper.delete(javaProject); |
| 58 | + } |
| 59 | + CoreUtility.setAutoBuilding(wasAutoBuilding); |
| 60 | + } |
| 61 | + |
| 62 | + private IJavaProject setUpProject() throws Exception { |
| 63 | + javaProject= JavaProjectHelper.createJavaProject(ClassFileInputTests.class.getSimpleName(), "bin"); |
| 64 | + JavaProjectHelper.addSourceContainer(javaProject, "src"); |
| 65 | + JavaProjectHelper.addRTJar(javaProject); |
| 66 | + return javaProject; |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + void testHighlightRange() throws CoreException { |
| 71 | + createHelloWorldClass(); |
| 72 | + ClassFileEditor classEditor= createClassFileEditor(TYPE_NAME); |
| 73 | + classEditor.highlightInstruction("main", "([Ljava/lang/String;)V", 5); |
| 74 | + assertEquals(" 5 invokevirtual java.io.PrintStream.println(java.lang.String) : void [24]", getSingleHighlightedRange(classEditor)); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + void testHighlightInConstructor() throws CoreException { |
| 79 | + createHelloWorldClass(); |
| 80 | + ClassFileEditor classEditor= createClassFileEditor(TYPE_NAME); |
| 81 | + classEditor.highlightInstruction(TYPE_NAME, "()V", 1); |
| 82 | + assertEquals(" 1 invokespecial java.lang.Object() [8]", getSingleHighlightedRange(classEditor)); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + void testChangeHighlightRange() throws CoreException { |
| 87 | + createHelloWorldClass(); |
| 88 | + ClassFileEditor classEditor= createClassFileEditor(TYPE_NAME); |
| 89 | + classEditor.highlightInstruction("main", "([Ljava/lang/String;)V", 5); |
| 90 | + classEditor.highlightInstruction("main", "([Ljava/lang/String;)V", 0); |
| 91 | + assertEquals(" 0 getstatic java.lang.System.out : java.io.PrintStream [16]", getSingleHighlightedRange(classEditor)); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + void testUnhighlight() throws CoreException { |
| 96 | + createHelloWorldClass(); |
| 97 | + ClassFileEditor classEditor= createClassFileEditor(TYPE_NAME); |
| 98 | + classEditor.highlightInstruction("main", "([Ljava/lang/String;)V", 5); |
| 99 | + StyledText noSourceTextWidget= classEditor.getNoSourceTextWidget(); |
| 100 | + assertEquals(1, noSourceTextWidget.getStyleRanges().length); |
| 101 | + classEditor.unhighlight(); |
| 102 | + assertEquals(0, noSourceTextWidget.getStyleRanges().length); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + void testHighlightNonexistentCodeIndex() throws CoreException { |
| 107 | + createHelloWorldClass(); |
| 108 | + ClassFileEditor classEditor= createClassFileEditor(TYPE_NAME); |
| 109 | + classEditor.highlightInstruction("main", "([Ljava/lang/String;)V", 100); |
| 110 | + StyledText noSourceTextWidget= classEditor.getNoSourceTextWidget(); |
| 111 | + assertEquals(0, noSourceTextWidget.getStyleRanges().length); |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + void testHighlightNonexistentMethod() throws CoreException { |
| 116 | + createHelloWorldClass(); |
| 117 | + ClassFileEditor classEditor= createClassFileEditor(TYPE_NAME); |
| 118 | + classEditor.highlightInstruction("main", "a", 5); |
| 119 | + assertEquals(0, classEditor.getNoSourceTextWidget().getStyleRanges().length); |
| 120 | + } |
| 121 | + |
| 122 | + @Test |
| 123 | + void testHighlightMultipleCompilationUnits() throws CoreException { |
| 124 | + createClassFromSource("A.java", """ |
| 125 | + public class A { |
| 126 | + void a() { |
| 127 | + System.out.println("a"); |
| 128 | + } |
| 129 | + } |
| 130 | + class B { |
| 131 | + void b() { |
| 132 | + System.out.println("b"); |
| 133 | + } |
| 134 | + } |
| 135 | + """); |
| 136 | + ClassFileEditor classEditor= createClassFileEditor("A"); |
| 137 | + classEditor.highlightInstruction("a", "()V", 3); |
| 138 | + assertEquals(" 3 ldc <String \"a\"> [21]", getSingleHighlightedRange(classEditor)); |
| 139 | + |
| 140 | + classEditor= createClassFileEditor("B"); |
| 141 | + classEditor.highlightInstruction("b", "()V", 3); |
| 142 | + assertEquals(" 3 ldc <String \"b\"> [21]", getSingleHighlightedRange(classEditor)); |
| 143 | + |
| 144 | + } |
| 145 | + |
| 146 | + private String getSingleHighlightedRange(ClassFileEditor classEditor) { |
| 147 | + StyledText noSourceTextWidget= classEditor.getNoSourceTextWidget(); |
| 148 | + StyleRange[] ranges= noSourceTextWidget.getStyleRanges(); |
| 149 | + assertEquals(1, ranges.length); |
| 150 | + StyledTextContent content= noSourceTextWidget.getContent(); |
| 151 | + StyleRange range= ranges[0]; |
| 152 | + return content.getTextRange(range.start, range.length); |
| 153 | + } |
| 154 | + |
| 155 | + private void createHelloWorldClass() throws CoreException { |
| 156 | + createClassFromSource(TYPE_NAME + ".java", """ |
| 157 | + public class HelloWorld { |
| 158 | + public static void main(String[] args) { |
| 159 | + System.out.println("Hello World"); |
| 160 | + } |
| 161 | + } |
| 162 | + """); |
| 163 | + } |
| 164 | + |
| 165 | + private void createClassFromSource(String fileName, String content) throws CoreException { |
| 166 | + IPackageFragment fragment= javaProject.findPackageFragment(javaProject.getProject().getFullPath().append("src")); |
| 167 | + fragment.createCompilationUnit(fileName, content, true, new NullProgressMonitor()); |
| 168 | + javaProject.getProject().build(IncrementalProjectBuilder.FULL_BUILD, new NullProgressMonitor()); |
| 169 | + javaProject.getProject().getFile("src/" + fileName).delete(true,new NullProgressMonitor()); |
| 170 | + } |
| 171 | + |
| 172 | + private ClassFileEditor createClassFileEditor(String typeName) throws CoreException { |
| 173 | + ClassFileEditor editor= (ClassFileEditor) EditorUtility.openInEditor(javaProject.getProject().getFile("bin/" + typeName + ".class")); |
| 174 | + assertFalse(editor.isEditable()); |
| 175 | + return editor; |
| 176 | + } |
| 177 | +} |
0 commit comments