Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import java.lang.reflect.Field;

public class Main {
    public static String GEN_PACKAGE_NAME = "";

    public static int getResourceStringId(String parameterName) {
        return getResourceId("string", parameterName);
    }

    public static int getResourceId(String parameterName) {
        return getResourceId("id", parameterName);
    }

    public static int getResourceId(String resFileName, String parameterName) {
        return getResourceId(GEN_PACKAGE_NAME, resFileName, parameterName);
    }

    public static int getResourceId(String packageName, String resFileName, String parameterName) {
        if ((packageName != null) && (resFileName != null) && (parameterName != null))
            try {
                Class localClass = Class.forName(packageName + "$" + resFileName);
                Field localField = localClass.getField(parameterName);
                Object localObject = localField.get(localClass.newInstance());
                return Integer.parseInt(localObject.toString());
            } catch (Exception e) {
                e.printStackTrace();
            }
        return -1;
    }
}