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

    Generate a random string, which is guaranteed to be a valid Java identifier

    /**
     * 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);
    }
    
Search
    • Home