Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.lang.reflect.Field;

import java.util.HashMap;

import java.util.Map;

public class Main {
    /** A stash of variables which we automatically decode */
    private static final Map<String, String> vars = new HashMap<String, String>();

    /**
     * Decode a variable. Variables are in the form $varname. If the incoming
     * expression is not a variable or is not recognised it is simply returned
     * verbatim.
     * @param in The incoming attribute
     * @return String
     * @throws Exception
     */
    private static String decode(String in) throws Exception {
        if (in != null && in.length() > 1 && in.charAt(0) == '$') {
            String key = in.substring(1);
            if (key.charAt(0) == '#') {
                // It's a class name and reflection job in the form $$full.class.name.member
                int lastIdx = key.lastIndexOf('.');
                String member = key.substring(lastIdx + 1);
                String className = key.substring(1, lastIdx);
                Class<?> clazz = Class.forName(className);
                Field field = clazz.getDeclaredField(member);
                field.setAccessible(true);
                return String.valueOf(field.get(null));
            } else {
                String val = vars.get(key);
                if (val == null) {
                    throw new Exception("Unknown variable " + in);
                } else {
                    return val;
                }
            }
        } else {
            return in;
        }
    }
}