ludc
2025-01-16 68fd566d21b3efc3a670a5295289b1801f5a4155
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package qq.builder;
 
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
 
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchConfigurationType;
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
import org.eclipse.debug.core.ILaunchManager;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IMember;
import org.eclipse.jdt.core.IMethod;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.search.IJavaSearchScope;
import org.eclipse.jdt.core.search.SearchEngine;
import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin;
import org.eclipse.jdt.internal.debug.ui.launcher.LauncherMessages;
import org.eclipse.jdt.internal.debug.ui.launcher.MainMethodSearchEngine;
import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.operation.IRunnableContext;
 
public class JavaApplicationLaunchClientShortcut extends
        JavaClientLaunchShortcut {
    protected IJavaElement[] getJavaElements(Object[] objects) {
        List list = new ArrayList(objects.length);
        for (int i = 0; i < objects.length; ++i) {
            Object object = objects[i];
            if (object instanceof IAdaptable) {
                IJavaElement element = (IJavaElement) ((IAdaptable) object)
                        .getAdapter(IJavaElement.class);
                if (element != null) {
                    if (element instanceof IMember) {
                        IJavaElement type = ((IMember) element)
                                .getDeclaringType();
                        if (type != null) {
                            element = type;
                        }
                    }
                    list.add(element);
                }
            }
        }
        return ((IJavaElement[]) list.toArray(new IJavaElement[list.size()]));
    }
 
    protected ILaunchConfiguration createConfiguration(IType type) {
        ILaunchConfiguration config = null;
        ILaunchConfigurationWorkingCopy wc = null;
        try {
            ILaunchConfigurationType configType = getConfigurationType();
            wc = configType.newInstance(null, getLaunchManager()
                    .generateLaunchConfigurationName("LogonApplication"));
            wc.setAttribute(
                    IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME,
                    "com.vci.rmip.logon.client.LogonApplication");
            wc.setAttribute(
                    IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME,
                    getJavaProject());
            wc.setMappedResources(new IResource[] { null });
            config = wc.doSave();
        } catch (CoreException exception) {
            MessageDialog.openError(JDIDebugUIPlugin.getActiveWorkbenchShell(),
                    LauncherMessages.JavaLaunchShortcut_3, exception
                            .getStatus().getMessage());
        }
        return config;
    }
 
    protected ILaunchConfigurationType getConfigurationType() {
        return getLaunchManager().getLaunchConfigurationType(
                IJavaLaunchConfigurationConstants.ID_JAVA_APPLICATION);
    }
 
    private ILaunchManager getLaunchManager() {
        return DebugPlugin.getDefault().getLaunchManager();
    }
 
    protected IType[] findTypes(Object[] elements, IRunnableContext context)
            throws InterruptedException, CoreException {
        try {
            if (elements.length == 1) {
                IType type = isMainMethod(elements[0]);
                if (type != null) {
                    return new IType[] { type };
                }
            }
            IJavaElement[] javaElements = getJavaElements(elements);
            MainMethodSearchEngine engine = new MainMethodSearchEngine();
            int constraints = 1;
            constraints |= 2;
            IJavaSearchScope scope = SearchEngine.createJavaSearchScope(
                    javaElements, constraints);
            return engine.searchMainMethods(context, scope, true);
        } catch (InvocationTargetException e) {
            throw ((CoreException) e.getTargetException());
        }
    }
 
    private IType isMainMethod(Object o) {
        if (o instanceof IAdaptable) {
            IAdaptable adapt = (IAdaptable) o;
            IJavaElement element = (IJavaElement) adapt
                    .getAdapter(IJavaElement.class);
            if ((element != null) && (element.getElementType() == 9))
                try {
                    IMethod method = (IMethod) element;
                    if (!(method.isMainMethod())) {
                        return method.getDeclaringType();
 
                    }
                } catch (JavaModelException jme) {
                    JDIDebugUIPlugin.log(jme);
                }
        }
        return null;
    }
 
    protected String getTypeSelectionTitle() {
        return LauncherMessages.JavaApplicationLaunchShortcut_0;
    }
 
    protected String getEditorEmptyMessage() {
        return LauncherMessages.JavaApplicationLaunchShortcut_1;
    }
 
    protected String getSelectionEmptyMessage() {
        return LauncherMessages.JavaApplicationLaunchShortcut_2;
    }
}