Updates API & cleanup
emmanue1
8 years ago
1 | 1 | *.class |
2 | 2 | |
3 | 3 | # JD-GUI |
4 | src-generated/ | |
4 | 5 | jd-gui.cfg |
5 | 6 | |
6 | 7 | # Idea |
19 | 20 | # Mac |
20 | 21 | .DS_Store |
21 | 22 | |
23 | #Windows | |
24 | Thumbs.db | |
25 | ||
22 | 26 | # Maven |
23 | 27 | log/ |
24 | 28 | target/ |
29 | 33 | |
30 | 34 | # WinMerge |
31 | 35 | *.bak |
32 |
7 | 7 | import java.net.URI; |
8 | 8 | |
9 | 9 | /** |
10 | * uri = [scheme:][//authority][path][?query][#fragment]<br> | |
11 | * scheme = generic | jar | war | ear | dex | ...<br> | |
12 | * authority = ''<br> | |
13 | * path = path/to/dir/ | path/to/file<br> | |
14 | * query = '' | highlight=text<br> | |
15 | * fragment = '' | type | innertype | lineNumber=... | highlightPattern=...&highlightFlags=[drtcmfs]&highlightScope=...<br> | |
10 | * uri : scheme '://' path ('?' query)? ('#' fragment)?<br> | |
11 | * scheme : 'generic' | 'jar' | 'war' | 'ear' | 'dex' | ...<br> | |
12 | * path : singlePath('!' singlePath)*<br> | |
13 | * singlePath : [path/to/dir/] | [path/to/file]<br> | |
14 | * query : queryLineNumber | queryPosition | querySearch<br> | |
15 | * queryLineNumber : 'lineNumber=' [numeric]<br> | |
16 | * queryPosition : 'position=' [numeric]<br> | |
17 | * querySearch : 'highlightPattern=' queryPattern '&highlightFlags=' queryFlags ('&highlightScope=' typeName)?<br> | |
18 | * queryPattern : [start of string] | [start of type name] | [start of field name] | [start of method name]<br> | |
19 | * queryFlags : 'd'? // Match declarations<br> | |
20 | * 'r'? // Match references<br> | |
21 | * 't'? // Match types<br> | |
22 | * 'c'? // Match constructors<br> | |
23 | * 'm'? // Match methods<br> | |
24 | * 'f'? // Match fields<br> | |
25 | * 's'? // Match strings<br> | |
26 | * fragment : fragmentType | fragmentField | fragmentMethod<br> | |
27 | * fragmentType : typeName<br> | |
28 | * fragmentField : typeName '-' [field name] '-' descriptor<br> | |
29 | * fragmentMethod : typeName '-' [method name] '-' methodDescriptor<br> | |
30 | * methodDescriptor : '(*)?' | // Match all method descriptors<br> | |
31 | * '(' descriptor* ')' descriptor<br> | |
32 | * descriptor : '?' | // Match a primitive or a type name<br> | |
33 | * '['* primitiveOrTypeName<br> | |
34 | * primitiveOrTypeName : 'B' | 'C' | 'D' | 'F' | 'I' | 'J' | 'L' typeName ';' | 'S' | 'Z'<br> | |
35 | * typeName : [internal qualified name] | '*\/' [name]<br> | |
16 | 36 | * <br> |
17 | * Examples: | |
37 | * Examples:<br> | |
18 | 38 | * <ul> |
19 | 39 | * <li>file://dir1/dir2/</li> |
20 | 40 | * <li>file://dir1/dir2/file</li> |
27 | 47 | * <li>jar://dir1/dir2/javafile#type-methodName-descriptor</li> |
28 | 48 | * <li>jar://dir1/dir2/javafile#innertype</li> |
29 | 49 | * <li>jar://dir1/dir2/javafile#innertype-fieldName-?</li> |
30 | * <li>jar://dir1/dir2/javafile#innertype-methodName-(?)?</li> | |
50 | * <li>jar://dir1/dir2/javafile#innertype-methodName-(*)?</li> | |
51 | * <li>jar://dir1/dir2/javafile#innertype-methodName-(?JZLjava/lang/Sting;C)I</li> | |
31 | 52 | * <li>jar://dir1/dir2/javafile#innertype-fieldName-descriptor</li> |
32 | 53 | * <li>jar://dir1/dir2/javafile#innertype-methodName-descriptor</li> |
33 | 54 | * |
34 | * <li>file://dir1/dir2/file?lineNumber=number</li> | |
55 | * <li>file://dir1/dir2/file?lineNumber=numeric</li> | |
35 | 56 | * <li>file://dir1/dir2/file?position=numeric</li> |
36 | * <li>file://dir1/dir2/file?highlightPattern=hello&highlightFlags=drtcmfs&highlightScope=internalTypeName</li> | |
57 | * <li>file://dir1/dir2/file?highlightPattern=hello&highlightFlags=drtcmfs&highlightScope=java/lang/String</li> | |
58 | * <li>file://dir1/dir2/file?highlightPattern=hello&highlightFlags=drtcmfs&highlightScope=*\/String</li> | |
37 | 59 | * </ul> |
38 | 60 | */ |
39 | 61 | public interface UriOpenable { |
13 | 13 | import jd.gui.model.configuration.Configuration |
14 | 14 | import jd.gui.model.container.FilteredContainerWrapper |
15 | 15 | import jd.gui.service.type.TypeFactoryService |
16 | import jd.gui.util.UriUtil | |
17 | 16 | import jd.gui.view.SearchInConstantPoolsView |
18 | 17 | |
19 | 18 | import java.awt.Cursor |
30 | 30 | } |
31 | 31 | |
32 | 32 | // 'cleanIdea' task extension // |
33 | task fullCleanIdea { | |
33 | cleanIdea { | |
34 | 34 | file(project.name + '.iws').delete() |
35 | 35 | ant.delete(dir: 'out') |
36 | 36 | } |
37 | fullCleanIdea.mustRunAfter cleanIdea | |
38 | 37 | |
39 | 38 | // All in one JAR // |
40 | 39 | subprojects.each { subproject -> |
115 | 114 | } |
116 | 115 | |
117 | 116 | distributions { |
118 | osx { | |
119 | contents { | |
120 | into('JD-GUI.app/Contents') { | |
121 | from 'build/distributions/osx' | |
122 | } | |
123 | into('JD-GUI.app/Contents/Resources/Java') { | |
124 | from jar.archivePath | |
125 | } | |
126 | from 'LICENSE', 'NOTICE', 'README.md' | |
117 | osx.contents { | |
118 | into('JD-GUI.app/Contents') { | |
119 | from 'build/distributions/osx' | |
127 | 120 | } |
121 | into('JD-GUI.app/Contents/Resources/Java') { | |
122 | from jar.archivePath | |
123 | } | |
124 | from 'LICENSE', 'NOTICE', 'README.md' | |
128 | 125 | } |
129 | windows { | |
130 | contents { | |
131 | from 'build/launch4j/jd-gui.exe' | |
132 | from 'LICENSE', 'NOTICE', 'README.md' | |
133 | } | |
126 | windows.contents { | |
127 | from 'build/launch4j/jd-gui.exe' | |
128 | from 'LICENSE', 'NOTICE', 'README.md' | |
134 | 129 | } |
135 | 130 | } |
136 | 131 | installOsxDist.dependsOn build |
7 | 7 | compile 'com.fifesoft:rsyntaxtextarea:2.5.6' |
8 | 8 | compile 'jd:jd-core:0.7.1' |
9 | 9 | compile project(':api') |
10 | testCompile 'org.codehaus.groovy:groovy-test:2.4.0' | |
10 | 11 | } |
+1
-1
96 | 96 | if (!initialized) { |
97 | 97 | removeAllChildren() |
98 | 98 | // Create type node |
99 | def type = api.getTypeFactory(entry).make(api, entry, null) | |
99 | def type = api.getTypeFactory(entry)?.make(api, entry, null) | |
100 | 100 | if (type) { |
101 | 101 | add(new TypeTreeNode(entry, type, new TreeNodeBean(label: type.displayTypeName, icon: type.icon), pageFactory)) |
102 | 102 | } |
16 | 16 | import jd.gui.util.decompiler.ClassFileSourcePrinter |
17 | 17 | import jd.gui.util.decompiler.ContainerLoader |
18 | 18 | import jd.gui.util.decompiler.GuiPreferences |
19 | import jd.gui.util.matcher.DescriptorMatcher | |
19 | 20 | import org.fife.ui.rsyntaxtextarea.DocumentRange |
20 | 21 | import org.fife.ui.rsyntaxtextarea.SyntaxConstants |
21 | 22 | |
28 | 29 | extends CustomLineNumbersPage |
29 | 30 | implements UriGettable, IndexesChangeListener, LineNumberNavigable, FocusedTypeGettable, PreferencesChangeListener { |
30 | 31 | |
31 | protected static final String ESCAPE_UNICODE_CHARACTERS = 'ClassFileViewerPreferences.escapeUnicodeCharacters' | |
32 | protected static final String OMIT_THIS_PREFIX = 'ClassFileViewerPreferences.omitThisPrefix' | |
33 | protected static final String REALIGN_LINE_NUMBERS = 'ClassFileViewerPreferences.realignLineNumbers' | |
32 | protected static final String ESCAPE_UNICODE_CHARACTERS = 'ClassFileViewerPreferences.escapeUnicodeCharacters' | |
33 | protected static final String OMIT_THIS_PREFIX = 'ClassFileViewerPreferences.omitThisPrefix' | |
34 | protected static final String REALIGN_LINE_NUMBERS = 'ClassFileViewerPreferences.realignLineNumbers' | |
34 | 35 | protected static final String DISPLAY_DEFAULT_CONSTRUCTOR = 'ClassFileViewerPreferences.displayDefaultConstructor' |
35 | 36 | |
36 | 37 | protected static final Decompiler DECOMPILER = new DecompilerImpl() |
252 | 253 | boolean checkLineNumber(int lineNumber) { lineNumber <= maximumLineNumber } |
253 | 254 | |
254 | 255 | // --- UriOpenable --- // |
256 | /** | |
257 | * @param uri for URI format, @see jd.gui.api.feature.UriOpenable | |
258 | */ | |
255 | 259 | boolean openUri(URI uri) { |
256 | 260 | List<DocumentRange> ranges = [] |
257 | 261 | def fragment = uri.fragment |
260 | 264 | textArea.highlighter.clearMarkAllHighlights() |
261 | 265 | |
262 | 266 | if (fragment) { |
263 | int index = fragment.indexOf('?') | |
264 | ||
265 | if (index == -1) { | |
266 | // Known descriptor ==> Search and high light item | |
267 | def data = declarations.get(fragment) | |
268 | if (data) { | |
269 | ranges.add(new DocumentRange(data.startPosition, data.endPosition)) | |
270 | } | |
271 | } else { | |
272 | // Unknown descriptor ==> Select all and scroll to the first one | |
273 | def prefix = fragment.substring(0, fragment.lastIndexOf('-') + 1) | |
274 | boolean method = (fragment.charAt(index - 1) == '(') | |
275 | int prefixLength = prefix.size() | |
276 | ||
277 | for (def entry : declarations.entrySet()) { | |
278 | if (entry.key.startsWith(prefix)) { | |
279 | def flag = (entry.key.charAt(prefixLength) == '(') | |
280 | if (method == flag) { | |
281 | ranges.add(new DocumentRange(entry.value.startPosition, entry.value.endPosition)) | |
282 | } | |
283 | } | |
284 | } | |
285 | } | |
267 | matchFragmentAndAddDocumentRange(fragment, declarations, ranges) | |
286 | 268 | } |
287 | 269 | |
288 | 270 | if (query) { |
303 | 285 | } |
304 | 286 | } |
305 | 287 | } else { |
306 | def highlightFlags = parameters.get('highlightFlags') | |
307 | def highlightPattern = parameters.get('highlightPattern') | |
308 | ||
309 | if (highlightFlags && highlightPattern) { | |
310 | def highlightScope = parameters.get('highlightScope') | |
311 | def regexp = createRegExp(highlightPattern) | |
312 | def pattern = Pattern.compile(regexp + '.*') | |
313 | ||
314 | if (highlightFlags.indexOf('s') != -1) { | |
315 | // Highlight strings | |
316 | def patternForString = Pattern.compile(regexp) | |
317 | ||
318 | for (def data : strings) { | |
319 | if (!highlightScope || data.owner.equals(highlightScope)) { | |
320 | def matcher = patternForString.matcher(data.text) | |
321 | int offset = data.startPosition | |
322 | ||
323 | while(matcher.find()) { | |
324 | ranges.add(new DocumentRange(offset + matcher.start(), offset + matcher.end())) | |
325 | } | |
326 | } | |
327 | } | |
328 | } | |
329 | ||
330 | boolean t = (highlightFlags.indexOf('t') != -1) // Highlight types | |
331 | boolean f = (highlightFlags.indexOf('f') != -1) // Highlight fields | |
332 | boolean m = (highlightFlags.indexOf('m') != -1) // Highlight methods | |
333 | boolean c = (highlightFlags.indexOf('c') != -1) // Highlight constructors | |
334 | ||
335 | if (highlightFlags.indexOf('d') != -1) { | |
336 | // Highlight declarations | |
337 | for (def entry : declarations.entrySet()) { | |
338 | def declaration = entry.value | |
339 | ||
340 | if (!highlightScope || declaration.type.equals(highlightScope)) { | |
341 | if ((t && declaration.isAType()) || (c && declaration.isAConstructor())) { | |
342 | matchAndAddDocumentRange(pattern, getMostInnerTypeName(declaration.type), declaration.startPosition, declaration.endPosition, ranges) | |
343 | } | |
344 | if ((f && declaration.isAField()) || (m && declaration.isAMethod())) { | |
345 | matchAndAddDocumentRange(pattern, declaration.name, declaration.startPosition, declaration.endPosition, ranges) | |
346 | } | |
347 | } | |
348 | } | |
349 | } | |
350 | ||
351 | if (highlightFlags.indexOf('r') != -1) { | |
352 | // Highlight references | |
353 | for (def entry : hyperlinks.entrySet()) { | |
354 | def hyperlink = entry.value | |
355 | def reference = hyperlink.reference as ReferenceData | |
356 | ||
357 | if (!highlightScope || reference.owner.equals(highlightScope)) { | |
358 | if ((t && reference.isAType()) || (c && reference.isAConstructor())) { | |
359 | matchAndAddDocumentRange(pattern, getMostInnerTypeName(reference.type), hyperlink.startPosition, hyperlink.endPosition, ranges) | |
360 | } | |
361 | if ((f && reference.isAField()) || (m && reference.isAMethod())) { | |
362 | matchAndAddDocumentRange(pattern, reference.name, hyperlink.startPosition, hyperlink.endPosition, ranges) | |
363 | } | |
364 | } | |
365 | } | |
366 | } | |
367 | } | |
288 | matchQueryAndAddDocumentRange(parameters, declarations, hyperlinks, strings, ranges) | |
368 | 289 | } |
369 | 290 | } |
370 | 291 | |
373 | 294 | textArea.markAll(ranges) |
374 | 295 | setCaretPositionAndCenter(ranges.sort().get(0)) |
375 | 296 | } |
376 | } | |
377 | ||
378 | @CompileStatic | |
379 | void matchAndAddDocumentRange(Pattern pattern, String text, int start, int end, List<DocumentRange> ranges) { | |
297 | ||
298 | return true | |
299 | } | |
300 | ||
301 | @CompileStatic | |
302 | static void matchFragmentAndAddDocumentRange( | |
303 | String fragment, HashMap<String, DeclarationData> declarations, List<DocumentRange> ranges) { | |
304 | ||
305 | if ((fragment.indexOf('?') != -1) || (fragment.indexOf('*') != -1)) { | |
306 | // Unknown type and/or descriptor ==> Select all and scroll to the first one | |
307 | int lastDash = fragment.lastIndexOf('-') | |
308 | ||
309 | if (lastDash == -1) { | |
310 | // Search types | |
311 | String slashAndTypeName = fragment.substring(1) | |
312 | String typeName = fragment.substring(2) | |
313 | ||
314 | for (def entry : declarations.entrySet()) { | |
315 | if (entry.key.endsWith(slashAndTypeName) || entry.key.equals(typeName)) { | |
316 | ranges.add(new DocumentRange(entry.value.startPosition, entry.value.endPosition)) | |
317 | } | |
318 | } | |
319 | } else { | |
320 | def prefix = fragment.substring(0, lastDash+1) | |
321 | def suffix = fragment.substring(lastDash+1) | |
322 | def addRangeClosure | |
323 | ||
324 | if (suffix.charAt(0) == '(') { | |
325 | addRangeClosure = { String key, DeclarationData value -> | |
326 | int index = key.lastIndexOf('-') + 1 | |
327 | if (DescriptorMatcher.matchMethodDescriptors(suffix, key.substring(index))) { | |
328 | ranges.add(new DocumentRange(value.startPosition, value.endPosition)) | |
329 | } | |
330 | } | |
331 | } else { | |
332 | addRangeClosure = { String key, DeclarationData value -> | |
333 | int index = key.lastIndexOf('-') + 1 | |
334 | if (DescriptorMatcher.matchFieldDescriptors(suffix, key.substring(index))) { | |
335 | ranges.add(new DocumentRange(value.startPosition, value.endPosition)) | |
336 | } | |
337 | } | |
338 | } | |
339 | ||
340 | if (fragment.charAt(0) == '*') { | |
341 | // Unknown type | |
342 | String slashAndTypeNameAndName = prefix.substring(1) | |
343 | String typeNameAndName = prefix.substring(2) | |
344 | ||
345 | for (def entry : declarations.entrySet()) { | |
346 | if ((entry.key.indexOf(slashAndTypeNameAndName) != -1) || (entry.key.startsWith(typeNameAndName))) { | |
347 | addRangeClosure(entry.key, entry.value) | |
348 | } | |
349 | } | |
350 | } else { | |
351 | // Known type | |
352 | for (def entry : declarations.entrySet()) { | |
353 | if (entry.key.startsWith(prefix)) { | |
354 | addRangeClosure(entry.key, entry.value) | |
355 | } | |
356 | } | |
357 | } | |
358 | } | |
359 | } else { | |
360 | // Known type and descriptor ==> Search and high light item | |
361 | def data = declarations.get(fragment) | |
362 | if (data) { | |
363 | ranges.add(new DocumentRange(data.startPosition, data.endPosition)) | |
364 | } | |
365 | } | |
366 | } | |
367 | ||
368 | @CompileStatic | |
369 | static void matchQueryAndAddDocumentRange( | |
370 | Map<String, String> parameters, | |
371 | HashMap<String, DeclarationData> declarations, TreeMap<Integer, HyperlinkPage.HyperlinkData> hyperlinks, ArrayList<StringData> strings, | |
372 | List<DocumentRange> ranges) { | |
373 | ||
374 | def highlightFlags = parameters.get('highlightFlags') | |
375 | def highlightPattern = parameters.get('highlightPattern') | |
376 | ||
377 | if (highlightFlags && highlightPattern) { | |
378 | def highlightScope = parameters.get('highlightScope') | |
379 | def regexp = createRegExp(highlightPattern) | |
380 | def pattern = Pattern.compile(regexp + '.*') | |
381 | ||
382 | if (highlightFlags.indexOf('s') != -1) { | |
383 | // Highlight strings | |
384 | def patternForString = Pattern.compile(regexp) | |
385 | ||
386 | for (def data : strings) { | |
387 | if (matchScope(highlightScope, data.owner)) { | |
388 | def matcher = patternForString.matcher(data.text) | |
389 | int offset = data.startPosition | |
390 | ||
391 | while(matcher.find()) { | |
392 | ranges.add(new DocumentRange(offset + matcher.start(), offset + matcher.end())) | |
393 | } | |
394 | } | |
395 | } | |
396 | } | |
397 | ||
398 | boolean t = (highlightFlags.indexOf('t') != -1) // Highlight types | |
399 | boolean f = (highlightFlags.indexOf('f') != -1) // Highlight fields | |
400 | boolean m = (highlightFlags.indexOf('m') != -1) // Highlight methods | |
401 | boolean c = (highlightFlags.indexOf('c') != -1) // Highlight constructors | |
402 | ||
403 | if (highlightFlags.indexOf('d') != -1) { | |
404 | // Highlight declarations | |
405 | for (def entry : declarations.entrySet()) { | |
406 | def declaration = entry.value | |
407 | ||
408 | if (matchScope(highlightScope, declaration.type)) { | |
409 | if ((t && declaration.isAType()) || (c && declaration.isAConstructor())) { | |
410 | matchAndAddDocumentRange(pattern, getMostInnerTypeName(declaration.type), declaration.startPosition, declaration.endPosition, ranges) | |
411 | } | |
412 | if ((f && declaration.isAField()) || (m && declaration.isAMethod())) { | |
413 | matchAndAddDocumentRange(pattern, declaration.name, declaration.startPosition, declaration.endPosition, ranges) | |
414 | } | |
415 | } | |
416 | } | |
417 | } | |
418 | ||
419 | if (highlightFlags.indexOf('r') != -1) { | |
420 | // Highlight references | |
421 | for (def entry : hyperlinks.entrySet()) { | |
422 | def hyperlink = entry.value | |
423 | def reference = ((HyperlinkReferenceData)hyperlink).reference | |
424 | ||
425 | if (matchScope(highlightScope, reference.owner)) { | |
426 | if ((t && reference.isAType()) || (c && reference.isAConstructor())) { | |
427 | matchAndAddDocumentRange(pattern, getMostInnerTypeName(reference.type), hyperlink.startPosition, hyperlink.endPosition, ranges) | |
428 | } | |
429 | if ((f && reference.isAField()) || (m && reference.isAMethod())) { | |
430 | matchAndAddDocumentRange(pattern, reference.name, hyperlink.startPosition, hyperlink.endPosition, ranges) | |
431 | } | |
432 | } | |
433 | } | |
434 | } | |
435 | } | |
436 | } | |
437 | ||
438 | @CompileStatic | |
439 | static boolean matchScope(String scope, String type) { | |
440 | if (!scope) | |
441 | return true | |
442 | if (scope.charAt(0) == '*') | |
443 | return type.endsWith(scope.substring(1)) || type.equals(scope.substring(2)) | |
444 | return type.equals(scope) | |
445 | } | |
446 | ||
447 | @CompileStatic | |
448 | static void matchAndAddDocumentRange(Pattern pattern, String text, int start, int end, List<DocumentRange> ranges) { | |
380 | 449 | if (pattern.matcher(text).matches()) { |
381 | 450 | ranges.add(new DocumentRange(start, end)) |
382 | 451 | } |
383 | 452 | } |
384 | 453 | |
385 | 454 | @CompileStatic |
386 | String getMostInnerTypeName(String typeName) { | |
455 | static String getMostInnerTypeName(String typeName) { | |
387 | 456 | int lastPackageSeparatorIndex = typeName.lastIndexOf('/') + 1 |
388 | 457 | int lastTypeNameSeparatorIndex = typeName.lastIndexOf('$') + 1 |
389 | 458 | int lastIndex = Math.max(lastPackageSeparatorIndex, lastTypeNameSeparatorIndex) |
73 | 73 | // Example: at java.security.AccessController.doPrivileged(Native Method) |
74 | 74 | lastDotIndex = internalTypeName.lastIndexOf('/') |
75 | 75 | def shortTypeName = internalTypeName.substring(lastDotIndex+1) |
76 | api.openURI(x, y, entries, null, shortTypeName + '-' + methodName + '-(?)?') | |
76 | api.openURI(x, y, entries, null, shortTypeName + '-' + methodName + '-(*)?') | |
77 | 77 | } else { |
78 | 78 | // Example: at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294) |
79 | 79 | int colonIndex = lineNumberOrNativeMethodFlag.indexOf(':') |
45 | 45 | def internalTypeName = typeName.replace('.', '/') |
46 | 46 | // Undefined parameters : 2 candidate methods |
47 | 47 | // http://docs.oracle.com/javase/6/docs/api/java/lang/instrument/package-summary.html |
48 | addHyperlink(new ManifestHyperlinkData(startIndex, endIndex, internalTypeName + '-premain-(?)?')) | |
48 | addHyperlink(new ManifestHyperlinkData(startIndex, endIndex, internalTypeName + '-premain-(*)?')) | |
49 | 49 | } |
50 | 50 | // Display |
51 | 51 | setText(text) |
0 | /* | |
1 | * Copyright (c) 2008-2015 Emmanuel Dupuy | |
2 | * This program is made available under the terms of the GPLv3 License. | |
3 | */ | |
4 | ||
5 | package jd.gui.util.matcher; | |
6 | ||
7 | /* | |
8 | * Descriptor format : @see jd.gui.api.feature.UriOpenable | |
9 | */ | |
10 | public class DescriptorMatcher { | |
11 | ||
12 | public static boolean matchFieldDescriptors(String d1, String d2) { | |
13 | return matchDescriptors(new CharBuffer(d1), new CharBuffer(d2)); | |
14 | } | |
15 | ||
16 | protected static boolean matchDescriptors(CharBuffer cb1, CharBuffer cb2) { | |
17 | if (cb1.read() == '?') { | |
18 | if (cb2.read() == '?') { | |
19 | return true; | |
20 | } else { | |
21 | cb2.unread(); | |
22 | return cb2.skipType(); | |
23 | } | |
24 | } else { | |
25 | cb1.unread(); | |
26 | ||
27 | if (cb2.read() == '?') { | |
28 | return cb1.skipType(); | |
29 | } else { | |
30 | cb2.unread(); | |
31 | return cb1.compareTypeWith(cb2); | |
32 | } | |
33 | } | |
34 | } | |
35 | ||
36 | public static boolean matchMethodDescriptors(String d1, String d2) { | |
37 | CharBuffer cb1 = new CharBuffer(d1); | |
38 | CharBuffer cb2 = new CharBuffer(d2); | |
39 | ||
40 | if ((cb1.read() != '(') || (cb2.read() != '(')) | |
41 | return false; | |
42 | ||
43 | if (cb1.read() == '*') { | |
44 | return true; | |
45 | } | |
46 | if (cb2.read() == '*') { | |
47 | return true; | |
48 | } | |
49 | ||
50 | cb1.unread(); | |
51 | cb2.unread(); | |
52 | ||
53 | // Check parameter descriptors | |
54 | while (cb2.get() != ')') { | |
55 | if (matchDescriptors(cb1, cb2) == false) | |
56 | return false; | |
57 | } | |
58 | ||
59 | if ((cb1.read() != ')') || (cb2.read() != ')')) | |
60 | return false; | |
61 | ||
62 | // Check return descriptor | |
63 | return matchDescriptors(cb1, cb2); | |
64 | } | |
65 | ||
66 | protected static class CharBuffer { | |
67 | protected char[] buffer; | |
68 | protected int length; | |
69 | protected int offset; | |
70 | ||
71 | public CharBuffer(String s) { | |
72 | this.buffer = s.toCharArray(); | |
73 | this.length = buffer.length; | |
74 | this.offset = 0; | |
75 | } | |
76 | ||
77 | public char read() { | |
78 | if (offset < length) | |
79 | return buffer[offset++]; | |
80 | else | |
81 | return (char)0; | |
82 | } | |
83 | ||
84 | public boolean unread() { | |
85 | if (offset > 0) { | |
86 | offset--; | |
87 | return true; | |
88 | } else { | |
89 | return false; | |
90 | } | |
91 | } | |
92 | ||
93 | public char get() { | |
94 | if (offset < length) | |
95 | return buffer[offset]; | |
96 | else | |
97 | return (char)0; | |
98 | } | |
99 | ||
100 | public boolean skipType() { | |
101 | if (offset < length) { | |
102 | char c = buffer[offset++]; | |
103 | ||
104 | while ((c == '[') && (offset < length)) { | |
105 | c = buffer[offset++]; | |
106 | } | |
107 | ||
108 | if (c == 'L') { | |
109 | while (offset < length) { | |
110 | if (buffer[offset++] == ';') | |
111 | return true; | |
112 | } | |
113 | } else if (c != '[') { | |
114 | return true; | |
115 | } | |
116 | } | |
117 | return false; | |
118 | } | |
119 | ||
120 | public boolean compareTypeWith(CharBuffer other) { | |
121 | if (offset >= length) | |
122 | return false; | |
123 | ||
124 | char c = buffer[offset++]; | |
125 | ||
126 | if (c != other.read()) | |
127 | return false; | |
128 | ||
129 | if (c == 'L') { | |
130 | if ((offset >= length) || (other.offset >= other.length)) | |
131 | return false; | |
132 | ||
133 | char[] otherBuffer = other.buffer; | |
134 | ||
135 | if ((buffer[offset] == '*') || (otherBuffer[other.offset] == '*')) { | |
136 | int start = offset; | |
137 | int otherStart = other.offset; | |
138 | ||
139 | // Search ';' | |
140 | if ((searchEndOfType() == false) || (other.searchEndOfType() == false)) | |
141 | return false; | |
142 | ||
143 | int current = offset - 1; | |
144 | int otherCurrent = other.offset - 1; | |
145 | ||
146 | // Backward comparison | |
147 | while ((start < current) && (otherStart < otherCurrent)) { | |
148 | c = buffer[--current]; | |
149 | if (c == '*') | |
150 | return true; | |
151 | ||
152 | char otherC = otherBuffer[--otherCurrent]; | |
153 | if (otherC == '*') | |
154 | return true; | |
155 | if (c != otherC) | |
156 | return false; | |
157 | } | |
158 | } else { | |
159 | // Forward comparison | |
160 | while (offset < length) { | |
161 | c = buffer[offset++]; | |
162 | if (c != other.read()) | |
163 | return false; | |
164 | if (c == ';') | |
165 | return true; | |
166 | } | |
167 | return false; | |
168 | } | |
169 | } | |
170 | ||
171 | return true; | |
172 | } | |
173 | ||
174 | protected boolean searchEndOfType() { | |
175 | while (offset < length) { | |
176 | if (buffer[offset++] == ';') | |
177 | return true; | |
178 | } | |
179 | return false; | |
180 | } | |
181 | ||
182 | public String toString() { | |
183 | return new String(buffer, offset, length-offset); | |
184 | } | |
185 | } | |
186 | } |
0 | /* | |
1 | * Copyright (c) 2008-2015 Emmanuel Dupuy | |
2 | * This program is made available under the terms of the GPLv3 License. | |
3 | */ | |
4 | ||
5 | package jd.gui.util.matcher | |
6 | ||
7 | class DescriptorMatcherTest extends GroovyTestCase { | |
8 | void testMatchFieldDescriptors() { | |
9 | assertTrue DescriptorMatcher.matchFieldDescriptors("?", "?") | |
10 | ||
11 | assertTrue DescriptorMatcher.matchFieldDescriptors("I", "I") | |
12 | assertTrue DescriptorMatcher.matchFieldDescriptors("?", "I") | |
13 | assertTrue DescriptorMatcher.matchFieldDescriptors("I", "?") | |
14 | ||
15 | assertTrue DescriptorMatcher.matchFieldDescriptors("Ltest/Test;", "Ltest/Test;") | |
16 | assertTrue DescriptorMatcher.matchFieldDescriptors("?", "Ltest/Test;") | |
17 | assertTrue DescriptorMatcher.matchFieldDescriptors("Ltest/Test;", "?") | |
18 | assertTrue DescriptorMatcher.matchFieldDescriptors("L*/Test;", "Ltest/Test;") | |
19 | assertTrue DescriptorMatcher.matchFieldDescriptors("Ltest/Test;", "L*/Test;") | |
20 | ||
21 | assertTrue DescriptorMatcher.matchFieldDescriptors("L*/Test;", "L*/Test;") | |
22 | assertTrue DescriptorMatcher.matchFieldDescriptors("?", "L*/Test;") | |
23 | assertTrue DescriptorMatcher.matchFieldDescriptors("L*/Test;", "?") | |
24 | ||
25 | assertTrue DescriptorMatcher.matchFieldDescriptors("[Z", "[Z") | |
26 | assertTrue DescriptorMatcher.matchFieldDescriptors("[Z", "?") | |
27 | assertTrue DescriptorMatcher.matchFieldDescriptors("?", "[Z") | |
28 | ||
29 | assertTrue DescriptorMatcher.matchFieldDescriptors("Ltest/Test;", "Ltest/Test;") | |
30 | assertTrue DescriptorMatcher.matchFieldDescriptors("Ltest/Test;", "?") | |
31 | assertTrue DescriptorMatcher.matchFieldDescriptors("?", "Ltest/Test;") | |
32 | ||
33 | assertTrue DescriptorMatcher.matchFieldDescriptors("[[[Ltest/Test;", "[[[Ltest/Test;") | |
34 | assertTrue DescriptorMatcher.matchFieldDescriptors("[[[Ltest/Test;", "?") | |
35 | assertTrue DescriptorMatcher.matchFieldDescriptors("?", "[[[Ltest/Test;") | |
36 | ||
37 | assertTrue DescriptorMatcher.matchFieldDescriptors("[[[L*/Test;", "[[[L*/Test;") | |
38 | assertTrue DescriptorMatcher.matchFieldDescriptors("[[[L*/Test;", "?") | |
39 | assertTrue DescriptorMatcher.matchFieldDescriptors("?", "[[[L*/Test;") | |
40 | } | |
41 | ||
42 | void testMatchMethodDescriptors() { | |
43 | assertFalse DescriptorMatcher.matchMethodDescriptors("I", "I") | |
44 | ||
45 | assertTrue DescriptorMatcher.matchMethodDescriptors("()I", "()I") | |
46 | assertTrue DescriptorMatcher.matchMethodDescriptors("(*)?", "()I") | |
47 | assertTrue DescriptorMatcher.matchMethodDescriptors("()I", "(*)?") | |
48 | ||
49 | assertTrue DescriptorMatcher.matchMethodDescriptors("(I)I", "(I)I") | |
50 | assertTrue DescriptorMatcher.matchMethodDescriptors("(*)?", "(I)I") | |
51 | assertTrue DescriptorMatcher.matchMethodDescriptors("(I)I", "(*)?") | |
52 | ||
53 | assertTrue DescriptorMatcher.matchMethodDescriptors("(IJ)I", "(IJ)I") | |
54 | assertTrue DescriptorMatcher.matchMethodDescriptors("(*)?", "(IJ)I") | |
55 | assertTrue DescriptorMatcher.matchMethodDescriptors("(IJ)I", "(*)?") | |
56 | ||
57 | assertTrue DescriptorMatcher.matchMethodDescriptors("(Ltest/Test;)Ltest/Test;", "(Ltest/Test;)Ltest/Test;") | |
58 | assertTrue DescriptorMatcher.matchMethodDescriptors("(*)?", "(Ltest/Test;)Ltest/Test;") | |
59 | assertTrue DescriptorMatcher.matchMethodDescriptors("(Ltest/Test;)Ltest/Test;", "(*)?") | |
60 | assertTrue DescriptorMatcher.matchMethodDescriptors("([[Ltest/Test;[[Ltest/Test;)Ltest/Test;", "([[L*/Test;[[L*/Test;)L*/Test;") | |
61 | assertTrue DescriptorMatcher.matchMethodDescriptors("([[L*/Test;[[L*/Test;)L*/Test;", "([[Ltest/Test;[[Ltest/Test;)Ltest/Test;") | |
62 | ||
63 | assertTrue DescriptorMatcher.matchMethodDescriptors("(Ltest/Test;Ltest/Test;)Ltest/Test;", "(Ltest/Test;Ltest/Test;)Ltest/Test;") | |
64 | assertTrue DescriptorMatcher.matchMethodDescriptors("(*)?", "(Ltest/Test;Ltest/Test;)Ltest/Test;") | |
65 | assertTrue DescriptorMatcher.matchMethodDescriptors("(Ltest/Test;Ltest/Test;)Ltest/Test;", "(*)?") | |
66 | ||
67 | assertTrue DescriptorMatcher.matchMethodDescriptors("([[Ltest/Test;[[Ltest/Test;)Ltest/Test;", "([[Ltest/Test;[[Ltest/Test;)Ltest/Test;") | |
68 | assertTrue DescriptorMatcher.matchMethodDescriptors("(*)?", "([[Ltest/Test;[[Ltest/Test;)Ltest/Test;") | |
69 | assertTrue DescriptorMatcher.matchMethodDescriptors("([[Ltest/Test;[[Ltest/Test;)Ltest/Test;", "(*)?") | |
70 | assertTrue DescriptorMatcher.matchMethodDescriptors("([[L*/Test;[[L*/Test;)L*/Test;", "([[Ltest/Test;[[Ltest/Test;)Ltest/Test;") | |
71 | assertTrue DescriptorMatcher.matchMethodDescriptors("([[Ltest/Test;[[Ltest/Test;)Ltest/Test;", "([[L*/Test;[[L*/Test;)L*/Test;") | |
72 | ||
73 | assertTrue DescriptorMatcher.matchMethodDescriptors("(L*/Test;)L*/Test;", "(L*/Test;)L*/Test;") | |
74 | assertTrue DescriptorMatcher.matchMethodDescriptors("(*)?", "(L*/Test;)L*/Test;") | |
75 | assertTrue DescriptorMatcher.matchMethodDescriptors("(L*/Test;)L*/Test;", "(*)?") | |
76 | ||
77 | assertTrue DescriptorMatcher.matchMethodDescriptors("(L*/Test;L*/Test;)L*/Test;", "(L*/Test;L*/Test;)L*/Test;") | |
78 | assertTrue DescriptorMatcher.matchMethodDescriptors("(*)?", "(L*/Test;L*/Test;)L*/Test;") | |
79 | assertTrue DescriptorMatcher.matchMethodDescriptors("(Ltest/Test;Ltest/Test;)Ltest/Test;", "(*)?") | |
80 | ||
81 | assertTrue DescriptorMatcher.matchMethodDescriptors("([[L*/Test;[[L*/Test;)L*/Test;", "([[L*/Test;[[L*/Test;)L*/Test;") | |
82 | assertTrue DescriptorMatcher.matchMethodDescriptors("(*)?", "([[L*/Test;[[L*/Test;)L*/Test;") | |
83 | assertTrue DescriptorMatcher.matchMethodDescriptors("([[L*/Test;[[L*/Test;)L*/Test;", "(*)?") | |
84 | } | |
85 | } |
0 | /* | |
1 | * Copyright (c) 2008-2015 Emmanuel Dupuy | |
2 | * This program is made available under the terms of the GPLv3 License. | |
3 | */ | |
4 | ||
5 | package jd.gui.view.component | |
6 | ||
7 | class ClassFilePageTest extends GroovyTestCase { | |
8 | ||
9 | HashMap<String, ClassFilePage.DeclarationData> initDeclarations() { | |
10 | def data = new ClassFilePage.DeclarationData(0, 1, "Test", "test", "I") | |
11 | HashMap<String, ClassFilePage.DeclarationData> declarations = [:] | |
12 | ||
13 | // Init type declarations | |
14 | declarations.put("Test", data) | |
15 | declarations.put("test/Test", data) | |
16 | ||
17 | // Init field declarations | |
18 | declarations.put("Test-attributeInt-I", data) | |
19 | declarations.put("Test-attributeBoolean-Z", data) | |
20 | declarations.put("Test-attributeArrayBoolean-[[Z", data) | |
21 | declarations.put("Test-attributeString-Ljava/lang/String;", data) | |
22 | ||
23 | declarations.put("test/Test-attributeInt-I", data) | |
24 | declarations.put("test/Test-attributeBoolean-Z", data) | |
25 | declarations.put("test/Test-attributeArrayBoolean-[[Z", data) | |
26 | declarations.put("test/Test-attributeString-Ljava/lang/String;", data) | |
27 | ||
28 | // Init method declarations | |
29 | declarations.put("Test-getInt-()I", data) | |
30 | declarations.put("Test-getString-()Ljava/lang/String;", data) | |
31 | declarations.put("Test-add-(JJ)J", data) | |
32 | declarations.put("Test-createBuffer-(I)[C", data) | |
33 | ||
34 | declarations.put("test/Test-getInt-()I", data) | |
35 | declarations.put("test/Test-getString-()Ljava/lang/String;", data) | |
36 | declarations.put("test/Test-add-(JJ)J", data) | |
37 | declarations.put("test/Test-createBuffer-(I)[C", data) | |
38 | ||
39 | return declarations | |
40 | } | |
41 | ||
42 | TreeMap<Integer, HyperlinkPage.HyperlinkData> initHyperlinks() { | |
43 | def hyperlinks = new TreeMap<Integer, HyperlinkPage.HyperlinkData>() | |
44 | ||
45 | hyperlinks.put(0, new ClassFilePage.HyperlinkReferenceData(0, 1, new ClassFilePage.ReferenceData("java/lang/Integer", "MAX_VALUE", "I", "Test"))) | |
46 | hyperlinks.put(0, new ClassFilePage.HyperlinkReferenceData(0, 1, new ClassFilePage.ReferenceData("java/lang/Integer", "toString", "()Ljava/lang/String;", "Test"))) | |
47 | ||
48 | return hyperlinks | |
49 | } | |
50 | ||
51 | ArrayList<ClassFilePage.StringData> initStrings() { | |
52 | def strings = new ArrayList<ClassFilePage.StringData>() | |
53 | ||
54 | strings.add(new ClassFilePage.StringData(0, 3, "abc", "Test")) | |
55 | ||
56 | return strings | |
57 | } | |
58 | ||
59 | void testMatchFragmentAndAddDocumentRange() { | |
60 | def declarations = initDeclarations() | |
61 | def ranges = [] | |
62 | ||
63 | ranges.clear() | |
64 | ClassFilePage.matchFragmentAndAddDocumentRange("Test-attributeBoolean-Z", declarations, ranges) | |
65 | assertTrue ranges.size() == 1 | |
66 | ||
67 | ranges.clear() | |
68 | ClassFilePage.matchFragmentAndAddDocumentRange("test/Test-attributeBoolean-Z", declarations, ranges) | |
69 | assertTrue ranges.size() == 1 | |
70 | ||
71 | ranges.clear() | |
72 | ClassFilePage.matchFragmentAndAddDocumentRange("*/Test-attributeBoolean-Z", declarations, ranges) | |
73 | assertTrue ranges.size() == 2 | |
74 | ||
75 | ranges.clear() | |
76 | ClassFilePage.matchFragmentAndAddDocumentRange("Test-createBuffer-(I)[C", declarations, ranges) | |
77 | assertTrue ranges.size() == 1 | |
78 | ||
79 | ranges.clear() | |
80 | ClassFilePage.matchFragmentAndAddDocumentRange("test/Test-createBuffer-(I)[C", declarations, ranges) | |
81 | assertTrue ranges.size() == 1 | |
82 | ||
83 | ranges.clear() | |
84 | ClassFilePage.matchFragmentAndAddDocumentRange("*/Test-getString-(*)?", declarations, ranges) | |
85 | assertTrue ranges.size() == 2 | |
86 | ||
87 | ranges.clear() | |
88 | ClassFilePage.matchFragmentAndAddDocumentRange("test/Test-add-(?J)?", declarations, ranges) | |
89 | assertTrue ranges.size() == 1 | |
90 | } | |
91 | ||
92 | void testMatchQueryAndAddDocumentRange() { | |
93 | def declarations = initDeclarations() | |
94 | def hyperlinks = initHyperlinks() | |
95 | def strings = initStrings() | |
96 | def ranges = [] | |
97 | ||
98 | ranges.clear() | |
99 | ClassFilePage.matchQueryAndAddDocumentRange([highlightPattern:"ab", highlightFlags:"s", highlightScope:null], declarations, hyperlinks, strings, ranges) | |
100 | assertTrue ranges.size() == 1 | |
101 | ||
102 | ranges.clear() | |
103 | ClassFilePage.matchQueryAndAddDocumentRange([highlightPattern:"ab", highlightFlags:"s", highlightScope:""], declarations, hyperlinks, strings, ranges) | |
104 | assertTrue ranges.size() == 1 | |
105 | ||
106 | ranges.clear() | |
107 | ClassFilePage.matchQueryAndAddDocumentRange([highlightPattern:"ab", highlightFlags:"s", highlightScope:"Test"], declarations, hyperlinks, strings, ranges) | |
108 | assertTrue ranges.size() == 1 | |
109 | } | |
110 | ||
111 | void testMatchScope() { | |
112 | assertTrue ClassFilePage.matchScope(null, "java/lang/String") | |
113 | assertTrue ClassFilePage.matchScope("", "java/lang/String") | |
114 | ||
115 | assertTrue ClassFilePage.matchScope("java/lang/String", "java/lang/String") | |
116 | assertTrue ClassFilePage.matchScope("*/lang/String", "java/lang/String") | |
117 | assertTrue ClassFilePage.matchScope("*/String", "java/lang/String") | |
118 | ||
119 | assertTrue ClassFilePage.matchScope(null, "Test") | |
120 | assertTrue ClassFilePage.matchScope("", "Test") | |
121 | ||
122 | assertTrue ClassFilePage.matchScope("Test", "Test") | |
123 | assertTrue ClassFilePage.matchScope("*/Test", "Test") | |
124 | } | |
125 | } |