guru.nidi.codeassert.model.CodeClass Maven / Gradle / Ivy
/*
* Copyright © 2015 Stefan Niederhauser ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package guru.nidi.codeassert.model;
import guru.nidi.codeassert.config.LocationMatcher;
import guru.nidi.codeassert.util.CountSet;
import java.lang.reflect.Modifier;
import java.util.*;
import static java.util.Collections.emptyList;
/**
* The JavaClass
class represents a Java
* class or interface.
*
* @author Mike Clark
* @author Clarkware Consulting, Inc.
*/
public class CodeClass extends UsingElement {
private final String name;
private final CodePackage pack;
private final CountSet usedPackages;
private final CountSet usedClasses;
private final Set annotations;
final List fields = new ArrayList<>();
final List methods = new ArrayList<>();
String superClass;
String sourceFile;
int codeSize;
int totalSize;
int flags;
int sourceSize;
int codeLines;
int commentLines;
int emptyLines;
int totalLines;
CodeClass(String name, CodePackage pack) {
this.name = name;
this.pack = pack;
usedPackages = new CountSet<>();
usedClasses = new CountSet<>();
annotations = new HashSet<>();
sourceFile = "Unknown";
}
public boolean isParsed() {
return superClass != null;
}
public List getMembers() {
final List members = new ArrayList<>();
members.addAll(methods);
members.addAll(fields);
return members;
}
public String getName() {
return name;
}
public String getSimpleName() {
return name.substring(pack.getName().length() + 1);
}
public CodePackage getPackage() {
return pack;
}
public String getSuperClass() {
return superClass;
}
public String getSourceFile() {
return sourceFile;
}
public Set getAnnotations() {
return annotations;
}
public List getFields() {
return fields;
}
public List getMethods() {
return methods;
}
public int getCodeSize() {
return codeSize;
}
public int getTotalSize() {
return totalSize;
}
public boolean isConcrete() {
return !Modifier.isAbstract(flags) && !Modifier.isInterface(flags);
}
public int getSourceSize() {
return sourceSize;
}
public int getCodeLines() {
return codeLines;
}
public int getCommentLines() {
return commentLines;
}
public int getEmptyLines() {
return emptyLines;
}
public int getTotalLines() {
return totalLines;
}
@Override
public CodeClass self() {
return this;
}
public Collection usedForeignPackages() {
final Set res = new HashSet<>(usedPackages());
res.remove(pack);
return res;
}
public Collection usedPackages() {
return usedPackages.asSet();
}
public Map usedPackageCounts() {
return usedPackages.asMap();
}
public Collection usedClasses() {
return usedClasses.asSet();
}
public Map usedClassCounts() {
return usedClasses.asMap();
}
public boolean uses(CodePackage pack) {
return usedPackages.contains(pack);
}
@Override
public Collection uses() {
return usedClasses();
}
@Override
public String getPackageName() {
return pack.getName();
}
@Override
public Collection usedVia(UsingElement other) {
return emptyList();
}
@Override
public boolean isMatchedBy(LocationMatcher matcher) {
return matcher.matchesClass(name);
}
void addImport(String type, Model model) {
if (!name.equals(type)) {
final CodeClass clazz = model.getOrCreateClass(type);
if (clazz != null) {
final String packName = model.packageOf(type);
final CodePackage p = model.getOrCreatePackage(packName);
usedPackages.add(p);
pack.addEfferent(p);
usedClasses.add(clazz);
}
}
}
void addAnnotation(String type, Model model, Collection annotations) {
final CodeClass clazz = model.getOrCreateClass(type);
if (clazz != null) {
addImport(type, model);
annotations.add(clazz);
}
}
public boolean equals(Object other) {
if (other instanceof CodeClass) {
final CodeClass otherClass = (CodeClass) other;
return otherClass.getName().equals(getName());
}
return false;
}
public int hashCode() {
return getName().hashCode();
}
@Override
public String toString() {
return name;
}
}