Java examples
The following Java examples show you how to get started generating inline code suggestions and how to trigger the coding assistant in your own projects. Most of these examples also apply to other languages:
Generate classes and functions
To generate a full or partial class implementation, use code comments. The comment describes the intent of the class. Start simple, and add more details if necessary. After you generate a class, you can continue to generate functions within its skeleton.
When writing comments, it's preferable to use the language's comment structure. In Java, this is /** */.
Input:
/** * DocumentReader class. has a name, children and optional parent. * Includes I/O functions to read and * write document */
Output:
/** * DocumentReader class. has a name, children and optional parent. * Includes I/O functions to read and * write document */ import java.util.ArrayList; public class DocumentReader { private String name; private DocumentReader parent; private ArrayList<DocumentReader> children; /** * Constructor * @param name name of the document * @param parent parent of the document */ public DocumentReader(String name, DocumentReader parent) { this.name = name; this.parent = parent; this.children = new ArrayList<>(); } /** * Constructor * @param name name of the document */ public DocumentReader(String name) { this(name, null); } /** * Getter for name * @return name of the document */ public String getName() { return name; } /** * Getter for parent * @return parent of the document */ public DocumentReader getParent() { return parent; } /** * Getter for children * @return children of the document */ public ArrayList<DocumentReader> getChildren() { return children; } /** * Adds a child to the document * @param child child to be added */ public void addChild(DocumentReader child) { children.add(child); } /** * Removes a child from the document * @param child child to be removed */ public void removeChild(DocumentReader child) { children.remove(child); } }
Now that you have an initial class, you can generate an additional function by adding more comments.
Input:
/** * function to read document from local directory path * and print first line of document */
Output:
/** * function to read document from local directory path * and print first line of document */ public void readDocument() { try { BufferedReader reader = new BufferedReader(new FileReader(name)); String line = reader.readLine(); System.out.println(line); reader.close(); } catch (IOException e) { System.out.println("Error reading document: " + e.getMessage()); } }
Document code
Reduce the time you spend on code documentation by using inline documentation. Add a Java block comment (/* */).
Input:
import java.util.function.Function; public class S3Utils { /* * <place cursor here> */ public static Function<String, String> generateS3Function(String bucketName, String region) { return (String key) -> { String url = generateS3Url(bucketName, key, region); return url; }; } }
If inline generation doesn't occur, run an Amazon Q Developer inline suggestion after you place your cursor where you want documentation.
Output:
import java.util.function.Function; public class S3Utils { /* * Generate a function that returns the S3 URL for a given key * @param bucketName the name of the bucket * @param region the region of the bucket * @return the function that generates the S3 URL */ public static Function<String, String> generateS3Function(String bucketName, String region) { return (String key) -> { String url = generateS3Url(bucketName, key, region); return url; }; } }
For simpler explanations of code (classes, functions, or script elements), place double forward slashes (//) before the code sections where you want to add documentation.
Input:
// public class S3Utils { /* * Generate a function that returns the S3 URL for a given key * @param bucketName the name of the bucket * @param region the region of the bucket * @return the function that generates the S3 URL */ public static Function<String, String> generateS3Function(String bucketName, String region) { return (String key) -> { String url = generateS3Url(bucketName, key, region); return url; }; } }
Output:
// This class is used to generate a function that returns the S3 URL for a given key public class S3Utils { /* * Generate a function that returns the S3 URL for a given key * @param bucketName the name of the bucket * @param region the region of the bucket * @return the function that generates the S3 URL */ public static Function<String, String> generateS3Function(String bucketName, String region) { return (String key) -> { String url = generateS3Url(bucketName, key, region); return url; }; } }
Generate algorithms
Amazon Q Developer can generate popular algorithms, especially related to AWS SDKs. In your script, add a prompt that specifies the function you want.
This example generates a hash map of user ID keys to name values and then generates a sorting function using the bubble sort algorithm.
Input:
import java.util.HashMap; public class SortList { // generate key value pair hash map for // user id and name public static HashMap<Integer, String> getUserIdNameMap() { HashMap<Integer, String> userIdNameMap = new HashMap<Integer, String>(); userIdNameMap.put(1, "Amit"); userIdNameMap.put(2, "Rahul"); userIdNameMap.put(3, "Vijay"); userIdNameMap.put(4, "Raj"); userIdNameMap.put(5, "Kumar"); return userIdNameMap; } /* * Create function to sort UserIdName map by name alphabetically using bubble sort */ }
Output:
/* * Create function to sort UserIdName map by name alphabetically using bubble sort */ public static HashMap<Integer, String> sortUserIdNameMap(HashMap<Integer, String> userIdNameMap) { int size = userIdNameMap.size(); for (int i = 0; i < size; i++) { for (int j = i + 1; j < size; j++) { if (userIdNameMap.get(i).compareTo(userIdNameMap.get(j)) > 0) { String temp = userIdNameMap.get(i); userIdNameMap.put(i, userIdNameMap.get(j)); userIdNameMap.put(j, temp); } } } return userIdNameMap; }
In addition, you can create prompts for standard procedures. In this example, you create a prompt to store the hash map in an Amazon DynamoDB table. At the beginning of the script, you add a prompt to add DynamoDB Java libraries to help provide Amazon Q Developer with additional context.
Input:
// add libraries for Amazon DynamoDB import com.amazonaws.services.dynamodbv2.AmazonDynamoDB; import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder; /* * Store sorted UserIdName map into Amazon DynamoDB table named users */
Output:
// add libraries for Amazon DynamoDB import com.amazonaws.services.dynamodbv2.AmazonDynamoDB; import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder; /* * Store sorted UserIdName map into Amazon DynamoDB table named users */ public static void storeSortedUserIdNameMap(HashMap<Integer, String> userIdNameMap) { // create Amazon DynamoDB client AmazonDynamoDB client = AmazonDynamoDBClientBuilder.defaultClient(); // store sorted UserIdName map into Amazon DynamoDB table named users for (int userId : userIdNameMap.keySet()) { String name = userIdNameMap.get(userId); client.putItem("users", new HashMap<String, Object>() {{ put("userId", userId); put("name", name); }}); } }
Generate unit tests
With inline comments, Amazon Q Developer can generate unit tests to perform assertions and other JUnit tests. In this example, you first ask Amazon Q to generate applicable libraries for JUnit to set the context of the test script. Next, you create a public class followed by a declarator and comment strings.
Input:
// import libraries for java unit tests import org.junit.Test; import static org.junit.Assert.*; public class UnitTests { @Test /* * Create unit test for getUserIdNameMap */ }
Output:
@Test /* * Create unit test for getUserIdNameMap */ public void testGetUserIdNameMap() { // create a new instance of the UserIdNameMap class UserIdNameMap userIdNameMap = new UserIdNameMap(); // create a new instance of the User class User user = new User("John", "Doe"); // add the user to the map userIdNameMap.addUser(user); // get the user from the map User retrievedUser = userIdNameMap.getUser("John"); // assert that the retrieved user is the same as the user added to the map assertEquals(user, retrievedUser); }
In a chat, Amazon Q Developer also suggests and generates unit tests based on your code input prompts. For more information, see Chat examples.