Skip to content

Commit

Permalink
[performance] final JavaElement members
Browse files Browse the repository at this point in the history
* moves IProjectNature from JavaProject to dedicated JavaNature class
* caches org.eclipse.jdt.internal.core.JavaElement.hashCode
  • Loading branch information
EcljpseB0T authored and jukzi committed Jan 30, 2024
1 parent fe45b5e commit 4869388
Show file tree
Hide file tree
Showing 56 changed files with 526 additions and 674 deletions.
Expand Up @@ -532,7 +532,7 @@ public void testExternalJarChange() throws JavaModelException, IOException {
env.removeProject(projectPath);
}

public void testMissingBuilder() throws JavaModelException {
public void testMissingBuilder() throws Exception {
IPath project1Path = env.addProject("P1"); //$NON-NLS-1$
env.addExternalJars(project1Path, Util.getJavaClassLibs());

Expand All @@ -558,13 +558,9 @@ public void testMissingBuilder() throws JavaModelException {

env.addRequiredProject(project2Path, project1Path);

try {
JavaProject p = (JavaProject) env.getJavaProject(project1Path);
p.deconfigure();
JavaModelManager.getJavaModelManager().setLastBuiltState(p.getProject(), null);
} catch (CoreException e) {
e.printStackTrace();
}
JavaProject p = (JavaProject) env.getJavaProject(project1Path);
p.getProject().getNature(JavaCore.NATURE_ID).deconfigure();
JavaModelManager.getJavaModelManager().setLastBuiltState(p.getProject(), null);

env.addClass(project2Path, "", "SubTest", //$NON-NLS-1$ //$NON-NLS-2$
"public class SubTest extends Test {}" //$NON-NLS-1$
Expand Down
Expand Up @@ -670,7 +670,7 @@ private void append(IType type) throws JavaModelException {
}
if (isLocal && !(type instanceof LambdaExpression)) { // don't want occurrence counts for lambdas. it can be confusing at best, as not all are built.
this.line.append("#");
this.line.append(((SourceRefElement)type).occurrenceCount);
this.line.append(((SourceRefElement)type).getOccurrenceCount());
}
}
protected IJavaElement getElement(SearchMatch searchMatch) {
Expand Down
Expand Up @@ -3528,7 +3528,7 @@ public void testBug94389() throws CoreException {
// Verify that all matches have correct occurence count
int size = collector.matches.size();
for (int i=0; i<size; i++) {
assertEquals("Invalid foo method occurence count (m="+(m+1)+")", i+1, ((SourceMethod) methods[i]).occurrenceCount);
assertEquals("Invalid foo method occurence count (m="+(m+1)+")", i+1, ((SourceMethod) methods[i]).getOccurrenceCount());
}
}
}
Expand Down
Expand Up @@ -195,12 +195,12 @@ public class OverflowingTestOpenable extends Openable {
*/
public boolean isOpen;

public OverflowingTestBuffer buffer;
public final OverflowingTestBuffer buffer;

/**
* The cache this element is stored in
*/
public ElementCache<OverflowingTestOpenable> cache;
public final ElementCache<OverflowingTestOpenable> cache;

/**
* Constructs a new openable, with unsaved changes as specified,
Expand Down
Expand Up @@ -17,7 +17,6 @@

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.internal.compiler.env.IElementInfo;
import org.eclipse.jdt.internal.compiler.lookup.Binding;
Expand All @@ -39,9 +38,7 @@ public IElementInfo getElementInfo(IProgressMonitor monitor) throws JavaModelExc
}

@Override
public IType getType(String typeName, int count) {
AssistSourceType type = new AssistSourceType(this, typeName, this.bindingCache, this.infoCache);
type.occurrenceCount = count;
return type;
public AssistSourceType getType(String typeName, int count) {
return new AssistSourceType(this, typeName, this.bindingCache, this.infoCache, count);
}
}
Expand Up @@ -16,9 +16,7 @@
import java.util.Map;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jdt.core.IAnnotation;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.internal.compiler.env.IElementInfo;
import org.eclipse.jdt.internal.compiler.lookup.Binding;
Expand Down Expand Up @@ -75,14 +73,12 @@ protected void toStringInfo(int tab, StringBuilder buffer, Object info,boolean s
}

@Override
public IAnnotation getAnnotation(String annotationName) {
public AssistAnnotation getAnnotation(String annotationName) {
return new AssistAnnotation(this, annotationName, this.infoCache);
}

@Override
public IType getType(String typeName, int count) {
AssistSourceType type = new AssistSourceType(this, typeName, this.bindingCache, this.infoCache);
type.occurrenceCount = count;
return type;
public AssistSourceType getType(String typeName, int count) {
return new AssistSourceType(this, typeName, this.bindingCache, this.infoCache, count);
}
}
Expand Up @@ -16,10 +16,7 @@
import java.util.Map;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jdt.core.IAnnotation;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.ITypeParameter;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.internal.compiler.env.IElementInfo;
import org.eclipse.jdt.internal.compiler.lookup.Binding;
Expand Down Expand Up @@ -76,19 +73,17 @@ protected void toStringInfo(int tab, StringBuilder buffer, Object info,boolean s
}

@Override
public IAnnotation getAnnotation(String annotationName) {
public AssistAnnotation getAnnotation(String annotationName) {
return new AssistAnnotation(this, annotationName, this.infoCache);
}

@Override
public IType getType(String typeName, int count) {
AssistSourceType type = new AssistSourceType(this, typeName, this.bindingCache, this.infoCache);
type.occurrenceCount = count;
return type;
public AssistSourceType getType(String typeName, int count) {
return new AssistSourceType(this, typeName, this.bindingCache, this.infoCache, count);
}

@Override
public ITypeParameter getTypeParameter(String typeParameterName) {
public AssistTypeParameter getTypeParameter(String typeParameterName) {
return new AssistTypeParameter(this, typeParameterName, this.infoCache);
}
}
Expand Up @@ -16,13 +16,8 @@
import java.util.Map;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jdt.core.IAnnotation;
import org.eclipse.jdt.core.IField;
import org.eclipse.jdt.core.IInitializer;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IMethod;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.ITypeParameter;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.internal.compiler.env.IElementInfo;
import org.eclipse.jdt.internal.compiler.lookup.Binding;
Expand All @@ -37,7 +32,11 @@ public class AssistSourceType extends ResolvedSourceType {
private boolean isResolved;

public AssistSourceType(JavaElement parent, String name, Map<JavaElement, Binding> bindingCache, Map<IJavaElement, IElementInfo> infoCache) {
super(parent, name, null);
this(parent, name, bindingCache, infoCache, 1);
}

public AssistSourceType(JavaElement parent, String name, Map<JavaElement, Binding> bindingCache, Map<IJavaElement, IElementInfo> infoCache, int occurrenceCount) {
super(parent, name, null, occurrenceCount);
this.bindingCache = bindingCache;
this.infoCache = infoCache;
}
Expand Down Expand Up @@ -87,22 +86,22 @@ protected void toStringInfo(int tab, StringBuilder buffer, Object info,boolean s
}

@Override
public IAnnotation getAnnotation(String annotationName) {
public AssistAnnotation getAnnotation(String annotationName) {
return new AssistAnnotation(this, annotationName, this.infoCache);
}

@Override
public IField getField(String fieldName) {
public AssistSourceField getField(String fieldName) {
return new AssistSourceField(this, fieldName, this.bindingCache, this.infoCache);
}

@Override
public IInitializer getInitializer(int count) {
public AssistInitializer getInitializer(int count) {
return new AssistInitializer(this, count, this.bindingCache, this.infoCache);
}

@Override
public IMethod getMethod(String selector, String[] parameterTypeSignatures) {
public AssistSourceMethod getMethod(String selector, String[] parameterTypeSignatures) {
return new AssistSourceMethod(this, selector, parameterTypeSignatures, this.bindingCache, this.infoCache);
}

Expand All @@ -112,14 +111,12 @@ public IType getType(String typeName) {
}

@Override
public IType getType(String typeName, int count) {
AssistSourceType type = new AssistSourceType(this, typeName, this.bindingCache, this.infoCache);
type.occurrenceCount = count;
return type;
public AssistSourceType getType(String typeName, int count) {
return new AssistSourceType(this, typeName, this.bindingCache, this.infoCache, count);
}

@Override
public ITypeParameter getTypeParameter(String typeParameterName) {
public AssistTypeParameter getTypeParameter(String typeParameterName) {
return new AssistTypeParameter(this, typeParameterName, this.infoCache);
}
}
Expand Up @@ -351,7 +351,7 @@ private ASTParser createParser(int kind) {
}

private ICompilationUnit createDummyModuleInfoCompilationUnit() {
JavaProject dummyProject = new JavaProject() {
JavaProject dummyProject = new JavaProject(null, null) {
@Override
public Map<String, String> getOptions(boolean inheritJavaCoreOptions) {
return new HashMap<>();
Expand Down
Expand Up @@ -43,7 +43,7 @@
*/
public abstract class AbstractClassFile extends Openable implements IClassFile, SuffixConstants {

protected String name;
protected final String name;

protected AbstractClassFile(PackageFragment parent, String nameWithoutExtension) {
super(parent);
Expand Down Expand Up @@ -128,10 +128,13 @@ protected ClassFileInfo createElementInfo() {
}
@Override
public boolean equals(Object o) {
if (!(o instanceof AbstractClassFile)) return false;
AbstractClassFile other = (AbstractClassFile) o;
if (!(o instanceof AbstractClassFile other)) return false;
return this.name.equals(other.name) && this.getParent().equals(other.getParent());
}
@Override
protected int calculateHashCode() {
return Util.combineHashCodes(this.name.hashCode(), this.getParent().hashCode());
}

/**
* Finds the deepest <code>IJavaElement</code> in the hierarchy of
Expand Down Expand Up @@ -393,10 +396,7 @@ public IJavaElement getWorkingCopy(IProgressMonitor monitor, org.eclipse.jdt.cor
protected boolean hasBuffer() {
return true;
}
@Override
public int hashCode() {
return Util.combineHashCodes(this.name.hashCode(), this.getParent().hashCode());
}

/**
* Returns true - class files are always read only.
*/
Expand Down
Expand Up @@ -29,26 +29,28 @@ public class Annotation extends SourceRefElement implements IAnnotation {
public static final IAnnotation[] NO_ANNOTATIONS = new IAnnotation[0];
public static final IMemberValuePair[] NO_MEMBER_VALUE_PAIRS = new IMemberValuePair[0];

protected String name;
protected final String name;
// require to distinguish same annotations in different member value pairs
protected String memberValuePairName;
protected final String memberValuePairName;

public Annotation(JavaElement parent, String name) {
this(parent, name, null);
}

public Annotation(JavaElement parent, String name, String memberValuePairName) {
super(parent);
this(parent, name, memberValuePairName, 1);
}

public Annotation(JavaElement parent, String name, String memberValuePairName, int occurrenceCount) {
super(parent, occurrenceCount);
this.name = name.intern();
this.memberValuePairName = memberValuePairName;
}

@Override
public boolean equals(Object o) {
if (!(o instanceof Annotation)) {
if (!(o instanceof Annotation other)) {
return false;
}
Annotation other = (Annotation) o;
if (this.memberValuePairName == null) {
if (other.memberValuePairName != null)
return false;
Expand All @@ -59,6 +61,11 @@ public boolean equals(Object o) {
return super.equals(o);
}

@Override
protected int calculateHashCode() {
return Util.combineHashCodes(super.calculateHashCode(), (this.memberValuePairName == null) ? 0 : this.memberValuePairName.hashCode());
}

public IMember getDeclaringMember() {
return (IMember) getParent();
}
Expand Down Expand Up @@ -136,15 +143,6 @@ public IClassFile getClassFile() {
return getParent().getClassFile();
}

@Override
public int hashCode() {
final int prime = 31;
int result = super.hashCode();
result = prime * result + ((this.memberValuePairName == null) ? 0 : this.memberValuePairName.hashCode());
result = prime * result + this.name.hashCode();
return result;
}

@Override
protected void toStringName(StringBuilder buffer) {
buffer.append('@');
Expand Down
Expand Up @@ -34,15 +34,15 @@
* @see ICompilationUnit
*/
public class BasicCompilationUnit implements ICompilationUnit {
protected char[] contents;
protected final char[] contents;

// Note that if this compiler ICompilationUnit's content is known in advance, the fileName is not used to retrieve this content.
// Instead it is used to keep enough information to recreate the IJavaElement corresponding to this compiler ICompilationUnit.
// Thus the fileName can be a path to a .class file, or even a path in a .jar to a .class file.
// (e.g. /P/lib/mylib.jar|org/eclipse/test/X.class)
protected char[] fileName;
protected final char[] fileName;

protected char[][] packageName;
protected final char[][] packageName;
protected char[] mainTypeName;
protected char[] moduleName;
protected String encoding;
Expand Down
Expand Up @@ -35,6 +35,9 @@
protected BinaryField(JavaElement parent, String name) {
super(parent, name);
}
protected BinaryField(JavaElement parent, String name, int occurrenceCount) {
super(parent, name, occurrenceCount);
}
@Override
public boolean equals(Object o) {
if (!(o instanceof BinaryField)) return false;
Expand Down Expand Up @@ -105,8 +108,7 @@ public boolean isResolved() {
}
@Override
public JavaElement resolved(Binding binding) {
SourceRefElement resolvedHandle = new ResolvedBinaryField(this.getParent(), this.name, new String(binding.computeUniqueKey()));
resolvedHandle.occurrenceCount = this.occurrenceCount;
SourceRefElement resolvedHandle = new ResolvedBinaryField(this.getParent(), this.name, new String(binding.computeUniqueKey()), this.getOccurrenceCount());
return resolvedHandle;
}
/*
Expand Down
Expand Up @@ -39,6 +39,9 @@ public abstract class BinaryMember extends NamedMember {
protected BinaryMember(JavaElement parent, String name) {
super(parent, name);
}
protected BinaryMember(JavaElement parent, String name, int occurrenceCount) {
super(parent, name, occurrenceCount);
}
/*
* @see ISourceManipulation
*/
Expand Down

0 comments on commit 4869388

Please sign in to comment.