File JGitRepository.java
has 302 lines of code (exceeds 250 allowed). Consider refactoring. Open
/*
* This code is free software; you can redistribute it and/or modify it under
* the terms of the new BSD License.
*
* Copyright (c) 2012-2020, Sebastian Staudt
JGitRepository
has 23 methods (exceeds 20 allowed). Consider refactoring. Open
public class JGitRepository extends AbstractGitRepository {
static final String COMMONDIR_FILE = "commondir";
static final Pattern DESCRIBE_PATTERN = Pattern.compile("(.*)-([1-9][0-9]*)-g([0-9a-f]+)$");
static final String GITDIR_FILE = "gitdir";
Method buildRepository
has a Cognitive Complexity of 12 (exceeds 5 allowed). Consider refactoring. Open
final void buildRepository(File workTree, File gitDir) throws GitRepositoryException {
if (gitDir == null && workTree == null) {
throw new GitRepositoryException("Neither worktree nor GIT_DIR is set.");
} else {
if (workTree != null && !workTree.exists()) {
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Method resolveLinkedWorkTree
has a Cognitive Complexity of 10 (exceeds 5 allowed). Consider refactoring. Open
private void resolveLinkedWorkTree(File workTree, File foundGitDir, FileRepositoryBuilder repositoryBuilder) throws IOException {
if (directoryContains(foundGitDir.getParentFile(), workTree)) {
repositoryBuilder.setGitDir(foundGitDir);
repositoryBuilder.setWorkTree(foundGitDir.getParentFile());
} else {
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Method describe
has 27 lines of code (exceeds 25 allowed). Consider refactoring. Open
@Override
public GitTagDescription describe() throws GitRepositoryException {
DescribeCommand command = getDescribeCommand();
try {
Method getTags
has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring. Open
@Override
public Map<String, GitTag> getTags()
throws GitRepositoryException {
Map<String, GitTag> tags = new HashMap<>();
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Method describe
has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring. Open
@Override
public GitTagDescription describe() throws GitRepositoryException {
DescribeCommand command = getDescribeCommand();
try {
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Overridable method 'getRepositoryBuilder' called during object construction Open
buildRepository(workTree, gitDir);
- Read upRead up
- Exclude checks
ConstructorCallsOverridableMethod
Since: PMD 1.04
Priority: High
Categories: Style
Remediation Points: 50000
Calling overridable methods during construction poses a risk of invoking methods on an incompletely constructed object and can be difficult to debug. It may leave the sub-class unable to construct its superclass or forced to replicate the construction process completely within itself, losing the ability to call super(). If the default constructor contains a call to an overridable method, the subclass may be completely uninstantiable. Note that this includes method calls throughout the control flow graph - i.e., if a constructor Foo() calls a private method bar() that calls a public method buz(), this denotes a problem.
Example:
public class SeniorClass {
public SeniorClass(){
toString(); //may throw NullPointerException if overridden
}
public String toString(){
return 'IAmSeniorClass';
}
}
public class JuniorClass extends SeniorClass {
private String name;
public JuniorClass(){
super(); //Automatic call leads to NullPointerException
name = 'JuniorClass';
}
public String toString(){
return name.toUpperCase();
}
}
Avoid instantiating new objects inside loops Open
tags.put(object.getName(), new JGitTag(revTag));
- Read upRead up
- Exclude checks
AvoidInstantiatingObjectsInLoops
Since: PMD 2.2
Priority: Medium
Categories: Style
Remediation Points: 50000
New objects created within loops should be checked to see if they can created outside them and reused.
Example:
public class Something {
public static void main( String as[] ) {
for (int i = 0; i < 10; i++) {
Foo f = new Foo(); // Avoid this whenever you can it's really expensive
}
}
}
Document empty constructor Open
JGitRepository() {}
- Read upRead up
- Exclude checks
UncommentedEmptyConstructor
Since: PMD 3.4
Priority: Medium
Categories: Style
Remediation Points: 50000
Uncommented Empty Constructor finds instances where a constructor does not contain statements, but there is no comment. By explicitly commenting empty constructors it is easier to distinguish between intentional (commented) and unintentional empty constructors.
Example:
public Foo() {
// This constructor is intentionally empty. Nothing special is needed here.
}
Avoid instantiating new objects inside loops Open
action.execute(new JGitCommit(commit));
- Read upRead up
- Exclude checks
AvoidInstantiatingObjectsInLoops
Since: PMD 2.2
Priority: Medium
Categories: Style
Remediation Points: 50000
New objects created within loops should be checked to see if they can created outside them and reused.
Example:
public class Something {
public static void main( String as[] ) {
for (int i = 0; i < 10; i++) {
Foo f = new Foo(); // Avoid this whenever you can it's really expensive
}
}
}
Avoid excessively long variable names like originalGitDirPath Open
String originalGitDirPath = readFileToString(originalGitDirFile, UTF_8).trim();
- Read upRead up
- Exclude checks
LongVariable
Since: PMD 0.3
Priority: Medium
Categories: Style
Remediation Points: 50000
Fields, formal arguments, or local variable names that are too long can make the code difficult to follow.
Example:
public class Something {
int reallyLongIntName = -3; // VIOLATION - Field
public static void main( String argumentsList[] ) { // VIOLATION - Formal
int otherReallyLongName = -5; // VIOLATION - Local
for (int interestingIntIndex = 0; // VIOLATION - For
interestingIntIndex < 10;
interestingIntIndex ++ ) {
}
}
New exception is thrown in catch block, original stack trace may be lost Open
throw new GitRepositoryException(e.getMessage());
- Read upRead up
- Exclude checks
PreserveStackTrace
Since: PMD 3.7
Priority: Medium
Categories: Style
Remediation Points: 50000
Throwing a new exception from a catch block without passing the original exception into the new exception will cause the original stack trace to be lost making it difficult to debug effectively.
Example:
public class Foo {
void good() {
try{
Integer.parseInt('a');
} catch (Exception e) {
throw new Exception(e); // first possibility to create exception chain
}
try {
Integer.parseInt('a');
} catch (Exception e) {
throw (IllegalStateException)new IllegalStateException().initCause(e); // second possibility to create exception chain.
}
}
void bad() {
try{
Integer.parseInt('a');
} catch (Exception e) {
throw new Exception(e.getMessage());
}
}
}
Avoid excessively long variable names like originalGitDirFile Open
File originalGitDirFile = new File(foundGitDir, GITDIR_FILE);
- Read upRead up
- Exclude checks
LongVariable
Since: PMD 0.3
Priority: Medium
Categories: Style
Remediation Points: 50000
Fields, formal arguments, or local variable names that are too long can make the code difficult to follow.
Example:
public class Something {
int reallyLongIntName = -3; // VIOLATION - Field
public static void main( String argumentsList[] ) { // VIOLATION - Formal
int otherReallyLongName = -5; // VIOLATION - Local
for (int interestingIntIndex = 0; // VIOLATION - For
interestingIntIndex < 10;
interestingIntIndex ++ ) {
}
}