Eclipse Apache Maven-Tycho

Introducing about Maven and how to use Maven Tycho plugin to build plugins into the SNAPSHOT Refer1 Refer2 Refer3 1. Apache Maven Apache Maven is an powerful build tool primary for Java software projects. It is implemented in Java which makes it platform-independent. 1.1. Setup Env Install maven: https://maven.apache.org/download.cgi Verify: mvn --version Config proxy (ONLY IF NEED): C:\Users${username}.m2\settings.xml or ${maven_home}\apache-maven-3.9.10\conf\settings.xml <settings> <proxies> <proxy> <id>example-proxy</id> <active>true</active> <protocol>http</protocol> <host>proxy.example.com</host> <port>8080</port> <username>proxyuser</username> <password>somepassword</password> <nonProxyHosts>www.google.com|*.example.com</nonProxyHosts> </proxy> </proxies> </settings> 1.2. Concepts Need the pom file defines: identifiers for the project to be build/ properties relevant for build configuration/ plugins which providefunctionality for the build via a build section. /library and project dependencies via the dependencies section Each project have its onw pom file but What is pom file ? https://maven.apache.org/pom.html Example: <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> // version of the pom file <groupId>com.vogella.maven.first</groupId> // group id -> maven will build the package: <groupId>:<artifactId>:<version> <artifactId>com.vogella.maven.first</artifactId> // project id <version>1.0-SNAPSHOT</version> // project version <name>com.vogella.maven.first</name> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>11</maven.compiler.source> // java version <maven.compiler.target>11</maven.compiler.target> </properties> </project> A Maven project uses the groupId, artifactId, version (also knows as GAV) and the packaging property to uniquely identify a Maven component. ...

June 26, 2025 · 2 min · Phong Nguyen

Markdown Syntax Guide

Introduction Markdown is a lightweight markup language that use to add formatting elements to plaintext documents. Table of contents 1. Heading: To create heading, add one to six # symbols before your heading. The number of # will determine the size of heading. Syntax # H1 ## H2 ### H3 #### H4 ##### H5 ###### H6 Preview H1 H2 H3 H4 H5 H6 2. Emphasis: You can add emphasis by making text bold or italic. ...

August 9, 2024 · 2 min · Phong Nguyen

Makefile Guide

Introduction GNU Make is a tool which controls the generation of executables and other non-source files of a program from the program’s source files. Make get its knowledge of how to build your program from a file called the makefile, which lists each of the non-source files and how to compute it from the other files. When you write a program, you should write a makefile for it, so that it is possible to use Make to build and install the program. ...

August 9, 2024 · 11 min · Phong Nguyen

Java OSGi Service

Explain how to use the OSGi Service Refer1 Refer2 Refer3 Refer4 1. Introduction OSGi Services are essentially Java objects that provide a specific functionality or interface, and other components,plugins can dynamically discover and use these services. Multiple plugins can provide a service implementation for the service interface. Plugins can access the service implementation via the service interface. E.g. When you want to create modular that can be added or removed at runtime, you can use the OSGi service. ...

February 10, 2025 · 6 min · Phong Nguyen

Java NPE

Some stuffs related to NPE Refer1 Refer2 1. Optional This class will be an container object which may or may not contain a non-null value Examples: // Potential dangers of null String version = computer.getSoundcard().getUSB().getVersion(); // Solution 1: Ugly due to the nested checks, decreasing the readability String version = "UNKNOWN"; if(computer != null){ Soundcard soundcard = computer.getSoundcard(); if(soundcard != null){ USB usb = soundcard.getUSB(); if(usb != null){ version = usb.getVersion(); } } } // Solution 2(JavaSE7): String version = computer?.getSoundcard()?.getUSB()?.getVersion(); String version = computer?.getSoundcard()?.getUSB()?.getVersion() ?: "UNKNOWN"; // Solution 3 (JavaSE8) public class Computer { private Optional<Soundcard> soundcard; public Optional<Soundcard> getSoundcard() { ... } ... } public class Soundcard { private Optional<USB> usb; public Optional<USB> getUSB() { ... } } public class USB{ public String getVersion(){ ... } } String name = computer.flatMap(Computer::getSoundcard) .flatMap(Soundcard::getUSB) .map(USB::getVersion) .orElse("UNKNOWN"); 1.1 Creating Optional objects - Optional.of(var)/ Optional.ofNullable(var) SoundCard soundCard = new SoundCard(); Optional<SoundCard> sc = Optional.of(soundCard); // NPE if soundCard is null Optional<SoundCard> sc = Optional.ofNullable(soundCard); // may hold a null value 1.2. Check presenting - .ifPresent(//) // ugly code SoundCard soundcard = ...; if(soundcard != null){ System.out.println(soundcard); } // solution 1 SoundCard soundcard = ...; if(soundcard != null){ System.out.println(soundcard); } // solution 2 if(soundcard.isPresent()){ System.out.println(soundcard.get()); } 1.3. Default - .orElse/ .orElseThrow // ugly code Soundcard soundcard = maybeSoundcard != null ? maybeSoundcard : new Soundcard("basic_sound_card"); // solution 1 Soundcard soundcard = maybeSoundcard.orElse(new Soundcard("defaut")); // default value Soundcard soundcard = maybeSoundCard.orElseThrow(IllegalStateException::new); // default throw E … ...

