1
0
mirror of https://github.com/chylex/Code-Statistics.git synced 2025-03-15 18:15:42 +01:00

Add a Java annotation use counter that uses an event in JavaCodeParser

This commit is contained in:
chylex 2016-03-06 00:59:53 +01:00
parent e22d49b161
commit fb73309037
2 changed files with 31 additions and 2 deletions
CodeStatistics/Handling/Languages/Java

View File

@ -6,6 +6,10 @@ using CodeStatistics.Handling.Languages.Java.Utils;
namespace CodeStatistics.Handling.Languages.Java{
public class JavaCodeParser : CodeParser{
public delegate void OnAnnotationRead(Annotation annotation);
public event OnAnnotationRead AnnotationCallback;
public JavaCodeParser(string code) : base(code){
IsWhiteSpace = JavaCharacters.IsWhiteSpace;
IsIdentifierStart = JavaCharacters.IsIdentifierStart;
@ -14,7 +18,9 @@ namespace CodeStatistics.Handling.Languages.Java{
}
public override CodeParser Clone(string newCode = null){
return new JavaCodeParser(newCode ?? string.Empty);
return new JavaCodeParser(newCode ?? string.Empty){
AnnotationCallback = this.AnnotationCallback
};
}
/// <summary>
@ -107,7 +113,10 @@ namespace CodeStatistics.Handling.Languages.Java{
SkipBlock('(',')');
}
return new Annotation(simpleName);
Annotation annotation = new Annotation(simpleName);
if (AnnotationCallback != null)AnnotationCallback(annotation);
return annotation;
}
/// <summary>

View File

@ -3,17 +3,27 @@ using System.Collections.Generic;
using CodeStatistics.Handling.Languages.Java.Elements;
using CodeStatistics.Handling.Languages.Java.Utils;
using CodeStatistics.Collections;
using System.Linq;
namespace CodeStatistics.Handling.Languages.Java{
class JavaState{
private readonly Dictionary<File,JavaFileInfo> fileInfo = new Dictionary<File,JavaFileInfo>();
private readonly HashSet<string> packages = new HashSet<string>();
private readonly Dictionary<string,int> annotationUses = new Dictionary<string,int>();
public readonly TopElementList<TypeIdentifier> IdentifiersSimpleTop = new TopElementList<TypeIdentifier>(10,(x,y) => y.Name.Length-x.Name.Length);
public readonly TopElementList<TypeIdentifier> IdentifiersSimpleBottom = new TopElementList<TypeIdentifier>(10,(x,y) => x.Name.Length-y.Name.Length);
public readonly TopElementList<TypeIdentifier> IdentifiersFullTop = new TopElementList<TypeIdentifier>(10,(x,y) => y.FullName.Length-x.FullName.Length);
public readonly TopElementList<TypeIdentifier> IdentifiersFullBottom = new TopElementList<TypeIdentifier>(10,(x,y) => x.FullName.Length-y.FullName.Length);
public List<KeyValuePair<string,int>> AnnotationUses{
get{
List<KeyValuePair<string,int>> list = annotationUses.ToList();
list.Sort((kvp1, kvp2) => kvp2.Value-kvp1.Value);
return list;
}
}
public int PackageCount { get { return packages.Count; } }
public JavaFileInfo GetFile(File file){
@ -25,6 +35,7 @@ namespace CodeStatistics.Handling.Languages.Java{
fileInfo.Add(file,info);
JavaCodeParser parser = new JavaCodeParser(JavaParseUtils.PrepareCodeFile(file.Contents));
parser.AnnotationCallback += IncrementAnnotation;
ReadPackage(parser,info);
ReadImportList(parser,info);
@ -39,6 +50,15 @@ namespace CodeStatistics.Handling.Languages.Java{
packages.Add(info.Package);
}
private void IncrementAnnotation(Annotation annotation){
if (annotationUses.ContainsKey(annotation.SimpleName)){
++annotationUses[annotation.SimpleName];
}
else{
annotationUses[annotation.SimpleName] = 1;
}
}
private static void ReadPackage(JavaCodeParser parser, JavaFileInfo info){
parser.SkipSpaces();