Java tutorial
//package com.java2s; import javax.lang.model.element.TypeElement; public class Main { public static String getPackageName(TypeElement classDecl) { String fullName = classDecl.getQualifiedName().toString(); int pos = fullName.lastIndexOf('.'); if (pos == -1) return null; return fullName.substring(0, pos); } public static String getPackageName(TypeElement classDecl, String path) { String orgPkg = getPackageName(classDecl); if (path.equals(".")) return orgPkg; if (path.startsWith("/")) { return path.replace('/', '.').substring(1); } path = orgPkg.replace('.', '/') + "/" + path; ArrayList<String> list = new ArrayList<String>(Arrays.asList(path.split("/"))); for (int i = 0; i < list.size();) { String cur = list.get(i); if (cur.equals(".")) { list.remove(i); } else if (cur.equals("..")) { i--; list.remove(i); list.remove(i); } else { i++; } } StringBuilder sbuf = new StringBuilder(); for (int i = 0; i < list.size(); i++) { sbuf.append("."); sbuf.append(list.get(i)); } return sbuf.toString().substring(1); } }