• Intro Headers Instructions Macros Commands
  • Fork me on GitHub
    • Introduction
    • How to install bnd
    • Guided Tour
    • Guided Tour Workspace & Projects
    • Concepts
    • Best practices
    • Build
    • Project Setup
    • Generating JARs
    • Versioning
    • Baselining
    • Service Components
    • Metatype
    • Contracts
    • Bundle Annotations
    • Accessor Properties
    • SPI Annotations
    • Resolving Dependencies
    • Launching
    • Startlevels
    • Testing
    • Testing with Launchpad
    • Packaging Applications
    • JPMS Libraries
    • Wrapping Libraries to OSGi Bundles
    • Generating Documentation
    • Commands
    • For Developers
    • Tips for Windows users
    • Tools bound to bnd
    • Headers
    • Instruction Reference
    • Instruction Index
    • Macro Reference
    • Macro Index
    • Plugins
    • External Plugins
    • Settings
    • Errors
    • Warnings
    • Frequently Asked Questions
  • random

    /**
     * Generate a random string, which is guaranteed to be a valid Java
     * identifier (first character is an ASCII letter, subsequent characters are
     * ASCII letters or numbers). Takes an optional parameter for the length of
     * string to generate; default is 8 characters.
     */
    public String _random(String[] args) {
    	int numchars = 8;
    	if (args.length > 1) {
    		try {
    			numchars = Integer.parseInt(args[1]);
    		}
    		catch (NumberFormatException e) {
    			throw new IllegalArgumentException("Invalid character count parameter in ${random} macro.");
    		}
    	}
    
    	synchronized (Processor.class) {
    		if (random == null)
    			random = new Random();
    	}
    
    	char[] letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
    	char[] alphanums = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".toCharArray();
    
    	char[] array = new char[numchars];
    	for (int i = 0; i < numchars; i++) {
    		char c;
    		if (i == 0)
    			c = letters[random.nextInt(letters.length)];
    		else
    			c = alphanums[random.nextInt(alphanums.length)];
    		array[i] = c;
    	}
    
    	return new String(array);
    }
    
    • GitHub