Skip to content

The Unified and Autonomous Development Platform

A basic JavaAnalyzer.

robin shinec8cac72c3693
diff --git a/commons.lang/pom.xml b/commons.lang/pom.xml
index 69f7b8d495..3982604540 100644
--- a/commons.lang/pom.xml
+++ b/commons.lang/pom.xml
@@ -71,7 +71,7 @@
</repositories>
<properties>
- <moduleClass>com.pmease.commons.lang.TokenizerModule</moduleClass>
+ <moduleClass>com.pmease.commons.lang.LangModule</moduleClass>
</properties>
</project>
diff --git a/commons.lang/src/main/java/com/pmease/commons/lang/TokenizerModule.java b/commons.lang/src/main/java/com/pmease/commons/lang/LangModule.java
similarity index 87%
rename from commons.lang/src/main/java/com/pmease/commons/lang/TokenizerModule.java
rename to commons.lang/src/main/java/com/pmease/commons/lang/LangModule.java
index 645c8b7df1..4e0fb7f14f 100644
--- a/commons.lang/src/main/java/com/pmease/commons/lang/TokenizerModule.java
+++ b/commons.lang/src/main/java/com/pmease/commons/lang/LangModule.java
@@ -6,7 +6,7 @@ import com.pmease.commons.loader.AbstractPluginModule;
* NOTE: Do not forget to rename moduleClass property defined in the pom if you've renamed this class.
*
*/
-public class TokenizerModule extends AbstractPluginModule {
+public class LangModule extends AbstractPluginModule {
@Override
protected void configure() {
diff --git a/commons.lang/src/main/java/com/pmease/commons/lang/LineAwareToken.java b/commons.lang/src/main/java/com/pmease/commons/lang/LineAwareToken.java
index 4eacc71f9d..2a999caa3e 100644
--- a/commons.lang/src/main/java/com/pmease/commons/lang/LineAwareToken.java
+++ b/commons.lang/src/main/java/com/pmease/commons/lang/LineAwareToken.java
@@ -1,20 +1,22 @@
package com.pmease.commons.lang;
-
public class LineAwareToken extends Token {
private static final long serialVersionUID = 1L;
- private final int line;
+ private final int linePos;
- public LineAwareToken(Token token, int line) {
- super(token.style(), token.text());
-
- this.line = line;
+ public LineAwareToken(String style, String text, int linePos) {
+ super(style, text);
+ this.linePos = linePos;
}
- public int getLine() {
- return line;
+ public LineAwareToken(Token token, int linePos) {
+ this(token.style(), token.text(), linePos);
}
+ public int getLinePos() {
+ return linePos;
+ }
+
}
diff --git a/commons.lang/src/main/java/com/pmease/commons/lang/Outline.java b/commons.lang/src/main/java/com/pmease/commons/lang/Outline.java
new file mode 100644
index 0000000000..84eb04d58d
--- /dev/null
+++ b/commons.lang/src/main/java/com/pmease/commons/lang/Outline.java
@@ -0,0 +1,5 @@
+package com.pmease.commons.lang;
+
+public interface Outline {
+
+}
diff --git a/commons.lang/src/main/java/com/pmease/commons/lang/Token.java b/commons.lang/src/main/java/com/pmease/commons/lang/Token.java
index e3b997e058..1b331436a3 100644
--- a/commons.lang/src/main/java/com/pmease/commons/lang/Token.java
+++ b/commons.lang/src/main/java/com/pmease/commons/lang/Token.java
@@ -26,75 +26,71 @@ public class Token implements Serializable {
}
public boolean isComment() {
- return style.contains("cm-comment");
+ return style.contains("comment");
}
public boolean isKeyword() {
- return style.contains("cm-keyword");
+ return style.contains("keyword");
}
public boolean isNumber() {
- return style.contains("cm-number");
+ return style.contains("number");
}
public boolean isIdentifier() {
- return style.contains("cm-variable");
+ return style.contains("variable");
}
public boolean isString() {
- return style.contains("cm-string");
+ return style.contains("string");
}
public boolean isMeta() {
- return style.contains("cm-meta");
+ return style.contains("meta");
}
public boolean isTag() {
- return style.contains("cm-tag");
+ return style.contains("tag");
}
public boolean isBracket() {
- return style.contains("cm-bracket");
+ return style.contains("bracket");
}
public boolean isOperator() {
- return style.contains("cm-operator");
+ return style.contains("operator");
}
public boolean isAtom() {
- return style.contains("cm-atom");
+ return style.contains("atom");
}
public boolean isDef() {
- return style.contains("cm-def");
+ return style.contains("def");
}
public boolean isQualifier() {
- return style.contains("cm-qualifier");
+ return style.contains("qualifier");
}
public boolean isAttribute() {
- return style.contains("cm-attribute");
+ return style.contains("attribute");
}
public boolean isProperty() {
- return style.contains("cm-property");
+ return style.contains("property");
}
public boolean isBuiltin() {
- return style.contains("cm-builtin");
+ return style.contains("builtin");
}
public boolean isLink() {
- return style.contains("cm-link");
+ return style.contains("link");
}
public boolean isError() {
- return style.contains("cm-error");
- }
-
- public boolean isNotCommentOrString() {
- return !isComment() && !isString();
+ return style.contains("error");
}
public boolean isEof() {
@@ -105,7 +101,17 @@ public class Token implements Serializable {
return style.length() == 0 && text.equals("\n");
}
- public boolean isWhitespace() {
+ public boolean is(String... anySymbols) {
+ if (isComment() || isString())
+ return false;
+ for (String symbol: anySymbols) {
+ if (symbol.equals(text))
+ return true;
+ }
+ return false;
+ }
+
+ public boolean isWhitespace() {
if (style.length() != 0)
return false;
for (char ch: text.toCharArray()) {
diff --git a/commons.lang/src/main/java/com/pmease/commons/lang/TokenStream.java b/commons.lang/src/main/java/com/pmease/commons/lang/TokenStream.java
index ce7dfaa6d3..e558435c0a 100644
--- a/commons.lang/src/main/java/com/pmease/commons/lang/TokenStream.java
+++ b/commons.lang/src/main/java/com/pmease/commons/lang/TokenStream.java
@@ -4,62 +4,46 @@ import java.util.ArrayList;
import java.util.List;
public class TokenStream {
-
- private static final Token EOF = new Token("", "");
-
- private static final Token EOL = new Token("", "\n");
-
+
+ private static final LineAwareToken EOF = new LineAwareToken("", "", -1);
+
private final List<LineAwareToken> tokens;
- private int tokenPos; // 0-indexed position of current token
+ private int tokenPos = 0; // 0-indexed position of current token
- public TokenStream(List<TokenizedLine> lines, boolean preserveWhitespace) {
- tokens = new ArrayList<>();
- int linePos = 0;
- for (TokenizedLine line: lines) {
- for (Token token: line.getTokens()) {
- if (token.isNotCommentOrString() && (preserveWhitespace || !token.isWhitespace()))
- tokens.add(new LineAwareToken(token, linePos));
- }
- if (preserveWhitespace)
- tokens.add(new LineAwareToken(EOL, linePos));
- linePos++;
- }
+ public TokenStream(List<LineAwareToken> tokens) {
+ this.tokens = tokens;
}
- private Token getToken(int pos) {
- if (pos >=0 && pos < tokens.size())
- return tokens.get(pos);
+ public LineAwareToken getToken(int tokenPos) {
+ if (tokenPos >=0 && tokenPos < tokens.size())
+ return tokens.get(tokenPos);
else
return EOF;
}
- public Token current() {
- return getToken(tokenPos);
- }
-
- public int tokenPos() {
- return tokenPos;
+ public LineAwareToken next() {
+ return nextCount(1);
}
-
- public Token next() {
- tokenPos++;
- return current();
+
+ public LineAwareToken nextCount(int count) {
+ tokenPos += count;
+ return getToken(tokenPos);
}
- public Token lookAhead(int ahead) {
+ public LineAwareToken lookAhead(int ahead) {
return getToken(tokenPos+ahead);
}
- public Token lookBehind(int behind) {
+ public LineAwareToken lookBehind(int behind) {
return getToken(tokenPos-behind);
}
-
- public Token next(String...anyOf) {
- Token token = next();
+
+ public LineAwareToken nextSymbol(String...anySymbols) {
+ LineAwareToken token = next();
while(!token.isEof()) {
- for (String text: anyOf) {
- if (text.equals(token.text()))
+ for (String text: anySymbols) {
+ if (token.is(text))
return token;
}
token = next();
@@ -67,33 +51,58 @@ public class TokenStream {
return token;
}
- private Token nextBalanced(String open, String close) {
+ private LineAwareToken nextBalanced(String open, String close) {
int nestingLevel = 1;
- Token balanced = next(open, close);
+ LineAwareToken balanced = nextSymbol(open, close);
while (!balanced.isEof()) {
- if (balanced.text().equals(close)) {
+ if (balanced.is(close)) {
if (--nestingLevel == 0)
return balanced;
- } else if (balanced.text().equals(open)) {
+ } else if (balanced.is(open)) {
nestingLevel++;
}
- balanced = next(open, close);
+ balanced = nextSymbol(open, close);
}
return balanced;
}
- public Token nextBalanced(Token token) {
- if (token.text().equals("{")) {
+ public LineAwareToken nextBalanced(Token token) {
+ if (token.is("{")) {
return nextBalanced("{", "}");
- } else if (token.text().equals("[")) {
+ } else if (token.is("[")) {
return nextBalanced("[", "]");
- } else if (token.text().equals("<")) {
+ } else if (token.is("<")) {
return nextBalanced("<", ">");
- } else if (token.text().equals("(")) {
+ } else if (token.is("(")) {
return nextBalanced("(", ")");
} else {
throw new IllegalStateException("Not a balanceable token: " + token.text());
}
}
+ public int tokenPos() {
+ return tokenPos;
+ }
+
+ /**
+ * Get tokens between startPos and endPos
+ *
+ * @param startPos
+ * 0-indexed start position, inclusive
+ * @param endPos
+ * 0-indexed end position, inclusive
+ * @return
+ * list of tokens between startPos and endPos
+ */
+ public List<LineAwareToken> tokens(int startPos, int endPos) {
+ List<LineAwareToken> tokens = new ArrayList<>();
+ for (int i=startPos; i<=endPos; i++)
+ tokens.add(getToken(i));
+ return tokens;
+ }
+
+ public LineAwareToken current() {
+ return nextCount(0);
+ }
+
}
diff --git a/commons.lang/src/main/java/com/pmease/commons/lang/analyzers/java/FieldDef.java b/commons.lang/src/main/java/com/pmease/commons/lang/analyzers/java/FieldDef.java
new file mode 100644
index 0000000000..5770294f5f
--- /dev/null
+++ b/commons.lang/src/main/java/com/pmease/commons/lang/analyzers/java/FieldDef.java
@@ -0,0 +1,33 @@
+package com.pmease.commons.lang.analyzers.java;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.annotation.Nullable;
+
+public class FieldDef {
+
+ public String name;
+
+ /**
+ * This field will be null for a enum constant
+ */
+ @Nullable
+ public String type;
+
+ public int line;
+
+ public List<Modifier> modifiers = new ArrayList<>();
+
+ @Override
+ public String toString() {
+ StringBuilder builder = new StringBuilder();
+ for (Modifier modifier: modifiers)
+ builder.append(modifier.name().toLowerCase()).append(" ");
+ if (type != null)
+ builder.append(type).append(" ");
+ builder.append(name);
+ return builder.toString();
+ }
+
+}
diff --git a/commons.lang/src/main/java/com/pmease/commons/lang/analyzers/java/JavaAnalyzer.java b/commons.lang/src/main/java/com/pmease/commons/lang/analyzers/java/JavaAnalyzer.java
new file mode 100644
index 0000000000..9ed5124192
--- /dev/null
+++ b/commons.lang/src/main/java/com/pmease/commons/lang/analyzers/java/JavaAnalyzer.java
@@ -0,0 +1,569 @@
+package com.pmease.commons.lang.analyzers.java;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.annotation.Nullable;
+
+import com.google.common.collect.Lists;
+import com.pmease.commons.lang.LineAwareToken;
+import com.pmease.commons.lang.Outline;
+import com.pmease.commons.lang.Token;
+import com.pmease.commons.lang.TokenStream;
+import com.pmease.commons.lang.TokenizedLine;
+
+public class JavaAnalyzer {
+
+ private static final String[] PRIMITIVES = new String[]{
+ "float", "int", "long", "boolean", "char", "byte", "short", "double", "void"};
+
+ public Outline analyze(List<TokenizedLine> lines) {
+ List<LineAwareToken> tokens = new ArrayList<>();
+ int linePos = 0;
+ for (TokenizedLine line: lines) {
+ for (Token token: line.getTokens()) {
+ if (!token.isComment() && !token.isWhitespace()) {
+ // Code mirror tokenize @ together with subsequent identifier as a meta
+ // if they are not separated by space
+ if (token.isMeta() && token.text().startsWith("@") && token.text().length() > 1) {
+ tokens.add(new LineAwareToken("", "@", linePos));
+ String rest = token.text().substring(1);
+ if (rest.equals("interface"))
+ tokens.add(new LineAwareToken("cm-keyword", rest, linePos));
+ else
+ tokens.add(new LineAwareToken("cm-variable", rest, linePos));
+ } else {
+ tokens.add(new LineAwareToken(token, linePos));
+ }
+ }
+ }
+ linePos++;
+ }
+
+ return analyze(new TokenStream(tokens));
+ }
+
+ /*
+ * Analyze specified stream.
+ *
+ * @before-token: start of file
+ * @after-token: EOF
+ *
+ */
+ private JavaOutline analyze(TokenStream stream) {
+ JavaOutline outline = new JavaOutline();
+
+ Token token = stream.current();
+
+ while (token.is("@") && !stream.lookAhead(1).is("interface"))
+ skipAnnotation(stream);
+
+ if (token.is("package")) {
+ token = stream.next();
+ outline.packageName = skipTypeName(stream);
+ token = stream.next();
+ }
+
+ while (token.is("import")) {
+ token = stream.nextSymbol(";");
+ token = stream.next();
+ }
+
+ while(true) {
+ while (token.is(";"))
+ token = stream.next();
+ if (!token.isEof()) {
+ List<Modifier> modifiers = skipModifiers(stream);
+ outline.typeDefs.add(defineType(stream, modifiers));
+ token = stream.next();
+ } else {
+ break;
+ }
+ }
+
+ return outline;
+ }
+
+ /*
+ * Populate typeDef with various members in the type and then skip to end of the body.
+ *
+ * @before-token: '{' or ';' (';' occurs inside of a enum definition)
+ * @after-token: '}'
+ */
+ private void defineTypeBody(TokenStream stream, TypeDef typeDef) {
+ Token token = stream.next();
+ while(true) {
+ while (token.is(";"))
+ token = stream.next();
+ if (token.is("{")) { // instance initializer
+ stream.nextBalanced(token);
+ stream.next();
+ } else if (token.is("static") && stream.lookAhead(1).is("{")) { // static initializer
+ stream.nextBalanced(stream.next());
+ stream.next();
+ } else if (token.is("}") || token.isEof()) {
+ break;
+ } else {
+ List<Modifier> modifiers = skipModifiers(stream);
+ if (token.is("<")) { // skip type params of constructor or method
+ stream.nextBalanced(token);
+ token = stream.next();
+ }
+ if (token.is("class", "interface", "enum") || token.is("@") && stream.lookAhead(1).is("interface")) {
+ typeDef.typeDefs.add(defineType(stream, modifiers));
+ } else {
+ skipModifiers(stream); // skip annotations applied to method return type
+ token = stream.current();
+ if (token.text().equals(typeDef.name) && stream.lookAhead(1).is("(")) {
+ // this is a constructor
+ typeDef.methodDefs.add(defineMethod(stream, modifiers, null));
+ } else {
+ String typeRef = skipTypeRef(stream);
+ token = stream.current();
+ if (token.isIdentifier() && stream.lookAhead(1).is("("))
+ typeDef.methodDefs.add(defineMethod(stream, modifiers, typeRef));
+ else
+ typeDef.fieldDefs.addAll(defineFields(stream, modifiers, typeRef));
+ }
+ }
+ token = stream.next();
+ }
+ }
+
+ }
+
+ /*
+ * Skip value assigned to a field. We either need to skip to next ';' (end of current statement)
+ * or ',' (separator of field declaration). After stripping all top scopes ('[...]', '{...}',
+ * '(...)') before encountering ',' or ';', we can guarantee that ';' will not be contained in value,
+ * however ',' can still appear in value in below form:
+ * ...new HashMap<String, String>...
+ * Also we can not strip scope '<...>' to remove this possibility as '<' '>' can also be used as
+ * conditional operators in value expression (for instance: 2>1?a:b). So we assume encountered ','
+ * as field separator first, and continue with our analyzing until our assumption is proved to be
+ * true or false. True assumption will result in over skipping and it will return a list of
+ * subsequent encountered field names, while false assumption does no harm, as we simply move on
+ * to next comma in the loop.
+ *
+ * @before-token: start of value
+ * @after-token: next token after value
+ *
+ * @return
+ * list of subsequent field names if skipping value caused multiple
+ * fields to be consumed
+ *
+ */
+ private List<String> skipValue(TokenStream stream) {
+ List<String> fieldNames = new ArrayList<>();
+ Token token = stream.current();
+ if (token.is("{")) { // array value
+ stream.nextBalanced(token);
+ stream.next();
+ return fieldNames;
+ } else {
+ while (true) {
+ token = stream.nextSymbol(",", ";", "{", "[", "(");
+ if (token.is("{", "[", "(")) {
+ // skip scopes and the only case for comma inside a value is inside type arguments like below:
+ // ... new HashMap<String, String>...
+ stream.nextBalanced(token);
+ stream.next();
+ } else if (token.is(";") || token.isEof()) {
+ break;
+ } else { // token is ','
+ List<String> subsequentFieldNames = assumeFieldSeparator(stream);
+ if (subsequentFieldNames != null) { // assumption is correct
+ fieldNames.addAll(subsequentFieldNames);
+ break;
+ }
+ }
+ }
+ return fieldNames;
+ }
+ }
+
+ /*
+ * Assume current token is field separator and continue to analyze the stream. Upon
+ * analyzing surprise, we return a null value to indicate the assumption is incorrect.
+ *
+ * @before-token: ','
+ * @after-token: next field separator (',') or ';' if assumption is
+ * correct; or any position in middle of value if
+ * assumption is incorrect
+ *
+ * @return
+ * list of encountered subsequent field names if assumption is correct, or
+ * null if assumption is incorrect
+ *
+ */
+ private List<String> assumeFieldSeparator(TokenStream stream) {
+ while (true) {
+ Token token = stream.next();
+ if (token.isIdentifier()) {
+ List<String> fieldNames = new ArrayList<>();
+ String identifier = token.text();
+ stream.next();
+ identifier += skipDims(stream);
+ fieldNames.add(identifier);
+ token = stream.current();
+ if (token.is("=")) { // assumption convinced
+ stream.next();
+ fieldNames.addAll(skipValue(stream));
+ return fieldNames;
+ } else if (token.is(";")) { // assumption convinced
+ return fieldNames;
+ } else if (token.is(",")) { // assumption still not convinced
+ List<String> subsequentFieldNames = assumeFieldSeparator(stream);
+ if (subsequentFieldNames != null)
+ fieldNames.addAll(subsequentFieldNames);
+ else
+ return null;
+ } else { // assumption is incorrect
+ return null;
+ }
+ } else { // assumption is incorrect
+ return null;
+ }
+ }
+ }
+
+ /*
+ * Skip possible dims.
+ *
+ * @before-token: start of possible dims
+ * @after-token: next token after dims, or remain unchanged if no dims
+ */
+ private String skipDims(TokenStream stream) {
+ String dims = "";
+ while (true) {
+ skipModifiers(stream);
+ Token token = stream.current();
+ if (token.is("[")) {
+ dims += "[]";
+ stream.nextBalanced(token);
+ stream.next();
+ } else {
+ break;
+ }
+ }
+ return dims;
+ }
+
+ /*
+ * Skip type reference.
+ *
+ * @before-token: start of type ref
+ * @after-token: next token after type ref
+ */
+ private String skipTypeRef(TokenStream stream) {
+ LineAwareToken token = stream.current();
+ String typeRef = token.text();
+ if (token.is(PRIMITIVES)) {
+ stream.next();
+ } else {
+ typeRef = skipTypeRefSegment(stream);
+ while (token.is(".")) {
+ typeRef += ".";
+ stream.next();
+ typeRef += skipTypeRefSegment(stream);
+ token = stream.current();
+ }
+ }
+ typeRef += skipDims(stream);
+ return typeRef;
+ }
+
+ /*
+ * Skip type ref segment.
+ *
+ * @begin-token: start of a type ref segment including possible annotations
+ * @end-token: next token after type ref segment
+ */
+ private String skipTypeRefSegment(TokenStream stream) {
+ skipModifiers(stream);
+ String identifier = stream.current().text();
+ Token token = stream.next();
+ if (token.is("<")) {
+ int tokenPos = stream.tokenPos();
+ stream.nextBalanced(token);
+ TokenStream typeArgStream = new TokenStream(stream.tokens(tokenPos, stream.tokenPos()));
+ token = typeArgStream.current();
+ while (!token.isEof()) {
+ skipModifiers(typeArgStream);
+ identifier += token.text();
+ token = typeArgStream.next();
+ }
+ stream.next();
+ }
+ return identifier;
+ }
+
+ /*
+ * Define a method or constructor.
+ *
+ * @before-token: identifier of the method
+ * @after-token: '}' for class method, ';' for interface method or annotation attribute
+ */
+ private MethodDef defineMethod(TokenStream stream, List<Modifier> modifiers, @Nullable String typeRef) {
+ Token token = stream.current();
+ MethodDef methodDef = new MethodDef();
+ methodDef.modifiers = modifiers;
+ methodDef.name = token.text();
+ stream.next(); // '('
+ token = stream.next();
+ while (!token.is(")") && !token.isEof()) {
+ skipModifiers(stream);
+ String paramType = skipTypeRef(stream);
+ skipModifiers(stream);
+ token = stream.current();
+ if (token.is("...")) { // varargs
+ paramType += "...";
+ stream.next(); // identifier
+ token = stream.next();
+ } if (token.is("this")) { // Receiver parameter
+ token = stream.next();
+ paramType = null;
+ } else if (token.isIdentifier() && stream.lookAhead(1).is(".")) { // Receiver parameter
+ stream.next(); // '.'
+ stream.next(); // 'this'
+ token = stream.next();
+ paramType = null;
+ } else { // normal parameter
+ stream.next(); // identifier
+ stream.next();
+ paramType += skipDims(stream);
+ token = stream.current();
+ }
+ if (paramType != null) {
+ if (methodDef.params != null)
+ methodDef.params += ", " + paramType;
+ else
+ methodDef.params = paramType;
+ }
+ if (token.is(","))
+ token = stream.next();
+ }
+ token = stream.next();
+ if (typeRef != null)
+ methodDef.returnType = typeRef + skipDims(stream);
+
+ token = stream.current();
+ if (token.is("throws")) {
+ while (true) {
+ token = stream.nextSymbol(";", "{", "(");
+ if (token.is("(")) {
+ stream.nextBalanced(token);
+ token = stream.next();
+ } else {
+ break;
+ }
+ }
+ } else if (token.is("default")) {
+ stream.nextSymbol(";");
+ }
+
+ token = stream.current();
+
+ if (token.is("{"))
+ stream.nextBalanced(token);
+
+ return methodDef;
+ }
+
+ /*
+ * Define fields declared in a single statement.
+ *
+ * @before-token: identifier of field declaration statement
+ * @after-token: end of fields declaration statement, which is ';'
+ */
+ private List<FieldDef> defineFields(TokenStream stream, List<Modifier> modifiers, String typeRef) {
+ Token token = stream.current();
+ List<FieldDef> fieldDefs = new ArrayList<>();
+ while (!token.is(";") && !token.isEof()) {
+ FieldDef fieldDef = new FieldDef();
+ fieldDefs.add(fieldDef);
+ fieldDef.name = token.text();
+ fieldDef.modifiers = modifiers;
+ stream.next();
+ fieldDef.type = typeRef + skipDims(stream);
+ token = stream.current();
+ if (token.is("=")) {
+ stream.next();
+ for (String fieldName: skipValue(stream)) {
+ fieldDef = new FieldDef();
+ int index = fieldName.indexOf('[');
+ if (index != -1) {
+ fieldDef.type = typeRef + fieldName.substring(index);
+ fieldDef.name = fieldName.substring(0, index);
+ } else {
+ fieldDef.type = typeRef;
+ fieldDef.name = fieldName;
+ }
+ fieldDef.modifiers = modifiers;
+ fieldDefs.add(fieldDef);
+ }
+ token = stream.current();
+ }
+ if (token.is(","))
+ token = stream.next();
+ }
+ return fieldDefs;
+ }
+
+ /*
+ * Define a type.
+ *
+ * @before-token: 'class', 'interface', 'enum', or '@interface'
+ * @after-token: '}'
+ */
+ private TypeDef defineType(TokenStream stream, List<Modifier> modifiers) {
+ TypeDef typeDef = new TypeDef();
+ typeDef.modifiers = modifiers;
+ Token token = stream.current();
+ if (token.is("@") && stream.lookAhead(1).is("interface")) {
+ typeDef.kind = TypeDef.Kind.ANNOTATION;
+ stream.next(); // 'interface'
+ stream.next(); // identifier
+ defineTypeHead(stream, typeDef);
+ defineTypeBody(stream, typeDef);
+ return typeDef;
+ } else if (token.is("class")) {
+ typeDef.kind = TypeDef.Kind.CLASS;
+ stream.next(); // identifier
+ defineTypeHead(stream, typeDef);
+ defineTypeBody(stream, typeDef);
+ return typeDef;
+ } else if (token.is("interface")) {
+ typeDef.kind = TypeDef.Kind.INTERFACE;
+ stream.next(); // identifier
+ defineTypeHead(stream, typeDef);
+ defineTypeBody(stream, typeDef);
+ return typeDef;
+ } else {
+ typeDef.kind = TypeDef.Kind.ENUM;
+ stream.next(); // identifier
+ defineTypeHead(stream, typeDef);
+
+ // process enum constants
+ token = stream.next();
+ while (true) {
+ if (token.is(";", "}") || token.isEof()) {
+ break;
+ } else if (token.is(",")) {
+ token = stream.next();
+ } else {
+ FieldDef fieldDef = new FieldDef();
+ skipModifiers(stream); // skip annotations
+ fieldDef.modifiers = Lists.newArrayList(Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL);
+ fieldDef.name = stream.current().text();
+ token = stream.next();
+ if (token.is("(")) { // enum constant arguments
+ stream.nextBalanced(token);
+ token = stream.next();
+ }
+ if (token.is("{")) { // enum constant class body
+ stream.nextBalanced(token);
+ token = stream.next();
+ }
+ typeDef.fieldDefs.add(fieldDef);
+ }
+ }
+
+ if (token.is(";"))
+ defineTypeBody(stream, typeDef);
+
+ return typeDef;
+ }
+ }
+
+ /*
+ * Populate typeDef with identifier and then skip to type body.
+ *
+ * @before-token: type identifier
+ * @after-token: start of type body which is '{'
+ */
+ private void defineTypeHead(TokenStream stream, TypeDef typeDef) {
+ Token token = stream.current();
+ typeDef.name = token.text();
+
+ while (true) {
+ token = stream.nextSymbol("{", "(");
+ if (token.is("(")) {
+ stream.nextBalanced(token);
+ token = stream.next();
+ } else {
+ break;
+ }
+ }
+ }
+
+ /*
+ * This method skips possible modifiers from current stream position.
+ *
+ * @before-token: possible start of modifiers
+ * @after-token: remain unchanged or token after the modifiers if there are modifiers
+ */
+ private List<Modifier> skipModifiers(TokenStream stream) {
+ List<Modifier> modifiers = new ArrayList<>();
+ Token token = stream.current();
+ while (true) {
+ if (token.is("@") && !stream.lookAhead(1).is("interface")) {
+ skipAnnotation(stream);
+ token = stream.current();
+ } else if (!token.isComment() && !token.isString()) {
+ Modifier modifier = null;
+ for (Modifier each: Modifier.values()) {
+ if (each.name().toLowerCase().equals(token.text())) {
+ modifier = each;
+ break;
+ }
+ }
+ if (modifier != null) {
+ modifiers.add(modifier);
+ token = stream.next();
+ } else {
+ break;
+ }
+ } else {
+ break;
+ }
+ }
+ return modifiers;
+ }
+
+ /*
+ * Skip a single annotation.
+ *
+ * @before-token: '@'
+ * @after-token: token after the annotation
+ */
+ private void skipAnnotation(TokenStream stream) {
+ stream.next();
+ skipTypeName(stream);
+ Token token = stream.current();
+ if (token.is("(")) {
+ token = stream.nextBalanced(token);
+ token = stream.next();
+ }
+ }
+
+ /*
+ * Skip type name sections.
+ *
+ * @before-token: first section of type name
+ * @after-token: token after type name
+ */
+ private String skipTypeName(TokenStream stream) {
+ String typeName = stream.current().text();
+ Token token = stream.next();
+ while (token.is(".")) {
+ typeName += ".";
+ token = stream.next();
+ typeName += token.text();
+ token = stream.next();
+ }
+ return typeName;
+ }
+
+}
+
diff --git a/commons.lang/src/main/java/com/pmease/commons/lang/analyzers/java/JavaOutline.java b/commons.lang/src/main/java/com/pmease/commons/lang/analyzers/java/JavaOutline.java
new file mode 100644
index 0000000000..34427bb669
--- /dev/null
+++ b/commons.lang/src/main/java/com/pmease/commons/lang/analyzers/java/JavaOutline.java
@@ -0,0 +1,29 @@
+package com.pmease.commons.lang.analyzers.java;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.annotation.Nullable;
+
+import com.pmease.commons.lang.Outline;
+
+public class JavaOutline implements Outline {
+
+ @Nullable
+ public String packageName;
+
+ public List<TypeDef> typeDefs = new ArrayList<>();
+
+ @Override
+ public String toString() {
+ StringBuilder builder = new StringBuilder();
+ if (packageName != null)
+ builder.append("package ").append(packageName).append(";\n\n");
+
+ for (TypeDef typeDef: typeDefs)
+ builder.append(typeDef).append("\n\n");
+
+ return builder.toString();
+ }
+
+}
diff --git a/commons.lang/src/main/java/com/pmease/commons/lang/analyzers/java/MethodDef.java b/commons.lang/src/main/java/com/pmease/commons/lang/analyzers/java/MethodDef.java
new file mode 100644
index 0000000000..22594de5bd
--- /dev/null
+++ b/commons.lang/src/main/java/com/pmease/commons/lang/analyzers/java/MethodDef.java
@@ -0,0 +1,37 @@
+package com.pmease.commons.lang.analyzers.java;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.annotation.Nullable;
+
+public class MethodDef {
+
+ public String name;
+
+ @Nullable
+ public String returnType; // null for constructor
+
+ @Nullable
+ public String params;
+
+ public int line;
+
+ public List<Modifier> modifiers = new ArrayList<>();
+
+ @Override
+ public String toString() {
+ StringBuilder builder = new StringBuilder();
+ for (Modifier modifier: modifiers)
+ builder.append(modifier.name().toLowerCase()).append(" ");
+ if (returnType != null)
+ builder.append(returnType).append(" ");
+ builder.append(name);
+ if (params != null)
+ builder.append("(").append(params).append(")");
+ else
+ builder.append("()");
+ return builder.toString();
+ }
+
+}
diff --git a/commons.lang/src/main/java/com/pmease/commons/lang/analyzers/java/Modifier.java b/commons.lang/src/main/java/com/pmease/commons/lang/analyzers/java/Modifier.java
new file mode 100644
index 0000000000..1350f2252e
--- /dev/null
+++ b/commons.lang/src/main/java/com/pmease/commons/lang/analyzers/java/Modifier.java
@@ -0,0 +1,6 @@
+package com.pmease.commons.lang.analyzers.java;
+
+public enum Modifier {
+ PUBLIC, PRIVATE, PROTECTED, STATIC, ABSTRACT, STRICTFP, FINAL,
+ SYNCHRONIZED, NATIVE, TRANSIENT, VOLATILE, DEFAULT;
+}
diff --git a/commons.lang/src/main/java/com/pmease/commons/lang/analyzers/java/TypeDef.java b/commons.lang/src/main/java/com/pmease/commons/lang/analyzers/java/TypeDef.java
new file mode 100644
index 0000000000..a622d7fa14
--- /dev/null
+++ b/commons.lang/src/main/java/com/pmease/commons/lang/analyzers/java/TypeDef.java
@@ -0,0 +1,55 @@
+package com.pmease.commons.lang.analyzers.java;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class TypeDef {
+
+ public enum Kind {
+ CLASS,
+ INTERFACE,
+ ANNOTATION {
+
+ @Override
+ public String toString() {
+ return "@interface";
+ }
+
+ },
+ ENUM
+ };
+
+ public String name;
+
+ public Kind kind;
+
+ public List<FieldDef> fieldDefs = new ArrayList<>();
+
+ public List<MethodDef> methodDefs = new ArrayList<>();
+
+ public List<TypeDef> typeDefs = new ArrayList<>();
+
+ public int line;
+
+ public List<Modifier> modifiers = new ArrayList<>();
+
+ @Override
+ public String toString() {
+ StringBuilder builder = new StringBuilder();
+ for (Modifier modifier: modifiers)
+ builder.append(modifier.name().toLowerCase()).append(" ");
+ builder.append(kind.toString().toLowerCase()).append(" ");
+ builder.append(name).append(" {\n\n");
+ for (FieldDef fieldDef: fieldDefs)
+ builder.append("\t").append(fieldDef).append("\n\n");
+ for (MethodDef methodDef: methodDefs)
+ builder.append("\t").append(methodDef).append("\n\n");
+ for (TypeDef typeDef: typeDefs)
+ builder.append("\t").append(typeDef).append("\n\n");
+
+ builder.append("}");
+
+ return builder.toString();
+ }
+
+}
diff --git a/commons.lang/src/test/java/com/pmease/commons/lang/TokenStreamTest.java b/commons.lang/src/test/java/com/pmease/commons/lang/TokenStreamTest.java
index b677f64e70..b69fcdc30d 100644
--- a/commons.lang/src/test/java/com/pmease/commons/lang/TokenStreamTest.java
+++ b/commons.lang/src/test/java/com/pmease/commons/lang/TokenStreamTest.java
@@ -1,12 +1,11 @@
package com.pmease.commons.lang;
+import java.util.ArrayList;
import java.util.List;
import org.junit.Assert;
import org.junit.Test;
-import com.pmease.commons.lang.TokenStream;
-import com.pmease.commons.lang.TokenizedLine;
import com.pmease.commons.lang.tokenizers.clike.JavaTokenizer;
public class TokenStreamTest {
@@ -17,9 +16,9 @@ public class TokenStreamTest {
+ " public void sayHello() {System.out.println(\"hello\");}"
+ "}";
- List<TokenizedLine> tokenizedLines = new JavaTokenizer().tokenize(text);
- TokenStream tokenStream = new TokenStream(tokenizedLines, false);
- Assert.assertEquals("}", tokenStream.nextBalanced(tokenStream.next("{")).text());
+ TokenStream tokenStream = new TokenStream(getTokens(text));
+
+ Assert.assertEquals("}", tokenStream.nextBalanced(tokenStream.nextSymbol("{")).text());
}
@Test
@@ -27,9 +26,21 @@ public class TokenStreamTest {
String text = "public class MyClass {"
+ " public void sayHello() {System.out.println(\"hello\");}";
- List<TokenizedLine> tokenizedLines = new JavaTokenizer().tokenize(text);
- TokenStream tokenStream = new TokenStream(tokenizedLines, false);
- Assert.assertTrue(tokenStream.nextBalanced(tokenStream.next("{")).isEof());
+ TokenStream tokenStream = new TokenStream(getTokens(text));
+ Assert.assertTrue(tokenStream.nextBalanced(tokenStream.nextSymbol("{")).isEof());
+ }
+
+ private List<LineAwareToken> getTokens(String text) {
+ List<LineAwareToken> tokens = new ArrayList<>();
+ int linePos = 0;
+ for (TokenizedLine line: new JavaTokenizer().tokenize(text)) {
+ for (Token token: line.getTokens()) {
+ if (!token.isComment() && !token.isWhitespace())
+ tokens.add(new LineAwareToken(token, linePos));
+ }
+ linePos++;
+ }
+ return tokens;
}
}
diff --git a/commons.lang/src/test/java/com/pmease/commons/lang/analyzers/java/JavaAnalyzerTest.java b/commons.lang/src/test/java/com/pmease/commons/lang/analyzers/java/JavaAnalyzerTest.java
new file mode 100644
index 0000000000..468f298408
--- /dev/null
+++ b/commons.lang/src/test/java/com/pmease/commons/lang/analyzers/java/JavaAnalyzerTest.java
@@ -0,0 +1,28 @@
+package com.pmease.commons.lang.analyzers.java;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.List;
+
+import org.apache.commons.io.FileUtils;
+import org.junit.Test;
+
+import com.pmease.commons.lang.TokenizedLine;
+import com.pmease.commons.lang.tokenizers.clike.JavaTokenizer;
+
+public class JavaAnalyzerTest {
+
+ @Test
+ public void test() {
+ try {
+ String text = FileUtils.readFileToString(new File("W:\\java8\\test\\src\\com\\example\\Test.java"));
+ List<TokenizedLine> lines = new JavaTokenizer().tokenize(text);
+ System.out.println(new JavaAnalyzer().analyze(lines));
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+
+ }
+
+}
diff --git a/gitplex.core/src/main/java/com/pmease/gitplex/core/model/PullRequest.java b/gitplex.core/src/main/java/com/pmease/gitplex/core/model/PullRequest.java
index c02928a941..442198a8a1 100644
--- a/gitplex.core/src/main/java/com/pmease/gitplex/core/model/PullRequest.java
+++ b/gitplex.core/src/main/java/com/pmease/gitplex/core/model/PullRequest.java
@@ -104,7 +104,7 @@ public class PullRequest extends AbstractEntity {
public String toString() {
return displayName;
}
-
+
}
public enum Event {
diff --git a/gitplex.product/src/test/java/gitplex/product/Test.java b/gitplex.product/src/test/java/gitplex/product/Test.java
index 12cee6402a..498de65372 100644
--- a/gitplex.product/src/test/java/gitplex/product/Test.java
+++ b/gitplex.product/src/test/java/gitplex/product/Test.java
@@ -1,29 +1,11 @@
package gitplex.product;
-import java.io.File;
import java.io.IOException;
-import java.util.List;
-
-import org.apache.commons.io.FileUtils;
-
-import com.pmease.commons.lang.TokenStream;
-import com.pmease.commons.lang.TokenizedLine;
-import com.pmease.commons.lang.tokenizers.clike.JavaTokenizer;
public class Test {
@org.junit.Test
public void test() throws IOException {
- JavaTokenizer java = new JavaTokenizer();
-
- List<String> lines = FileUtils.readLines(new File("w:\\temp\\Component.java"));
- for (int i=0; i<100; i++) {
- long time = System.currentTimeMillis();
- List<TokenizedLine> tokenizedLines = java.tokenize(lines);
- TokenStream tokenStream = new TokenStream(tokenizedLines, false);
- System.out.println(tokenStream.nextBalanced(tokenStream.next("{")));
- System.out.println(System.currentTimeMillis()-time);
- }
}
}