February 27, 2025 · 2 min · Phong Nguyen

Java JNA

Explain how to use the Java Native Access library (JRA) to access native libraries. References: Using JNA to Access Native Dynamic Libraries 1. Introduction Sometimes we need to use native code to implement some functionality: Reusing legacy code written in C/C++ or any other language able to create native code. Accessing system-specific functionality not available in the standard Java runtime. Trace-offs: Can’t directly use static libraries Slower when compared to handcrafted JNI code. => JNA today is probably the best available choice to access native code from Java. 2. JNA Project Setup Add JNA dependency to the project’s pom.xml (latest version of jna-platform) <dependency> <groupId>net.java.dev.jna</groupId> <artifactId>jna-platform</artifactId> <version>5.6.0</version> </dependency> 3. Using JNA Step1: Create a Java interface that extends JNA’s Library interface to describe the methods and types used when calling the target native code. Step2: Pass this interface to JNA which returns a concrete implementation of this interface that we use to invoke native methods. 3.1. Example 1: Calling methods from the C Standard Library CMath.java import com.sun.jna.Library; import com.sun.jna.Native; import com.sun.jna.Platform; public interface CMath extends Library { CMath INSTANCE = Native.load(Platform.isWindows() ? "msvcrt" : "c", CMath.class); double cosh(double value); } load() method takes two arguments, the return a concrete implementation of this interface, allowing to call any of its methods. The dynamic lib name The Java interface describing the methods that we’ll use We don’t have to add the .so (Linux) or .dll(Window) extension as they’are implied. Also, for Linux-based systems, we don’t need to specific the lib prefix. Main.java public class Main { public static void main(String[] args) { System.out.println(CMath.INSTANCE.cosh(180)); } } 3.2. Example 2: Basic types Mapping char => byte short => short wchar_t => char int => int long => com.sun.jna.NativeLong long long => long float => float double => double char * => String JNA provides the NativeLong type, which uses the proper type depending on the system’architecture. 3.3. Structures and Unions Given this C struct and union struct foo_t { int field1; int field2; char *field3; }; union foo_u { String foo; double bar; }; Java peer class would be @FieldOrder({"field1","field2","field3"}) public class FooType extends Structure { int field1; int field2; String field3; }; public class MyUnion extends Union { public String foo; public double bar; }; MyUnion u = new MyUnion(); u.foo = "test"; u.setType(String.class); lib.some_method(u); Java requires the @FiledOrder annotation so it can properly serialize data into a memory buffer before using it as an argument to the target method.

November 21, 2024 · 2 min · Phong Nguyen

Java Extension Points - Extensions

Explain how to use the Extension Point and Extensions. Refer1 Refer2 Refer3 1. Introduction Extensions are the central mechanism for contribute behavior to the platform. We used it to contribute functionality to a certain type of API. Extension points define new functions points for the platform that other plug-ins can plug into. E.g. When you want to create modular that can be added or removed at runtime, you can use the OSGi service. These extensions and extension points are defined via the plugin.xml file. ...

February 27, 2025 · 3 min · Phong Nguyen

DSF

Explain how to use the DSF. Refer1 1. Introduction Scenario: Debugger View Fetching Data Context: We want to show variable values in the Variables View as the user steps through the code. 1.1. Asynchronous Req: When the user steps through the program, the debugger needs to fetch variable values from the target (like a remote system). This operation might take a few milliseconds or more, depending on the connection. If we block the UI thread, the UI freezes. Solution: Use an asynchronous method to fetch variable data void getVariableValue(String varName, DataRequestMonitor<String> rm) { // Imagine this takes time (e.g., contacting GDB or a remote debugger) // Simulate async call executor.execute(() -> { String value = remoteFetchValue(varName); // Slow operation rm.setData(value); rm.done(); }); } Why Async? Prevents blocking the UI. Allows the Eclipse debug framework to continue updating other views. 1.2. Synchronous We want to implement a feature like evaluate expression that requires the value immediately. We don’t want to rewrite your entire logic using async callbacks just to get the result of getVariableValue(). ...

May 13, 2025 · 8 min · Phong Nguyen

Design Patterns

Explain how to use the design patterns. (Creational Patterns) Refer1 Refer2 Refer3 Refer4 1. Creational Design Patterns. Creational design patterns provide various object creation mechanisms, which increase flexibility and reuse of existing code. 1.1 Simple Factory Factory is an object for creating other objects Use cases: when the class does not know beforehand the exact types and dependencies of the objects it needs to create. When a method returns one of several possible classes that share a common super class and wants to encapsulate the logic of which object to create. when designing frameworks or libraries to give the best flexibility and isolation from concrete class types Examples: You want to manufacture the products many times. ...

February 11, 2025 · 6 min · Phong Nguyen

Java Annotations

Annotations in Java are metadata that provide information about the program but do not change its behavior. They can be used for compilation checks, runtime processing, or documentation generation. References: 1. @Override 2. @Deprecated /[ˈdep.rə.keɪt]/ Can be used on a field, method, constructor or class and indicates that this element it outdated and should not be used anymore.

February 5, 2025 · 1 min · Phong Nguyen