String (CharSequence text) { Lexer lexer = new GroovyLexer(); lexer.start(text); skipWhitespacesAndComments(lexer); final IElementType firstToken = lexer.getTokenType(); if (firstToken != GroovyTokenTypes.kPACKAGE) { return ""; } lexer.advance(); skipWhitespacesAndComments(lexer); final StringBuilder buffer = new StringBuilder(); while(true){ if (lexer.getTokenType() != GroovyTokenTypes.mIDENT) break; buffer.append(text, lexer.getTokenStart(), lexer.getTokenEnd()); lexer.advance(); skipWhitespacesAndComments(lexer); if (lexer.getTokenType() != GroovyTokenTypes.mDOT) break; buffer.append('.'); lexer.advance(); skipWhitespacesAndComments(lexer); } String packageName = buffer.toString(); if (packageName.isEmpty() || StringUtil.endsWithChar(packageName, '.')) return null; return packageName; }