|
| 1 | +# Java.Interop Copilot Instructions |
| 2 | + |
| 3 | +## Project Overview |
| 4 | + |
| 5 | +**Java.Interop** is a .NET library that provides Java Native Interface (JNI) bindings for managed languages such as C#. It enables bidirectional interoperability between .NET's Common Language Runtime (CLR) and Java Virtual Machines (JVMs), allowing .NET code to invoke Java methods and Java code to call back into managed code. |
| 6 | + |
| 7 | +**Primary Use Cases**: |
| 8 | +- .NET for Android development (successor to Xamarin.Android) |
| 9 | +- Desktop Java interop scenarios |
| 10 | +- Binding Java libraries for .NET consumption |
| 11 | +- Cross-platform Java integration |
| 12 | + |
| 13 | +## Architecture & Core Concepts |
| 14 | + |
| 15 | +### JNI (Java Native Interface) |
| 16 | +- Industry-standard interface for Java-native code interaction |
| 17 | +- Provides type-safe bindings using structs like `JniObjectReference` instead of raw `IntPtr` |
| 18 | +- Supports both SafeHandle-based (safer) and IntPtr-based (faster) implementations |
| 19 | +- Reference types: Local, Global, and WeakGlobal references with proper lifecycle management |
| 20 | + |
| 21 | +### Type System & Marshaling |
| 22 | +- **JavaObject**: Base class for managed wrappers of Java objects |
| 23 | +- **JniPeerMembers**: Caches method and field IDs for efficient access |
| 24 | +- **Value Marshaling**: Converts between Java and .NET types (e.g., `java.lang.String` ↔ `System.String`) |
| 25 | +- **Exception Marshaling**: Translates Java exceptions to .NET exceptions |
| 26 | + |
| 27 | +### Code Generation Pipeline |
| 28 | +1. **API Description**: XML files describing Java APIs |
| 29 | +2. **Generator Tool**: Converts API descriptions to C# binding code |
| 30 | +3. **Java Callable Wrappers (JCWs)**: Java stubs for calling managed methods |
| 31 | +4. **Marshal Methods**: Runtime-generated or pre-compiled bridging code |
| 32 | + |
| 33 | +## Repository Structure |
| 34 | + |
| 35 | +### Core Libraries (`src/`) |
| 36 | +- **`Java.Interop/`**: Main JNI binding library with core types and runtime |
| 37 | +- **`Java.Interop.Dynamic/`**: C# 4.0 `dynamic` provider for runtime method invocation |
| 38 | +- **`Java.Interop.Export/`**: `[Export]` attribute support for exposing managed methods to Java |
| 39 | +- **`Java.Runtime.Environment/`**: JVM loading and lifecycle management |
| 40 | +- **`Java.Base/`**: Bindings for core Java types (`java.lang.*`, etc.) |
| 41 | + |
| 42 | +### Code Generation Tools (`tools/`) |
| 43 | +- **`generator/`**: Primary tool for generating C# bindings from Java API descriptions |
| 44 | +- **`class-parse/`**: Parses Java `.class` files and generates API descriptions |
| 45 | +- **`java-source-utils/`**: Utilities for processing Java source code |
| 46 | +- **`jcw-gen/`**: Generates Java Callable Wrapper classes |
| 47 | +- **`param-name-importer/`**: Imports parameter names from Java source |
| 48 | + |
| 49 | +### Supporting Libraries |
| 50 | +- **`Java.Interop.Tools.JavaSource/`**: Javadoc parsing and XML documentation conversion |
| 51 | +- **`Java.Interop.Tools.Maven/`**: Maven project integration and dependency resolution |
| 52 | +- **`Xamarin.Android.Tools.Bytecode/`**: Java bytecode analysis and processing |
| 53 | +- **`Xamarin.SourceWriter/`**: Code generation utilities |
| 54 | + |
| 55 | +### Testing (`tests/`) |
| 56 | +- Unit tests for all major components |
| 57 | +- Performance benchmarks (`Java.Interop-PerformanceTests/`) |
| 58 | +- Integration tests with real JVM instances |
| 59 | +- Generator tests with sample API descriptions |
| 60 | + |
| 61 | +### Samples (`samples/`) |
| 62 | +- **`Hello-Core/`**: Minimal JNI usage without object mapping |
| 63 | +- **`Hello-Java.Base/`**: Using core Java type bindings |
| 64 | +- **`Hello-NativeAOT*/`**: Ahead-of-time compilation scenarios |
| 65 | + |
| 66 | +## Development Patterns & Conventions |
| 67 | + |
| 68 | +### Code Formatting |
| 69 | + |
| 70 | +C# code uses tabs (not spaces) and the Mono code-formatting style defined in `.editorconfig` |
| 71 | + |
| 72 | +* Your mission is to make diffs as absolutely as small as possible, preserving existing code formatting. |
| 73 | + |
| 74 | +* If you encounter additional spaces or formatting within existing code blocks, LEAVE THEM AS-IS. |
| 75 | + |
| 76 | +* If you encounter code comments, LEAVE THEM AS-IS. |
| 77 | + |
| 78 | +* Place a space prior to any parentheses `(` or `[` |
| 79 | + |
| 80 | +* Use `""` for empty string and *not* `string.Empty` |
| 81 | + |
| 82 | +* Use `[]` for empty arrays and *not* `Array.Empty<T>()` |
| 83 | + |
| 84 | +Examples of properly formatted code: |
| 85 | + |
| 86 | +```csharp |
| 87 | +Foo (); |
| 88 | +Bar (1, 2, "test"); |
| 89 | +myarray [0] = 1; |
| 90 | + |
| 91 | +if (someValue) { |
| 92 | + // Code here |
| 93 | +} |
| 94 | + |
| 95 | +try { |
| 96 | + // Code here |
| 97 | +} catch (Exception e) { |
| 98 | + // Code here |
| 99 | +} |
| 100 | +``` |
| 101 | + |
| 102 | +### Code Comments |
| 103 | +- Use XML documentation comments (`///`) for public APIs |
| 104 | +- Document JNI interop behavior and threading requirements |
| 105 | +- Include usage examples for complex scenarios |
| 106 | + |
| 107 | +### Error Handling |
| 108 | +- Java exceptions are automatically converted to .NET exceptions |
| 109 | +- Use `JniEnvironment.Errors.ExceptionOccurred()` for manual exception checking |
| 110 | +- Wrap JNI calls in `try`/`finally` blocks for proper resource cleanup |
| 111 | + |
| 112 | +### Memory Management |
| 113 | +- Local references: Automatically cleaned up by JVM |
| 114 | +- Global references: Must be explicitly freed via `JniObjectReference.Dispose()` |
| 115 | +- Use `using` statements or `try`/`finally` for proper cleanup |
| 116 | + |
| 117 | +### Threading |
| 118 | +- JNI environments are thread-local |
| 119 | +- Use `JniEnvironment.Current` to access the current thread's JNI environment |
| 120 | +- Java objects can be shared across threads with proper reference management |
| 121 | + |
| 122 | +## Build System |
| 123 | + |
| 124 | +### Prerequisites |
| 125 | +- .NET 9+ SDK |
| 126 | +- Java Development Kit (for compiling Java test classes) |
| 127 | +- Platform-specific JVM libraries |
| 128 | + |
| 129 | +### Build Commands |
| 130 | +```bash |
| 131 | +# Initialize submodules and prepare build |
| 132 | +dotnet build -t:Prepare |
| 133 | + |
| 134 | +# Build all projects |
| 135 | +dotnet build |
| 136 | + |
| 137 | +# Run specific tests |
| 138 | +dotnet test tests/Java.Interop-Tests/Java.Interop-Tests.csproj |
| 139 | + |
| 140 | +# Build with specific configuration |
| 141 | +dotnet build -c Release |
| 142 | +``` |
| 143 | + |
| 144 | +### Configuration |
| 145 | +- Use `Configuration.Override.props` for local build customization |
| 146 | +- Set `$(JdkJvmPath)` to specify JVM library location |
| 147 | +- Configure `$(JAVA_HOME)` for Java tooling |
| 148 | + |
| 149 | +## Useful Resources |
| 150 | + |
| 151 | +- [JNI Specification](http://docs.oracle.com/javase/8/docs/technotes/guides/jni/spec/jniTOC.html) |
| 152 | +- [.NET for Android Documentation](https://learn.microsoft.com/en-us/dotnet/android/) |
| 153 | +- [Android JNI Performance Guide](https://developer.android.com/training/articles/perf-jni) |
| 154 | +- [Project Architecture Documentation](Documentation/Architecture.md) |
| 155 | +- [Build Configuration Guide](Documentation/BuildConfiguration.md) |
| 156 | + |
| 157 | +## Getting Help |
| 158 | + |
| 159 | +- Review existing tests for usage patterns |
| 160 | +- Check [GitHub Issues](https://github.com/dotnet/java-interop/issues) for known problems |
| 161 | +- Consult the [.NET Discord](https://aka.ms/dotnet-discord) for community support |
| 162 | +- Follow [Coding Guidelines](http://www.mono-project.com/community/contributing/coding-guidelines/) for contributions |
0 commit comments