String (CharSequence text) { Lexer lexer = JavaParserDefinition.createLexer(LanguageLevel.JDK_1_5); lexer.start(text); skipWhiteSpaceAndComments(lexer); skipAnnotations(lexer); final IElementType firstToken = lexer.getTokenType(); if (firstToken != JavaTokenType.PACKAGE_KEYWORD) { if (JAVA_FILE_FIRST_TOKEN_SET.contains(firstToken)) { return ""; } return null; } lexer.advance(); skipWhiteSpaceAndComments(lexer); final StringBuilder buffer = new StringBuilder(); while(true){ if (lexer.getTokenType() != JavaTokenType.IDENTIFIER) break; buffer.append(text, lexer.getTokenStart(), lexer.getTokenEnd()); lexer.advance(); skipWhiteSpaceAndComments(lexer); if (lexer.getTokenType() != JavaTokenType.DOT) break; buffer.append('.'); lexer.advance(); skipWhiteSpaceAndComments(lexer); } String packageName = buffer.toString(); if (packageName.length() == 0 || StringUtil.endsWithChar(packageName, '.')) return null; return packageName; }