Skip to content

Commit 0207dd0

Browse files
committed
Minor performance improvement
1 parent c341cf2 commit 0207dd0

3 files changed

Lines changed: 25 additions & 27 deletions

File tree

java-squid/src/main/java/org/sonar/java/ast/visitors/SyntaxHighlighterVisitor.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,10 @@ private static List<Integer> startLines(File file, Charset charset) {
170170
throw Throwables.propagate(e);
171171
}
172172
startLines.add(0);
173-
for (int i = 0; i < content.length(); i++) {
174-
if (content.charAt(i) == '\n' || (content.charAt(i) == '\r' && i + 1 < content.length() && content.charAt(i + 1) != '\n')) {
173+
int contentLength = content.length();
174+
for (int i = 0; i < contentLength; i++) {
175+
char currentChar = content.charAt(i);
176+
if (currentChar == '\n' || (currentChar == '\r' && i + 1 < contentLength && content.charAt(i + 1) != '\n')) {
175177
startLines.add(i + 1);
176178
}
177179
}

java-squid/src/main/java/org/sonar/java/resolve/JavaSymbol.java

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@ Scope completedMembers() {
291291
*/
292292
public static class TypeJavaSymbol extends JavaSymbol implements TypeSymbol {
293293

294+
private String fullyQualifiedName;
294295
Scope members;
295296
Scope typeParameters;
296297
List<JavaType.TypeVariableJavaType> typeVariableTypes;
@@ -348,19 +349,22 @@ public Scope typeParameters() {
348349
}
349350

350351
public String getFullyQualifiedName() {
351-
String newQualification = "";
352-
if (owner.isPackageSymbol()) {
353-
if (!owner.name.isEmpty()) {
354-
newQualification = owner.name + ".";
352+
if(fullyQualifiedName == null) {
353+
String newQualification = "";
354+
if (owner.isPackageSymbol()) {
355+
if (!owner.name.isEmpty()) {
356+
newQualification = owner.name + ".";
357+
}
358+
} else if (owner.isTypeSymbol()) {
359+
newQualification = owner.type.fullyQualifiedName() + "$";
360+
} else if (owner.isMethodSymbol()) {
361+
newQualification = owner.owner.type().fullyQualifiedName() + "$";
362+
} else {
363+
throw new IllegalStateException("" + owner);
355364
}
356-
} else if (owner.isTypeSymbol()) {
357-
newQualification = owner.type.fullyQualifiedName() + "$";
358-
} else if (owner.isMethodSymbol()) {
359-
newQualification = owner.owner.type().fullyQualifiedName() + "$";
360-
} else {
361-
throw new IllegalStateException("" + owner);
365+
fullyQualifiedName = newQualification + getInternalName();
362366
}
363-
return newQualification + getInternalName();
367+
return fullyQualifiedName;
364368
}
365369

366370
/**

java-squid/src/main/java/org/sonar/java/se/checks/NullDereferenceCheck.java

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,13 @@ public ProgramState checkPreStatement(CheckerContext context, Tree syntaxNode) {
6464
// stack is empty, nothing to do.
6565
return context.getState();
6666
}
67-
if (syntaxNode.is(Tree.Kind.MEMBER_SELECT)) {
68-
return checkMemberSelect(context, (MemberSelectExpressionTree) syntaxNode, currentVal);
69-
}
67+
Tree toCheck = syntaxNode;
7068
if (syntaxNode.is(Tree.Kind.METHOD_INVOCATION)) {
7169
MethodInvocationTree methodInvocation = (MethodInvocationTree) syntaxNode;
72-
if (methodInvocation.methodSelect().is(Tree.Kind.MEMBER_SELECT)) {
73-
return checkMemberSelect(context, (MemberSelectExpressionTree) methodInvocation.methodSelect(), currentVal);
74-
}
70+
toCheck = methodInvocation.methodSelect();
71+
}
72+
if (toCheck.is(Tree.Kind.MEMBER_SELECT)) {
73+
return checkMemberSelect(context, (MemberSelectExpressionTree) toCheck, currentVal);
7574
}
7675
return context.getState();
7776
}
@@ -86,10 +85,6 @@ private ProgramState checkMemberSelect(CheckerContext context, MemberSelectExpre
8685
context.reportIssue(syntaxNode, this, "NullPointerException might be thrown as '" + SyntaxTreeNameFinder.getName(syntaxNode) + "' is nullable here");
8786
return null;
8887
}
89-
if (context.getState().getConstraint(currentVal) == null) {
90-
// we dereferenced the symbolic value so we can assume it is not null
91-
return currentVal.setSingleConstraint(context.getState(), ObjectConstraint.NOT_NULL);
92-
}
9388
return context.getState();
9489
}
9590

@@ -109,10 +104,7 @@ public ProgramState checkPostStatement(CheckerContext context, Tree syntaxNode)
109104

110105
private static List<ProgramState> setNullConstraint(CheckerContext context, Tree syntaxNode) {
111106
SymbolicValue val = context.getState().peekValue();
112-
if (syntaxNode.is(Tree.Kind.NULL_LITERAL)) {
113-
// invariant to check that value was correctly evaluated.
114-
assert val != null && val.equals(SymbolicValue.NULL_LITERAL);
115-
} else if (syntaxNode.is(Tree.Kind.METHOD_INVOCATION) && isAnnotatedCheckForNull((MethodInvocationTree) syntaxNode)) {
107+
if (syntaxNode.is(Tree.Kind.METHOD_INVOCATION) && isAnnotatedCheckForNull((MethodInvocationTree) syntaxNode)) {
116108
List<ProgramState> states = new ArrayList<>();
117109
states.addAll(val.setConstraint(context.getState(), ObjectConstraint.NULL));
118110
states.addAll(val.setConstraint(context.getState(), ObjectConstraint.NOT_NULL));

0 commit comments

Comments
 (0)