Skip to main content
summaryrefslogtreecommitdiffstats
blob: e4fd888739db51197ebf579453a7686f43f33653 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*******************************************************************************
 * Copyright (c) 2013 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.e4.tools.event.spy.internal.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.ui.JavaUI;
import org.osgi.framework.FrameworkUtil;

public class JDTUtils {
	private final static Pattern CLASS_NAME_PATTERN = Pattern.compile("(([a-zA-Z_]+[0-9]*\\.)+[a-zA-Z_]+[a-z0-9]*)");

	public static boolean containsClassName(String name) {
		return CLASS_NAME_PATTERN.matcher(name).find();
	}

	public static void openClass(String clsName) throws ClassNotFoundException {
		Matcher matcher = CLASS_NAME_PATTERN.matcher(clsName);
		if (!matcher.find()) {
			return;
		}
		try {
			Class<?> cls = FrameworkUtil.getBundle(JDTUtils.class).loadClass(matcher.group(1).trim());
			IProject project = findProjectFor(cls);

			if (project != null) {
				openInEditor(JavaCore.create(project), cls.getName());
			}
		} catch (ClassNotFoundException exc) {
			throw new ClassNotFoundException("Class not found in the bundle classpath: " + clsName);
		}
	}

	private static IProject findProjectFor(Class<?> cls) {
		for (IProject project : ResourcesPlugin.getWorkspace().getRoot().getProjects()) {
			if (project.getFile(cls.getName()) != null) {
				return project;
			}
		}
		return null;
	}

	private static void openInEditor(IJavaProject project, String clazz) throws ClassNotFoundException {
		if (project == null) {
			return;
		}
		try {
			IType type = project.findType(clazz);
			JavaUI.openInEditor(type, false, true);
		} catch (Exception e) {
			throw new ClassNotFoundException(e.getMessage());
		}
	}
}

Back to the top