-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathJavaProjectConfig.cs
More file actions
276 lines (230 loc) · 13 KB
/
Copy pathJavaProjectConfig.cs
File metadata and controls
276 lines (230 loc) · 13 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
namespace Tvl.VisualStudio.Language.Java.Project
{
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Project;
using Tvl.VisualStudio.Shell;
using __VSDBGLAUNCHFLAGS = Microsoft.VisualStudio.Shell.Interop.__VSDBGLAUNCHFLAGS;
using __VSDBGLAUNCHFLAGS2 = Microsoft.VisualStudio.Shell.Interop.__VSDBGLAUNCHFLAGS2;
using _PersistStorageType = Microsoft.VisualStudio.Shell.Interop._PersistStorageType;
using CommandLineBuilder = Microsoft.Build.Utilities.CommandLineBuilder;
using DEBUG_LAUNCH_OPERATION = Microsoft.VisualStudio.Shell.Interop.DEBUG_LAUNCH_OPERATION;
using DebugAgent = Tvl.VisualStudio.Language.Java.Project.PropertyPages.DebugAgent;
using Directory = System.IO.Directory;
using File = System.IO.File;
using IVsDebugger2 = Microsoft.VisualStudio.Shell.Interop.IVsDebugger2;
using IVsUIShell = Microsoft.VisualStudio.Shell.Interop.IVsUIShell;
using JavaDebugEngine = Tvl.VisualStudio.Language.Java.Debugger.JavaDebugEngine;
using Path = System.IO.Path;
using RegistryHive = Microsoft.Win32.RegistryHive;
using RegistryKey = Microsoft.Win32.RegistryKey;
using RegistryKeyPermissionCheck = Microsoft.Win32.RegistryKeyPermissionCheck;
using RegistryView = Microsoft.Win32.RegistryView;
using SecurityException = System.Security.SecurityException;
using StringComparison = System.StringComparison;
using SVsShellDebugger = Microsoft.VisualStudio.Shell.Interop.SVsShellDebugger;
using SVsUIShell = Microsoft.VisualStudio.Shell.Interop.SVsUIShell;
public class JavaProjectConfig : ProjectConfig
{
internal JavaProjectConfig(JavaProjectNode project, string configuration, string platform)
: base(project, configuration, platform)
{
Contract.Requires(project != null);
Contract.Requires(!string.IsNullOrEmpty(configuration));
Contract.Requires(!string.IsNullOrEmpty(platform));
}
public new JavaProjectNode ProjectManager
{
get
{
Contract.Ensures(Contract.Result<JavaProjectNode>() != null);
return (JavaProjectNode)base.ProjectManager;
}
}
public string FindJavaBinary(string fileName, bool developmentKit)
{
string vendorBase = GetConfigurationProperty("JvmRegistryBase", _PersistStorageType.PST_PROJECT_FILE, false);
string installation = developmentKit ? "Java Development Kit" : "Java Runtime Environment";
bool allow64bit = Platform.EndsWith("X64", StringComparison.OrdinalIgnoreCase) || Platform.EndsWith("Any CPU", StringComparison.OrdinalIgnoreCase);
bool allow32bit = Platform.EndsWith("X86", StringComparison.OrdinalIgnoreCase) || Platform.EndsWith("Any CPU", StringComparison.OrdinalIgnoreCase);
string javaBinary;
if (allow64bit && TryFindJavaPath(vendorBase, installation, fileName, RegistryView.Registry64, out javaBinary))
{
return javaBinary;
}
if (allow32bit && TryFindJavaPath(vendorBase, installation, fileName, RegistryView.Registry32, out javaBinary))
{
return javaBinary;
}
return null;
}
private static bool TryFindJavaPath(string vendor, string installation, string fileName, RegistryView registryView, out string javaBinary)
{
javaBinary = null;
try
{
string javaHome;
if (TryGetJavaHome(vendor, installation, registryView, out javaHome))
{
string binary = Path.Combine(javaHome, "bin", fileName);
if (!File.Exists(binary))
return false;
javaBinary = binary;
return true;
}
return false;
}
catch (SecurityException)
{
return false;
}
}
private static bool TryGetJavaHome(string vendor, string installation, RegistryView registryView, out string javaHome)
{
Contract.Requires<ArgumentNullException>(vendor != null, "vendor");
Contract.Requires<ArgumentNullException>(installation != null, "installation");
Contract.Requires<ArgumentException>(!string.IsNullOrEmpty(vendor));
Contract.Requires<ArgumentException>(!string.IsNullOrEmpty(installation));
javaHome = null;
if (registryView == RegistryView.Registry64 && !Environment.Is64BitOperatingSystem)
{
// without this check, Registry64 defaults to returning values from the 32-bit registry.
return false;
}
string javaKeyName = "SOFTWARE\\" + vendor + "\\" + installation;
using (var baseKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, registryView))
{
using (RegistryKey javaKey = baseKey.OpenSubKey(javaKeyName, RegistryKeyPermissionCheck.ReadSubTree))
{
if (javaKey == null)
return false;
object currentVersion = javaKey.GetValue("CurrentVersion");
if (currentVersion == null)
return false;
using (var homeKey = javaKey.OpenSubKey(currentVersion.ToString()))
{
if (homeKey == null || homeKey.GetValue("JavaHome") == null)
return false;
javaHome = homeKey.GetValue("JavaHome").ToString();
return !string.IsNullOrEmpty(javaHome);
}
}
}
}
public override void Invalidate()
{
base.Invalidate();
}
public override int QueryDebugLaunch(uint flags, out int fCanLaunch)
{
fCanLaunch = 1;
return VSConstants.S_OK;
}
public override int DebugLaunch(uint grfLaunch)
{
DebugTargetInfo info = new DebugTargetInfo();
CommandLineBuilder commandLine = new CommandLineBuilder();
bool x64 = Platform.EndsWith("X64", StringComparison.OrdinalIgnoreCase) || (Platform.EndsWith("Any CPU", StringComparison.OrdinalIgnoreCase) && Environment.Is64BitOperatingSystem);
string agentBaseFileName = "Tvl.Java.DebugHostWrapper";
if (x64)
agentBaseFileName += "X64";
bool useDevelopmentEnvironment = (grfLaunch & (uint)__VSDBGLAUNCHFLAGS.DBGLAUNCH_NoDebug) == 0;
string debugAgentName = GetConfigurationProperty(JavaConfigConstants.DebugAgent, _PersistStorageType.PST_USER_FILE, false);
bool useJdwp = string.Equals(DebugAgent.Jdwp.ToString(), debugAgentName, StringComparison.OrdinalIgnoreCase);
if (useJdwp)
{
commandLine.AppendSwitch("-Xdebug");
string serverValue = useDevelopmentEnvironment ? "y" : "n";
commandLine.AppendSwitch("-Xrunjdwp:transport=dt_socket,server=" + serverValue + ",address=6777");
}
else
{
string agentFolder = Path.GetDirectoryName(typeof(JavaProjectConfig).Assembly.Location);
string agentFileName = agentBaseFileName + ".dll";
string agentPath = Path.GetFullPath(Path.Combine(agentFolder, agentFileName));
commandLine.AppendSwitchIfNotNull("-agentpath:", agentPath);
string agentArguments = GetConfigurationProperty(JavaConfigConstants.DebugAgentArguments, _PersistStorageType.PST_USER_FILE, false);
if (!string.IsNullOrEmpty(agentArguments))
commandLine.AppendTextUnquoted("=" + agentArguments);
}
switch (GetConfigurationProperty(JavaConfigConstants.DebugStartAction, _PersistStorageType.PST_USER_FILE, false))
{
case "Class":
string jvmArguments = GetConfigurationProperty(JavaConfigConstants.DebugJvmArguments, _PersistStorageType.PST_USER_FILE, false);
if (!string.IsNullOrEmpty(jvmArguments))
commandLine.AppendTextUnquoted(" " + jvmArguments);
commandLine.AppendSwitch("-cp");
commandLine.AppendFileNameIfNotNull(GetConfigurationProperty("TargetPath", _PersistStorageType.PST_PROJECT_FILE, false));
string startupObject = GetConfigurationProperty(JavaConfigConstants.DebugStartClass, _PersistStorageType.PST_USER_FILE, false);
if (!string.IsNullOrEmpty(startupObject))
commandLine.AppendFileNameIfNotNull(startupObject);
break;
default:
throw new NotImplementedException("This preview version of the Java debugger only supports starting execution in a named class; the class name may be configured in the project properties on the Debug tab.");
}
string debugArgs = GetConfigurationProperty(JavaConfigConstants.DebugExtraArgs, _PersistStorageType.PST_USER_FILE, false);
if (!string.IsNullOrEmpty(debugArgs))
commandLine.AppendTextUnquoted(" " + debugArgs);
string workingDirectory = GetConfigurationProperty(JavaConfigConstants.DebugWorkingDirectory, _PersistStorageType.PST_USER_FILE, false);
if (string.IsNullOrEmpty(workingDirectory))
workingDirectory = GetConfigurationProperty(JavaConfigConstants.OutputPath, _PersistStorageType.PST_PROJECT_FILE, false);
if (!Path.IsPathRooted(workingDirectory))
{
workingDirectory = Path.GetFullPath(Path.Combine(this.ProjectManager.ProjectFolder, workingDirectory));
}
// Pass the project references via the CLASSPATH environment variable
List<string> classPathEntries = new List<string>();
IReferenceContainer referenceContainer = ProjectManager.GetReferenceContainer();
IList<ReferenceNode> references = referenceContainer.EnumReferences();
foreach (var referenceNode in references)
{
JarReferenceNode jarReferenceNode = referenceNode as JarReferenceNode;
if (jarReferenceNode != null)
{
if (File.Exists(jarReferenceNode.InstalledFilePath) || Directory.Exists(jarReferenceNode.InstalledFilePath))
classPathEntries.Add(jarReferenceNode.InstalledFilePath);
}
}
if (classPathEntries != null)
{
string classPath = string.Join(";", classPathEntries);
info.Environment.Add("CLASSPATH", classPath);
}
//List<string> arguments = new List<string>();
//arguments.Add(@"-agentpath:C:\dev\SimpleC\Tvl.Java.DebugHost\bin\Debug\Tvl.Java.DebugHostWrapper.dll");
////arguments.Add(@"-verbose:jni");
////arguments.Add(@"-cp");
////arguments.Add(@"C:\dev\JavaProjectTest\JavaProject\out\Debug");
//arguments.Add("tvl.school.ee382v.a3.problem1.program1");
////arguments.Add(GetConfigurationProperty("OutputPath", true));
////arguments.Add(GetConfigurationProperty(JavaConfigConstants.DebugStartClass, false, _PersistStorageType.PST_USER_FILE));
////arguments.Add(GetConfigurationProperty(JavaConfigConstants.DebugExtraArgs, false, _PersistStorageType.PST_USER_FILE));
//info.Arguments = string.Join(" ", arguments);
info.Arguments = commandLine.ToString();
info.Executable = FindJavaBinary("java.exe", useDevelopmentEnvironment);
//info.CurrentDirectory = GetConfigurationProperty("WorkingDirectory", false, _PersistStorageType.PST_USER_FILE);
info.CurrentDirectory = workingDirectory;
info.SendToOutputWindow = false;
info.DebugEngines = new Guid[]
{
typeof(JavaDebugEngine).GUID,
//VSConstants.DebugEnginesGuids.ManagedOnly_guid,
//VSConstants.DebugEnginesGuids.NativeOnly_guid,
};
Guid localPortSupplier = new Guid("{708C1ECA-FF48-11D2-904F-00C04FA302A1}");
info.PortSupplier = localPortSupplier;
info.LaunchOperation = DEBUG_LAUNCH_OPERATION.DLO_CreateProcess;
info.LaunchFlags = (__VSDBGLAUNCHFLAGS)grfLaunch | (__VSDBGLAUNCHFLAGS)__VSDBGLAUNCHFLAGS2.DBGLAUNCH_MergeEnv;
var debugger = (IVsDebugger2)Microsoft.VisualStudio.Shell.Package.GetGlobalService(typeof(SVsShellDebugger));
int result = debugger.LaunchDebugTargets(info);
if (result != VSConstants.S_OK)
{
IVsUIShell uishell = (IVsUIShell)Microsoft.VisualStudio.Shell.Package.GetGlobalService(typeof(SVsUIShell));
string message = uishell.GetErrorInfo();
}
return result;
}
}
}