Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 * Copyright 2010 JBoss Inc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Main {
    /**
     * Extracts the package name from the given class object
     */
    public static String getPackage(Class<?> cls) {
        // cls.getPackage() sometimes returns null, in which case fall back to string massaging.
        java.lang.Package pkg = cls.isArray() ? cls.getComponentType().getPackage() : cls.getPackage();
        if (pkg == null) {
            int dotPos;
            int dolPos = cls.getName().indexOf('$');
            if (dolPos > 0) {
                // we have nested classes, so adjust dotpos to before first $
                dotPos = cls.getName().substring(0, dolPos).lastIndexOf('.');
            } else {
                dotPos = cls.getName().lastIndexOf('.');
            }

            if (dotPos > 0) {
                return cls.getName().substring(0, dotPos);
            } else {
                // must be default package.
                return "";
            }
        } else {
            return pkg.getName();
        }
    }
}