Skip to content

The Unified and Autonomous Development Platform

Add comment to various symbol extraction classes

robin shine919c929abaca
diff --git a/commons.lang/src/main/java/com/gitplex/commons/lang/extractors/Symbol.java b/commons.lang/src/main/java/com/gitplex/commons/lang/extractors/Symbol.java
index 56498ec507..ebf4ae01a9 100644
--- a/commons.lang/src/main/java/com/gitplex/commons/lang/extractors/Symbol.java
+++ b/commons.lang/src/main/java/com/gitplex/commons/lang/extractors/Symbol.java
@@ -1,7 +1,6 @@
package com.gitplex.commons.lang.extractors;
import java.io.Serializable;
-import java.util.List;
import javax.annotation.Nullable;
@@ -10,6 +9,12 @@ import org.apache.wicket.request.resource.ResourceReference;
import com.gitplex.commons.util.Range;
+/**
+ * Represents a symbol extracted from source file
+ *
+ * @author robin
+ *
+ */
public abstract class Symbol implements Serializable {
private static final long serialVersionUID = 1L;
@@ -20,6 +25,20 @@ public abstract class Symbol implements Serializable {
private final TokenPosition pos;
+ /**
+ * Construct a symbol
+ *
+ * @param parent
+ * parent symbol, use <tt>null</tt> if there is no parent
+ * @param name
+ * name of the symbol to be indexed for search and cross referencing.
+ * Use <tt>null</tt> if the symbol is only for outline purpose, and
+ * does not need to be indexed for search and cross referenced. For
+ * instance a Java package should be displayed in outline, but does
+ * not need to be indexed
+ * @param pos
+ * position of the symbol in source file
+ */
public Symbol(@Nullable Symbol parent, @Nullable String name, TokenPosition pos) {
this.parent = parent;
this.name = name;
@@ -34,24 +53,47 @@ public abstract class Symbol implements Serializable {
* Get name of this symbol.
*
* @return
- * name of this symbol, or <tt>null</tt> if this symbol
- * does not have a name
+ * name of this symbol for indexing, or <tt>null</tt> if this symbol
+ * does not need to be indexed
*/
@Nullable
public String getName() {
return name;
}
-
+
+ /**
+ * Get position of the symbol in source file
+ *
+ * @return
+ * position of the symbol in source file
+ */
public TokenPosition getPos() {
return pos;
}
- public abstract Component render(String componentId, Range range);
+ /**
+ * Render the symbol in web UI
+ *
+ * @param componentId
+ * wicket component id corresponding to this symbol
+ * @param highlightRange
+ * range of the name to be highlighted if not <tt>null</tt>. For instance,
+ * when user searches for a symbol, this range will be used to highlight
+ * the matched search in the symbol name
+ *
+ * @return
+ * a wicket component to be displayed in web UI for the symbol
+ */
+ public abstract Component render(String componentId, @Nullable Range highlightRange);
+ /**
+ * Get icon of the symbol to be displayed in web UI
+ *
+ * @return
+ * icon of the symbol
+ */
public abstract ResourceReference getIcon();
- public abstract String describe(List<Symbol> symbols);
-
public int score() {
int relevance = 1;
Symbol parent = this.parent;
@@ -63,9 +105,26 @@ public abstract class Symbol implements Serializable {
return relevance*name.length();
}
+ /**
+ * Get scope of the symbol. Scope is used to show to the user under which scope the
+ * symbol is. For instance, A method &quot;sayHello()&quot; defined in Java class
+ * &quot;com.example.HelloWorld&quot; will return the scope as
+ * &quot;com.example.HelloWorld&quot;. Return <tt>null</tt> if the symbol does not
+ * have a scope
+ * @return
+ */
@Nullable
public abstract String getScope();
+ /**
+ * Whether or not this symbol is a primary symbol. Primary symbol will be searched
+ * before non-primary symbols. For instance, a Java symbol search will match
+ * against type symbols (class, enumeration, interface, annotation etc.) first
+ * before matching other symbols such as fields and methods.
+ *
+ * @return
+ * whether or not this symbol is primary
+ */
public abstract boolean isPrimary();
}
diff --git a/commons.lang/src/main/java/com/gitplex/commons/lang/extractors/Token.java b/commons.lang/src/main/java/com/gitplex/commons/lang/extractors/Token.java
index 8f6ebc5eee..ed6111ec6a 100644
--- a/commons.lang/src/main/java/com/gitplex/commons/lang/extractors/Token.java
+++ b/commons.lang/src/main/java/com/gitplex/commons/lang/extractors/Token.java
@@ -2,8 +2,16 @@ package com.gitplex.commons.lang.extractors;
import java.io.Serializable;
+import javax.annotation.Nullable;
+
import com.gitplex.commons.util.Range;
+/**
+ * A simplified version of ANTLR lexer token
+ *
+ * @author robin
+ *
+ */
public class Token implements Serializable {
private static final long serialVersionUID = 1L;
@@ -16,6 +24,12 @@ public class Token implements Serializable {
private final TokenPosition pos;
+ /**
+ * Constructing the token from ANTLR lexer token
+ *
+ * @param antlrToken
+ * the ANTLR lexer token
+ */
public Token(org.antlr.v4.runtime.Token antlrToken) {
type = antlrToken.getType();
text = antlrToken.getText();
@@ -24,16 +38,42 @@ public class Token implements Serializable {
pos = new TokenPosition(antlrToken.getLine()-1, range);
}
- public Token(int type, String text, TokenPosition pos) {
+ /**
+ * Construct the token with type, text and position
+ *
+ * @param type
+ * type of the token, normally it uses token type generated by ANTLR lexer
+ * @param text
+ * text of the token
+ * @param pos
+ * position of the token, use <tt>null</tt> for EOF token
+ */
+ public Token(int type, String text, @Nullable TokenPosition pos) {
this.type = type;
this.text = text;
this.pos = pos;
}
+ /**
+ * Check if this token is of specified type
+ *
+ * @param type
+ * ANTLR lexer token type to check
+ * @return
+ * whether or not this token is of specifed type
+ */
public boolean is(int type) {
return this.type == type;
}
+ /**
+ * Check if this token is of any specified types
+ *
+ * @param types
+ * ANTLR lexer token types to check
+ * @return
+ * whether or not this token is of specified types
+ */
public boolean is(int...types) {
for (int type: types) {
if (this.type == type)
@@ -42,32 +82,80 @@ public class Token implements Serializable {
return false;
}
+ /**
+ * Check if the token text equals specified text
+ *
+ * @param text
+ * text to compare
+ * @return
+ * whether or not the token text equals specified text
+ */
public boolean is(String text) {
return text.equals(this.text);
}
+ /**
+ * Is this token the EOF token
+ *
+ * @return
+ * whether or not the token is EOF token
+ */
public boolean isEof() {
return type == org.antlr.v4.runtime.Token.EOF;
}
+ /**
+ * Get ANTLR lexer token type
+ *
+ * @return
+ * ANTLR lexer token type
+ */
public int getType() {
return type;
}
+ /**
+ * Get text of the token
+ *
+ * @return
+ * text of the token
+ */
public String getText() {
return text;
}
+ /**
+ * Get position of the token in source file
+ *
+ * @return
+ * position of the token in source file
+ */
public TokenPosition getPos() {
return pos;
}
+ /**
+ * check if the token is of specified types
+ *
+ * @return
+ * token to be checked
+ * @throws
+ * UnexpectedTokenException if token is not of expected types
+ */
public Token checkType(int... expectedTypes) {
if (!is(expectedTypes))
throw new UnexpectedTokenException(this);
return this;
}
+ /**
+ * check if the token text equals specified text
+ *
+ * @return
+ * token to be checked
+ * @throws
+ * UnexpectedTokenException if token text does not equal to expected text
+ */
public Token checkText(String expectedText) {
if (!text.equals(expectedText))
throw new UnexpectedTokenException(this);
diff --git a/commons.lang/src/main/java/com/gitplex/commons/lang/extractors/TokenFilter.java b/commons.lang/src/main/java/com/gitplex/commons/lang/extractors/TokenFilter.java
index b7f1ea6e56..bf85c9edc7 100644
--- a/commons.lang/src/main/java/com/gitplex/commons/lang/extractors/TokenFilter.java
+++ b/commons.lang/src/main/java/com/gitplex/commons/lang/extractors/TokenFilter.java
@@ -2,7 +2,14 @@ package com.gitplex.commons.lang.extractors;
import org.antlr.v4.runtime.Token;
+/**
+ * This class represents a ANTLR token filter
+ *
+ * @author robin
+ *
+ */
public interface TokenFilter {
+
boolean accept(Token token);
public static final TokenFilter ALL = new TokenFilter() {
diff --git a/commons.lang/src/main/java/com/gitplex/commons/lang/extractors/TokenPosition.java b/commons.lang/src/main/java/com/gitplex/commons/lang/extractors/TokenPosition.java
index fce84c8c22..0c2ae34a58 100644
--- a/commons.lang/src/main/java/com/gitplex/commons/lang/extractors/TokenPosition.java
+++ b/commons.lang/src/main/java/com/gitplex/commons/lang/extractors/TokenPosition.java
@@ -6,6 +6,12 @@ import javax.annotation.Nullable;
import com.gitplex.commons.util.Range;
+/**
+ * Represent position of token in source file
+ *
+ * @author robin
+ *
+ */
public class TokenPosition implements Serializable {
private static final long serialVersionUID = 1L;
@@ -14,15 +20,35 @@ public class TokenPosition implements Serializable {
private final Range range;
+ /**
+ * Construct with line and range
+ *
+ * @param line
+ * 0-indexed line of the token
+ * @param range
+ * 0-indexed character position of the token in the line
+ */
public TokenPosition(int line, @Nullable Range range) {
this.line = line;
this.range = range;
}
+ /**
+ * Get line of the token
+ *
+ * @return
+ * 0-indexed line of the token
+ */
public int getLine() {
return line;
}
+ /**
+ * Get character position of the token in the line
+ *
+ * @return
+ * 0-indexed character position of the token in the line
+ */
@Nullable
public Range getRange() {
return range;
diff --git a/commons.lang/src/main/java/com/gitplex/commons/lang/extractors/TokenStream.java b/commons.lang/src/main/java/com/gitplex/commons/lang/extractors/TokenStream.java
index 6a0da55091..52d98bcd5a 100644
--- a/commons.lang/src/main/java/com/gitplex/commons/lang/extractors/TokenStream.java
+++ b/commons.lang/src/main/java/com/gitplex/commons/lang/extractors/TokenStream.java
@@ -10,13 +10,20 @@ import org.antlr.v4.runtime.Recognizer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+/**
+ * Token stream holds a list of tokens parsed from source file, and provides various
+ * methods to work with these tokens
+ *
+ * @author robin
+ *
+ */
public class TokenStream {
private static final Logger logger = LoggerFactory.getLogger(TokenStream.class);
private final List<Token> tokens;
- private int pos = -1; // 0-indexed position of current token
+ private int index = -1; // index of current token in stream
public TokenStream(List<Token> tokens) {
this.tokens = tokens;
@@ -44,25 +51,73 @@ public class TokenStream {
tokens.add(Token.EOF);
}
- public Token at(int pos) {
- if (pos >=0 && pos < tokens.size())
- return tokens.get(pos);
+ /**
+ * Get token at specified index in stream
+ *
+ * @param index
+ * index of the token in stream
+ * @return
+ * token at specified index
+ * @throws
+ * ExtractException if index is invalid
+ */
+ public Token at(int index) {
+ if (index >=0 && index < tokens.size())
+ return tokens.get(index);
else
- throw new ExtractException("Invalid token position: " + pos);
+ throw new ExtractException("Invalid token position: " + index);
}
+ /**
+ * Get next token in stream and move index forward
+ *
+ * @return
+ * next token in stream
+ * @throws
+ * ExtractException if no token at next index
+ */
public Token next() {
- return at(++pos);
+ return at(++index);
}
+ /**
+ * Get next N token in stream without moving index
+ *
+ * @param ahead
+ * number of tokens to look ahead
+ * @return
+ * next N token in stream
+ * @throws
+ * ExtractException if no token at specified look ahead index
+ */
public Token lookAhead(int ahead) {
- return at(pos+ahead);
+ return at(index+ahead);
}
+ /**
+ * Get previous N token in stream without moving index
+ *
+ * @param behind
+ * number of tokens to look behind
+ * @return
+ * previous N token in stream
+ * @throws
+ * ExtractException if no token at specified look behind index
+ */
public Token lookBehind(int behind) {
- return at(pos-behind);
+ return at(index-behind);
}
+ /**
+ * Get next token of specified type and move index forward
+ *
+ * @param type
+ * ANTLR lexer token type
+ * @return
+ * next token of specified type
+ * @throws
+ * ExtractException if no subsequent token matches specified type
+ */
public Token nextType(int type) {
Token token = next();
while(true) {
@@ -72,6 +127,16 @@ public class TokenStream {
}
}
+ /**
+ * Get next token of specified types and move index forward
+ *
+ * @param types
+ * ANTLR lexer token types
+ * @return
+ * next token of specified types
+ * @throws
+ * ExtractException if no subsequent token matches specified types
+ */
public Token nextType(int...types) {
Token token = next();
while(true) {
@@ -84,15 +149,20 @@ public class TokenStream {
/**
* Seek to next closed type, with balancing open/close considered, that is, all open/close pair
* of the same type will be skipped. Below is an example of start position and stop position
- * when calling this method.
+ * when calling this method.
*
* text { text { text } }
* ^ ^
* seek here stop here
*
* @param openType
+ * open type to seek
* @param closeType
+ * close type to seek
* @return
+ * token at stop position
+ * @throws
+ * ExtractException if no stop token can be found
*/
public Token nextClosed(int openType, int closeType) {
int nestingLevel = 1;
@@ -108,37 +178,63 @@ public class TokenStream {
}
}
- public int pos() {
- return pos;
+ /**
+ * Get current index in stream
+ *
+ * @return
+ * current index in stream
+ */
+ public int index() {
+ return index;
}
- public void pos(int pos) {
- this.pos = pos;
+ /**
+ * Set current index in stream
+ *
+ * @param index
+ * index to set
+ */
+ public void index(int index) {
+ this.index = index;
}
/**
- * Get tokens between startPos and endPos
+ * Get tokens between a range
*
- * @param startPos
- * 0-indexed start position, inclusive
- * @param endPos
- * 0-indexed end position, inclusive
+ * @param startIndex
+ * 0-indexed start index, inclusive
+ * @param endIndex
+ * 0-indexed end index, inclusive
* @return
- * list of tokens between startPos and endPos
+ * list of tokens between specified range
+ * @throws
+ * ExtractException if no token at specified range
*/
- public List<Token> between(int startPos, int endPos) {
+ public List<Token> between(int startIndex, int endIndex) {
List<Token> tokens = new ArrayList<>();
- for (int i=startPos; i<=endPos; i++)
+ for (int i=startIndex; i<=endIndex; i++)
tokens.add(at(i));
return tokens;
}
+ /**
+ * Get token at current index
+ *
+ * @return
+ * token at current index
+ */
public Token current() {
- return at(pos);
+ return at(index);
}
+ /**
+ * Seek to specified index
+ *
+ * @param index
+ * index to seek to
+ */
public void seek(int index) {
- this.pos = index;
+ this.index = index;
}
}
diff --git a/commons.lang/src/main/java/com/gitplex/commons/lang/extractors/java/CompilationUnit.java b/commons.lang/src/main/java/com/gitplex/commons/lang/extractors/java/CompilationUnit.java
index 1ce571c5e2..0dcf09e812 100644
--- a/commons.lang/src/main/java/com/gitplex/commons/lang/extractors/java/CompilationUnit.java
+++ b/commons.lang/src/main/java/com/gitplex/commons/lang/extractors/java/CompilationUnit.java
@@ -1,14 +1,11 @@
package com.gitplex.commons.lang.extractors.java;
-import java.util.List;
-
import javax.annotation.Nullable;
import org.apache.wicket.Component;
import org.apache.wicket.request.resource.PackageResourceReference;
import org.apache.wicket.request.resource.ResourceReference;
-import com.gitplex.commons.lang.extractors.Symbol;
import com.gitplex.commons.lang.extractors.TokenPosition;
import com.gitplex.commons.lang.extractors.java.icons.Icons;
import com.gitplex.commons.util.Range;
@@ -26,15 +23,6 @@ public class CompilationUnit extends JavaSymbol {
this.packageName = packageName;
}
- @Override
- public String describe(List<Symbol> symbols) {
- StringBuilder builder = new StringBuilder();
- if (packageName != null)
- builder.append("package ").append(packageName).append(";\n\n");
-
- return builder.toString();
- }
-
@Nullable
public String getPackageName() {
return packageName;
diff --git a/commons.lang/src/main/java/com/gitplex/commons/lang/extractors/java/FieldDef.java b/commons.lang/src/main/java/com/gitplex/commons/lang/extractors/java/FieldDef.java
index ade9889f8a..6bb061cfdd 100644
--- a/commons.lang/src/main/java/com/gitplex/commons/lang/extractors/java/FieldDef.java
+++ b/commons.lang/src/main/java/com/gitplex/commons/lang/extractors/java/FieldDef.java
@@ -8,7 +8,6 @@ import org.apache.wicket.Component;
import org.apache.wicket.request.resource.PackageResourceReference;
import org.apache.wicket.request.resource.ResourceReference;
-import com.gitplex.commons.lang.extractors.Symbol;
import com.gitplex.commons.lang.extractors.TokenPosition;
import com.gitplex.commons.lang.extractors.java.icons.Icons;
import com.gitplex.commons.util.Range;
@@ -49,17 +48,6 @@ public class FieldDef extends JavaSymbol {
return new FieldDefPanel(componentId, this, matchRange);
}
- @Override
- public String describe(List<Symbol> symbols) {
- StringBuilder builder = new StringBuilder();
- for (Modifier modifier: modifiers)
- builder.append(modifier.name().toLowerCase()).append(" ");
- if (type != null)
- builder.append(type).append(" ");
- builder.append(getName()).append(";");
- return builder.toString();
- }
-
@Override
public ResourceReference getIcon() {
String icon;
diff --git a/commons.lang/src/main/java/com/gitplex/commons/lang/extractors/java/JavaExtractor.java b/commons.lang/src/main/java/com/gitplex/commons/lang/extractors/java/JavaExtractor.java
index 2b39d195c4..898dbb21ff 100644
--- a/commons.lang/src/main/java/com/gitplex/commons/lang/extractors/java/JavaExtractor.java
+++ b/commons.lang/src/main/java/com/gitplex/commons/lang/extractors/java/JavaExtractor.java
@@ -285,9 +285,9 @@ public class JavaExtractor extends AbstractExtractor {
String identifier = stream.current().getText();
Token token = stream.next();
if (token.is(LT)) {
- int tokenPos = stream.pos();
+ int tokenPos = stream.index();
stream.nextClosed(LT, GT);
- TokenStream typeArgStream = new TokenStream(stream.between(tokenPos, stream.pos()));
+ TokenStream typeArgStream = new TokenStream(stream.between(tokenPos, stream.index()));
token = typeArgStream.next();
while (true) {
skipModifiers(typeArgStream);
diff --git a/commons.lang/src/main/java/com/gitplex/commons/lang/extractors/java/MethodDef.java b/commons.lang/src/main/java/com/gitplex/commons/lang/extractors/java/MethodDef.java
index 16420cca99..8fc103169f 100644
--- a/commons.lang/src/main/java/com/gitplex/commons/lang/extractors/java/MethodDef.java
+++ b/commons.lang/src/main/java/com/gitplex/commons/lang/extractors/java/MethodDef.java
@@ -8,7 +8,6 @@ import org.apache.wicket.Component;
import org.apache.wicket.request.resource.PackageResourceReference;
import org.apache.wicket.request.resource.ResourceReference;
-import com.gitplex.commons.lang.extractors.Symbol;
import com.gitplex.commons.lang.extractors.TokenPosition;
import com.gitplex.commons.lang.extractors.java.icons.Icons;
import com.gitplex.commons.util.Range;
@@ -63,21 +62,6 @@ public class MethodDef extends JavaSymbol {
return new MethodDefPanel(componentId, this, matchRange);
}
- @Override
- public String describe(List<Symbol> symbols) {
- StringBuilder builder = new StringBuilder();
- for (Modifier modifier: modifiers)
- builder.append(modifier.name().toLowerCase()).append(" ");
- if (type != null)
- builder.append(type).append(" ");
- builder.append(getName());
- if (params != null)
- builder.append("(").append(params).append(");");
- else
- builder.append("();");
- return builder.toString();
- }
-
@Override
public ResourceReference getIcon() {
String icon;
diff --git a/commons.lang/src/main/java/com/gitplex/commons/lang/extractors/java/TypeDef.java b/commons.lang/src/main/java/com/gitplex/commons/lang/extractors/java/TypeDef.java
index fada47b352..adabe49a6b 100644
--- a/commons.lang/src/main/java/com/gitplex/commons/lang/extractors/java/TypeDef.java
+++ b/commons.lang/src/main/java/com/gitplex/commons/lang/extractors/java/TypeDef.java
@@ -1,6 +1,5 @@
package com.gitplex.commons.lang.extractors.java;
-import java.util.ArrayList;
import java.util.List;
import javax.annotation.Nullable;
@@ -14,8 +13,6 @@ import com.gitplex.commons.lang.extractors.TokenPosition;
import com.gitplex.commons.lang.extractors.java.icons.Icons;
import com.gitplex.commons.util.Range;
import com.gitplex.commons.wicket.component.EmphasizeAwareLabel;
-import com.google.common.base.Joiner;
-import com.google.common.base.Splitter;
public class TypeDef extends JavaSymbol {
@@ -55,59 +52,6 @@ public class TypeDef extends JavaSymbol {
return packageName;
}
- @Override
- public String describe(List<Symbol> symbols) {
- StringBuilder builder = new StringBuilder();
- for (Modifier modifier: modifiers)
- builder.append(modifier.name().toLowerCase()).append(" ");
-
- if (kind == Kind.ANNOTATION)
- builder.append("@interface").append(" ");
- else
- builder.append(kind.toString().toLowerCase()).append(" ");
- builder.append(getName()).append(" {\n\n");
-
- List<String> enumConstants = new ArrayList<>();
- for (Symbol symbol: symbols) {
- if (symbol.getParent() == this && (symbol instanceof FieldDef)) {
- FieldDef fieldDef = (FieldDef) symbol;
- if (fieldDef.getType() == null)
- enumConstants.add(fieldDef.getName());
- }
- }
- if (!enumConstants.isEmpty())
- builder.append(" ").append(Joiner.on(", ").join(enumConstants)).append(";\n\n");
- else if (kind == Kind.ENUM)
- builder.append(" ;\n\n");
-
- for (Symbol symbol: symbols) {
- if (symbol.getParent() == this && (symbol instanceof FieldDef)) {
- FieldDef fieldDef = (FieldDef) symbol;
- if (fieldDef.getType() != null)
- builder.append(" ").append(fieldDef.describe(symbols)).append("\n\n");
- }
- }
-
- for (Symbol symbol: symbols) {
- if (symbol.getParent() == this && (symbol instanceof MethodDef)) {
- MethodDef methodDef = (MethodDef) symbol;
- builder.append(" ").append(methodDef.describe(symbols)).append("\n\n");
- }
- }
-
- for (Symbol symbol: symbols) {
- if (symbol.getParent() == this && (symbol instanceof TypeDef)) {
- TypeDef typeDef = (TypeDef) symbol;
- for (String line: Splitter.on('\n').omitEmptyStrings().split(typeDef.describe(symbols)))
- builder.append(" ").append(line).append("\n\n");
- }
- }
-
- builder.append("}\n\n");
-
- return builder.toString();
- }
-
@Override
public String getScope() {
if (getParent() != null) {
diff --git a/commons.lang/src/test/java/com/gitplex/commons/lang/extractors/ExtractorTest.java b/commons.lang/src/test/java/com/gitplex/commons/lang/extractors/ExtractorTest.java
new file mode 100644
index 0000000000..587b23cecb
--- /dev/null
+++ b/commons.lang/src/test/java/com/gitplex/commons/lang/extractors/ExtractorTest.java
@@ -0,0 +1,53 @@
+package com.gitplex.commons.lang.extractors;
+
+import java.io.IOException;
+import java.nio.charset.Charset;
+import java.util.List;
+
+import org.apache.commons.lang3.StringUtils;
+import org.junit.Assert;
+
+import com.gitplex.commons.lang.extractors.Symbol;
+import com.google.common.io.Resources;
+
+public abstract class ExtractorTest {
+
+ /**
+ * Describe specified symbol in specified context
+ *
+ * @param context
+ * context to describe the symbol in. Non-leaf symbols will use context to describe
+ * itself and all its child symbols. For instance, a Java class symbol description
+ * contains not only the class definition, but also all declaration symbols inside
+ * the class including fields and methods. In this case, it will look inside the
+ * context for symbols with parent pointing to it
+ * @param symbol
+ * symbol to be described. Non-leaf symbol should also describe its child symbols
+ *
+ * @return
+ * Description of the symbol
+ */
+ protected abstract String describe(List<Symbol> context, Symbol symbol);
+
+ /**
+ * This method describes the list of symbols into a string and compares it with expected string
+ */
+ protected void assertSymbol(String expected, List<Symbol> symbols) {
+ StringBuilder builder = new StringBuilder();
+ for (Symbol symbol: symbols) {
+ if (symbol.getParent() == null)
+ builder.append(describe(symbols, symbol));
+ }
+
+ Assert.assertEquals(StringUtils.replace(expected, "\r", "").trim(), builder.toString().toString().trim());
+ }
+
+ protected String readFile(String fileName) {
+ try {
+ return Resources.toString(Resources.getResource(getClass(), fileName), Charset.forName("UTF8"));
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+}
diff --git a/commons.lang/src/test/java/com/gitplex/commons/lang/extractors/java/TokenStreamTest.java b/commons.lang/src/test/java/com/gitplex/commons/lang/extractors/TokenStreamTest.java
similarity index 96%
rename from commons.lang/src/test/java/com/gitplex/commons/lang/extractors/java/TokenStreamTest.java
rename to commons.lang/src/test/java/com/gitplex/commons/lang/extractors/TokenStreamTest.java
index 616b230388..16dbaa048b 100644
--- a/commons.lang/src/test/java/com/gitplex/commons/lang/extractors/java/TokenStreamTest.java
+++ b/commons.lang/src/test/java/com/gitplex/commons/lang/extractors/TokenStreamTest.java
@@ -1,4 +1,4 @@
-package com.gitplex.commons.lang.extractors.java;
+package com.gitplex.commons.lang.extractors;
import static org.junit.Assert.fail;
diff --git a/commons.lang/src/test/java/com/gitplex/commons/lang/extractors/java/ExtractorTest.java b/commons.lang/src/test/java/com/gitplex/commons/lang/extractors/java/ExtractorTest.java
deleted file mode 100644
index 16e8e511a2..0000000000
--- a/commons.lang/src/test/java/com/gitplex/commons/lang/extractors/java/ExtractorTest.java
+++ /dev/null
@@ -1,33 +0,0 @@
-package com.gitplex.commons.lang.extractors.java;
-
-import java.io.IOException;
-import java.nio.charset.Charset;
-import java.util.List;
-
-import org.apache.commons.lang3.StringUtils;
-import org.junit.Assert;
-
-import com.gitplex.commons.lang.extractors.Symbol;
-import com.google.common.io.Resources;
-
-public class ExtractorTest {
-
- protected void assertSymbol(String expected, List<Symbol> symbols) {
- StringBuilder builder = new StringBuilder();
- for (Symbol symbol: symbols) {
- if (symbol.getParent() == null)
- builder.append(symbol.describe(symbols));
- }
-
- Assert.assertEquals(StringUtils.replace(expected, "\r", "").trim(), builder.toString().toString().trim());
- }
-
- protected String readFile(String fileName) {
- try {
- return Resources.toString(Resources.getResource(getClass(), fileName), Charset.forName("UTF8"));
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
-
-}
diff --git a/commons.lang/src/test/java/com/gitplex/commons/lang/extractors/java/JavaExtractorTest.java b/commons.lang/src/test/java/com/gitplex/commons/lang/extractors/java/JavaExtractorTest.java
index 92aae51353..11c5f11088 100644
--- a/commons.lang/src/test/java/com/gitplex/commons/lang/extractors/java/JavaExtractorTest.java
+++ b/commons.lang/src/test/java/com/gitplex/commons/lang/extractors/java/JavaExtractorTest.java
@@ -1,8 +1,16 @@
package com.gitplex.commons.lang.extractors.java;
+import java.util.ArrayList;
+import java.util.List;
+
import org.junit.Test;
+import com.gitplex.commons.lang.extractors.ExtractorTest;
+import com.gitplex.commons.lang.extractors.Symbol;
import com.gitplex.commons.lang.extractors.java.JavaExtractor;
+import com.gitplex.commons.lang.extractors.java.TypeDef.Kind;
+import com.google.common.base.Joiner;
+import com.google.common.base.Splitter;
public class JavaExtractorTest extends ExtractorTest {
@@ -30,4 +38,91 @@ public class JavaExtractorTest extends ExtractorTest {
new JavaExtractor().extract(readFile("Resource.src")));
}
+ @Override
+ protected String describe(List<Symbol> context, Symbol symbol) {
+ if (symbol instanceof CompilationUnit) {
+ CompilationUnit compilationUnit = (CompilationUnit) symbol;
+ StringBuilder builder = new StringBuilder();
+ if (compilationUnit.getPackageName() != null)
+ builder.append("package ").append(compilationUnit.getPackageName()).append(";\n\n");
+
+ return builder.toString();
+ } else if (symbol instanceof TypeDef) {
+ TypeDef typeDef = (TypeDef) symbol;
+ StringBuilder builder = new StringBuilder();
+ for (Modifier modifier: typeDef.getModifiers())
+ builder.append(modifier.name().toLowerCase()).append(" ");
+
+ if (typeDef.getKind() == Kind.ANNOTATION)
+ builder.append("@interface").append(" ");
+ else
+ builder.append(typeDef.getKind().toString().toLowerCase()).append(" ");
+ builder.append(typeDef.getName()).append(" {\n\n");
+
+ List<String> enumConstants = new ArrayList<>();
+ for (Symbol each: context) {
+ if (each.getParent() == symbol && (each instanceof FieldDef)) {
+ FieldDef fieldDef = (FieldDef) each;
+ if (fieldDef.getType() == null)
+ enumConstants.add(fieldDef.getName());
+ }
+ }
+ if (!enumConstants.isEmpty())
+ builder.append(" ").append(Joiner.on(", ").join(enumConstants)).append(";\n\n");
+ else if (typeDef.getKind() == Kind.ENUM)
+ builder.append(" ;\n\n");
+
+ for (Symbol each: context) {
+ if (each.getParent() == symbol && (each instanceof FieldDef)) {
+ FieldDef fieldDef = (FieldDef) each;
+ if (fieldDef.getType() != null)
+ builder.append(" ").append(describe(context, fieldDef)).append("\n\n");
+ }
+ }
+
+ for (Symbol each: context) {
+ if (each.getParent() == symbol && (each instanceof MethodDef)) {
+ MethodDef methodDef = (MethodDef) each;
+ builder.append(" ").append(describe(context, methodDef)).append("\n\n");
+ }
+ }
+
+ for (Symbol each: context) {
+ if (each.getParent() == symbol && (each instanceof TypeDef)) {
+ TypeDef subTypeDef = (TypeDef) each;
+ for (String line: Splitter.on('\n').omitEmptyStrings().split(describe(context, subTypeDef)))
+ builder.append(" ").append(line).append("\n\n");
+ }
+ }
+
+ builder.append("}\n\n");
+
+ return builder.toString();
+ } else if (symbol instanceof FieldDef) {
+ FieldDef fieldDef = (FieldDef) symbol;
+ StringBuilder builder = new StringBuilder();
+ for (Modifier modifier: fieldDef.getModifiers())
+ builder.append(modifier.name().toLowerCase()).append(" ");
+ if (fieldDef.getType() != null)
+ builder.append(fieldDef.getType()).append(" ");
+ builder.append(fieldDef.getName()).append(";");
+ return builder.toString();
+ } else if (symbol instanceof MethodDef) {
+ MethodDef methodDef = (MethodDef) symbol;
+ StringBuilder builder = new StringBuilder();
+ for (Modifier modifier: methodDef.getModifiers())
+ builder.append(modifier.name().toLowerCase()).append(" ");
+ if (methodDef.getType() != null)
+ builder.append(methodDef.getType()).append(" ");
+ builder.append(methodDef.getName());
+ if (methodDef.getParams() != null)
+ builder.append("(").append(methodDef.getParams()).append(");");
+ else
+ builder.append("();");
+ return builder.toString();
+ } else {
+ throw new RuntimeException("Unexpected symbol type: " + symbol.getClass());
+ }
+ }
+
}