Cls*.setMirror(): cleanup, fixes, and better diagnostic

This commit is contained in:
Roman Shevchenko
2012-10-26 22:07:31 +02:00
parent dd75fb3c75
commit c4c09245e1
74 changed files with 1042 additions and 1366 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -26,7 +26,8 @@ import org.jetbrains.annotations.Nullable;
* Represents a Java package.
*/
public interface PsiPackage extends PsiCheckedRenameElement, NavigationItem, PsiModifierListOwner, PsiDirectoryContainer, PsiQualifiedNamedElement {
@NonNls String PACKAGE_INFO_FILE = "package-info.java";
@NonNls String PACKAGE_INFO_CLASS = "package-info";
@NonNls String PACKAGE_INFO_FILE = PACKAGE_INFO_CLASS + ".java";
PsiPackage[] EMPTY_ARRAY = new PsiPackage[0];
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -15,17 +15,15 @@
*/
package com.intellij.psi.impl.compiled;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.pom.Navigatable;
import com.intellij.psi.*;
import com.intellij.psi.impl.PsiImplUtil;
import com.intellij.psi.impl.java.stubs.PsiAnnotationStub;
import com.intellij.psi.impl.meta.MetaRegistry;
import com.intellij.psi.impl.source.SourceTreeToPsiMap;
import com.intellij.psi.impl.source.tree.ChildRole;
import com.intellij.psi.impl.source.tree.CompositeElement;
import com.intellij.psi.impl.source.tree.TreeElement;
import com.intellij.psi.meta.PsiMetaData;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
@@ -35,28 +33,25 @@ import org.jetbrains.annotations.Nullable;
* @author ven
*/
public class ClsAnnotationImpl extends ClsRepositoryPsiElement<PsiAnnotationStub> implements PsiAnnotation, Navigatable {
private static final Logger LOG = Logger.getInstance("com.intellij.psi.impl.compiled.ClsAnnotationImpl");
private ClsJavaCodeReferenceElementImpl myReferenceElement; //protected by lock
private ClsAnnotationParameterListImpl myParameterList; //protected by lock
private final Object lock = new Object();
private ClsJavaCodeReferenceElementImpl myReferenceElement;
private ClsAnnotationParameterListImpl myParameterList;
public ClsAnnotationImpl(final PsiAnnotationStub stub) {
super(stub);
}
@Override
public void appendMirrorText(final int indentLevel, final StringBuilder buffer) {
public void appendMirrorText(int indentLevel, @NotNull StringBuilder buffer) {
buffer.append("@").append(getReferenceElement().getCanonicalText());
((ClsAnnotationParameterListImpl)getParameterList()).appendMirrorText(indentLevel, buffer);
appendText(getParameterList(), indentLevel, buffer);
}
@Override
public void setMirror(@NotNull TreeElement element) {
public void setMirror(@NotNull TreeElement element) throws InvalidMirrorException {
setMirrorCheckingType(element, null);
PsiAnnotation mirror = (PsiAnnotation)SourceTreeToPsiMap.treeElementToPsi(element);
((ClsElementImpl)getParameterList()).setMirror((TreeElement)SourceTreeToPsiMap.psiElementToTree(mirror.getParameterList()));
((ClsElementImpl)getNameReferenceElement()).setMirror((TreeElement)SourceTreeToPsiMap.psiElementToTree(mirror.getNameReferenceElement()));
PsiAnnotation mirror = SourceTreeToPsiMap.treeToPsiNotNull(element);
setMirror(getNameReferenceElement(), mirror.getNameReferenceElement());
setMirror(getParameterList(), mirror.getParameterList());
}
@Override
@@ -78,22 +73,18 @@ public class ClsAnnotationImpl extends ClsRepositoryPsiElement<PsiAnnotationStub
@Override
@NotNull
public PsiAnnotationParameterList getParameterList() {
synchronized (lock) {
synchronized (LAZY_BUILT_LOCK) {
if (myParameterList == null) {
final PsiAnnotationStub stub = getStub();
final CompositeElement mirror = stub.getTreeElement();
final PsiAnnotationParameterList paramList = (PsiAnnotationParameterList)mirror.findChildByRoleAsPsiElement(ChildRole.PARAMETER_LIST);
PsiAnnotationParameterList paramList = PsiTreeUtil.getRequiredChildOfType(getStub().getPsiElement(), PsiAnnotationParameterList.class);
myParameterList = new ClsAnnotationParameterListImpl(this, paramList.getAttributes());
}
return myParameterList;
}
}
@Override
@Nullable public String getQualifiedName() {
@Nullable
public String getQualifiedName() {
if (getReferenceElement() == null) return null;
return getReferenceElement().getCanonicalText();
}
@@ -115,7 +106,7 @@ public class ClsAnnotationImpl extends ClsRepositoryPsiElement<PsiAnnotationStub
}
@Override
public <T extends PsiAnnotationMemberValue> T setDeclaredAttributeValue(@NonNls String attributeName, T value) {
public <T extends PsiAnnotationMemberValue> T setDeclaredAttributeValue(@NonNls String attributeName, T value) {
throw new IncorrectOperationException(CAN_NOT_MODIFY_MESSAGE);
}
@@ -132,11 +123,10 @@ public class ClsAnnotationImpl extends ClsRepositoryPsiElement<PsiAnnotationStub
}
private ClsJavaCodeReferenceElementImpl getReferenceElement() {
synchronized (lock) {
synchronized (LAZY_BUILT_LOCK) {
if (myReferenceElement == null) {
final PsiAnnotationStub stub = getStub();
final CompositeElement mirror = stub.getTreeElement();
myReferenceElement = new ClsJavaCodeReferenceElementImpl(this, mirror.findChildByRole(ChildRole.CLASS_REFERENCE).getText());
String text = PsiTreeUtil.getRequiredChildOfType(getStub().getPsiElement(), PsiJavaCodeReferenceElement.class).getText();
myReferenceElement = new ClsJavaCodeReferenceElementImpl(this, text);
}
return myReferenceElement;
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -15,7 +15,6 @@
*/
package com.intellij.psi.impl.compiled;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.psi.*;
import com.intellij.psi.impl.source.SourceTreeToPsiMap;
import com.intellij.psi.impl.source.tree.TreeElement;
@@ -25,43 +24,36 @@ import org.jetbrains.annotations.NotNull;
* @author ven
*/
public class ClsAnnotationParameterListImpl extends ClsElementImpl implements PsiAnnotationParameterList {
private static final Logger LOG = Logger.getInstance("com.intellij.psi.impl.compiled.ClsAnnotationParameterListImpl");
private final ClsNameValuePairImpl[] myAttributes;
private final PsiAnnotation myParent;
private final ClsNameValuePairImpl[] myAttributes;
public ClsAnnotationParameterListImpl(PsiAnnotation parent, PsiNameValuePair[] psiAttributes) {
public ClsAnnotationParameterListImpl(@NotNull PsiAnnotation parent, @NotNull PsiNameValuePair[] psiAttributes) {
myParent = parent;
myAttributes = new ClsNameValuePairImpl[psiAttributes.length];
for (int i = 0; i < myAttributes.length; i++) {
String name = psiAttributes[i].getName();
PsiAnnotationMemberValue value = psiAttributes[i].getValue();
assert value != null : "name=" + name + " value" + value;
myAttributes[i] = new ClsNameValuePairImpl(this, name, value);
}
}
@Override
public void appendMirrorText(final int indentLevel, final StringBuilder buffer) {
public void appendMirrorText(int indentLevel, @NotNull StringBuilder buffer) {
if (myAttributes.length != 0) {
buffer.append("(");
for (int i = 0; i < myAttributes.length; i++) {
if (i > 0) buffer.append(", ");
myAttributes[i].appendMirrorText(indentLevel, buffer);
}
buffer.append(")");
}
}
@Override
public void setMirror(@NotNull TreeElement element) {
public void setMirror(@NotNull TreeElement element) throws InvalidMirrorException {
setMirrorCheckingType(element, null);
PsiAnnotationParameterList mirror = (PsiAnnotationParameterList)SourceTreeToPsiMap.treeElementToPsi(element);
PsiNameValuePair[] attrs = mirror.getAttributes();
LOG.assertTrue(myAttributes.length == attrs.length);
for (int i = 0; i < myAttributes.length; i++) {
myAttributes[i].setMirror((TreeElement)SourceTreeToPsiMap.psiElementToTree(attrs[i]));
}
setMirrors(myAttributes, SourceTreeToPsiMap.<PsiAnnotationParameterList>treeToPsiNotNull(element).getAttributes());
}
@Override
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -15,7 +15,6 @@
*/
package com.intellij.psi.impl.compiled;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.pom.Navigatable;
import com.intellij.psi.*;
import com.intellij.psi.impl.PsiImplUtil;
@@ -32,16 +31,15 @@ import org.jetbrains.annotations.Nullable;
* @author ven
*/
public abstract class ClsAnnotationValueImpl extends ClsElementImpl implements PsiAnnotation, Navigatable {
private static final Logger LOG = Logger.getInstance("com.intellij.psi.impl.compiled.ClsAnnotationValueImpl");
public static final ClsAnnotationImpl[] EMPTY_ARRAY = new ClsAnnotationImpl[0];
private final ClsElementImpl myParent;
private final ClsJavaCodeReferenceElementImpl myReferenceElement;
private final ClsAnnotationParameterListImpl myParameterList;
private final ClsElementImpl myParent;
public ClsAnnotationValueImpl(ClsElementImpl parent) {
@SuppressWarnings("AbstractMethodCallInConstructor")
public ClsAnnotationValueImpl(@NotNull ClsElementImpl parent) {
myParent = parent;
myReferenceElement = createReference();
myParameterList = createParameterList();
myParent = parent;
}
protected abstract ClsAnnotationParameterListImpl createParameterList();
@@ -49,18 +47,18 @@ public abstract class ClsAnnotationValueImpl extends ClsElementImpl implements P
protected abstract ClsJavaCodeReferenceElementImpl createReference();
@Override
public void appendMirrorText(final int indentLevel, final StringBuilder buffer) {
public void appendMirrorText(int indentLevel, @NotNull StringBuilder buffer) {
buffer.append("@").append(myReferenceElement.getCanonicalText());
myParameterList.appendMirrorText(indentLevel, buffer);
}
@Override
public void setMirror(@NotNull TreeElement element) {
public void setMirror(@NotNull TreeElement element) throws InvalidMirrorException {
setMirrorCheckingType(element, null);
PsiAnnotation mirror = (PsiAnnotation)SourceTreeToPsiMap.treeElementToPsi(element);
((ClsElementImpl)getParameterList()).setMirror((TreeElement)SourceTreeToPsiMap.psiElementToTree(mirror.getParameterList()));
((ClsElementImpl)getNameReferenceElement()).setMirror((TreeElement)SourceTreeToPsiMap.psiElementToTree(mirror.getNameReferenceElement()));
PsiAnnotation mirror = SourceTreeToPsiMap.treeToPsiNotNull(element);
setMirror(getNameReferenceElement(), mirror.getNameReferenceElement());
setMirror(getParameterList(), mirror.getParameterList());
}
@Override
@@ -91,9 +89,9 @@ public abstract class ClsAnnotationValueImpl extends ClsElementImpl implements P
}
@Override
@Nullable public String getQualifiedName() {
if (myReferenceElement == null) return null;
return myReferenceElement.getCanonicalText();
@Nullable
public String getQualifiedName() {
return myReferenceElement != null ? myReferenceElement.getCanonicalText() : null;
}
@Override
@@ -113,7 +111,7 @@ public abstract class ClsAnnotationValueImpl extends ClsElementImpl implements P
}
@Override
public <T extends PsiAnnotationMemberValue> T setDeclaredAttributeValue(@NonNls String attributeName, T value) {
public <T extends PsiAnnotationMemberValue> T setDeclaredAttributeValue(@NonNls String attributeName, T value) {
throw new IncorrectOperationException(CAN_NOT_MODIFY_MESSAGE);
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -15,7 +15,6 @@
*/
package com.intellij.psi.impl.compiled;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.psi.*;
import com.intellij.psi.impl.source.SourceTreeToPsiMap;
import com.intellij.psi.impl.source.tree.TreeElement;
@@ -27,9 +26,8 @@ import org.jetbrains.annotations.NotNull;
public class ClsArrayInitializerMemberValueImpl extends ClsElementImpl implements PsiArrayInitializerMemberValue {
private final ClsElementImpl myParent;
private final PsiAnnotationMemberValue[] myInitializers;
private static final Logger LOG = Logger.getInstance("com.intellij.psi.impl.compiled.ClsArrayInitializerMemberValueImpl");
public ClsArrayInitializerMemberValueImpl(ClsElementImpl parent, PsiAnnotationMemberValue[] initializers) {
public ClsArrayInitializerMemberValueImpl(@NotNull ClsElementImpl parent, @NotNull PsiAnnotationMemberValue[] initializers) {
myParent = parent;
myInitializers = initializers;
}
@@ -42,26 +40,19 @@ public class ClsArrayInitializerMemberValueImpl extends ClsElementImpl implement
}
@Override
public void appendMirrorText(final int indentLevel, final StringBuilder buffer) {
public void appendMirrorText(int indentLevel, @NotNull StringBuilder buffer) {
buffer.append('{');
for (int i = 0; i < myInitializers.length; i++) {
if (i > 0) buffer.append(", ");
((ClsElementImpl)myInitializers[i]).appendMirrorText(0, buffer);
appendText(myInitializers[i], 0, buffer);
}
buffer.append('}');
}
@Override
public void setMirror(@NotNull TreeElement element) {
public void setMirror(@NotNull TreeElement element) throws InvalidMirrorException {
setMirrorCheckingType(element, null);
PsiArrayInitializerMemberValue mirror = (PsiArrayInitializerMemberValue)SourceTreeToPsiMap.treeElementToPsi(element);
PsiAnnotationMemberValue[] initializers = mirror.getInitializers();
LOG.assertTrue(myInitializers.length == initializers.length);
for (int i = 0; i < myInitializers.length; i++) {
ClsElementImpl value = (ClsElementImpl)myInitializers[i];
value.setMirror((TreeElement)SourceTreeToPsiMap.psiElementToTree(initializers[i]));
}
setMirrors(getInitializers(), SourceTreeToPsiMap.<PsiArrayInitializerMemberValue>treeToPsiNotNull(element).getInitializers());
}
@Override
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2011 JetBrains s.r.o.
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -55,12 +55,12 @@ public class ClsBinaryExpressionImpl extends ClsElementImpl implements PsiBinary
}
@Override
public void appendMirrorText(int indentLevel, StringBuilder buffer) {
public void appendMirrorText(int indentLevel, @NotNull StringBuilder buffer) {
buffer.append(getText());
}
@Override
public void setMirror(@NotNull TreeElement element) {
public void setMirror(@NotNull TreeElement element) throws InvalidMirrorException {
setMirrorCheckingType(element, JavaElementType.BINARY_EXPRESSION);
}
@@ -124,13 +124,14 @@ public class ClsBinaryExpressionImpl extends ClsElementImpl implements PsiBinary
return myLOperand.getType();
}
@Override
public String toString() {
return "PsiBinaryExpression:" + getText();
}
@NotNull
@Override
public PsiExpression[] getOperands() {
return new PsiExpression[]{getLOperand(), getROperand()};
}
@Override
public String toString() {
return "PsiBinaryExpression:" + getText();
}
}
@@ -17,12 +17,10 @@ package com.intellij.psi.impl.compiled;
import com.intellij.navigation.ItemPresentation;
import com.intellij.navigation.ItemPresentationProviders;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.extensions.Extensions;
import com.intellij.openapi.ui.Queryable;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.util.Pair;
import com.intellij.openapi.util.Ref;
import com.intellij.psi.*;
import com.intellij.psi.impl.InheritanceImplUtil;
import com.intellij.psi.impl.PsiClassImplUtil;
@@ -32,11 +30,10 @@ import com.intellij.psi.impl.java.stubs.JavaStubElementTypes;
import com.intellij.psi.impl.java.stubs.PsiClassStub;
import com.intellij.psi.impl.source.*;
import com.intellij.psi.impl.source.tree.TreeElement;
import com.intellij.psi.javadoc.PsiDocComment;
import com.intellij.psi.scope.PsiScopeProcessor;
import com.intellij.psi.search.SearchScope;
import com.intellij.util.ArrayUtil;
import com.intellij.util.IncorrectOperationException;
import com.intellij.psi.util.PsiUtilCore;
import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -47,83 +44,24 @@ import java.util.Collection;
import java.util.List;
import java.util.Map;
public class ClsClassImpl extends ClsRepositoryPsiElement<PsiClassStub<?>> implements PsiExtensibleClass, PsiQualifiedNamedElement, Queryable {
public class ClsClassImpl extends ClsMemberImpl<PsiClassStub<?>> implements PsiExtensibleClass, PsiQualifiedNamedElement, Queryable {
public static final Key<PsiClass> DELEGATE_KEY = Key.create("DELEGATE");
private static final Logger LOG = Logger.getInstance("#com.intellij.psi.impl.compiled.ClsClassImpl");
private final ClassInnerStuffCache myInnersCache = new ClassInnerStuffCache(this);
private final PsiIdentifier myNameIdentifier;
private final PsiDocComment myDocComment;
public ClsClassImpl(final PsiClassStub stub) {
super(stub);
myDocComment = isDeprecated() ? new ClsDocCommentImpl(this) : null;
myNameIdentifier = new ClsIdentifierImpl(this, getShortName());
}
@Override
@NotNull
public PsiElement[] getChildren() {
PsiIdentifier name = getNameIdentifier();
PsiDocComment docComment = getDocComment();
PsiModifierList modifierList = getModifierList();
PsiReferenceList extendsList = getExtendsList();
PsiReferenceList implementsList = getImplementsList();
List<PsiField> fields = getOwnFields();
List<PsiMethod> methods = getOwnMethods();
List<PsiClass> classes = getOwnInnerClasses();
int count =
(docComment != null ? 1 : 0)
+ 1 // modifierList
+ 1 // name
+ 1 // extends list
+ 1 // implementsList
+ fields.size()
+ methods.size()
+ classes.size();
PsiElement[] children = new PsiElement[count];
int offset = 0;
if (docComment != null) {
children[offset++] = docComment;
}
children[offset++] = modifierList;
children[offset++] = name;
children[offset++] = extendsList;
children[offset++] = implementsList;
ArrayUtil.copy(fields, children, offset);
offset += fields.size();
ArrayUtil.copy(methods, children, offset);
offset += methods.size();
ArrayUtil.copy(classes, children, offset);
/*offset += classes.size();*/
return children;
}
@Override
@NotNull
public PsiIdentifier getNameIdentifier() {
return myNameIdentifier;
}
private String getShortName() {
String qName = getQualifiedName();
String name = PsiNameHelper.getShortClassName(qName);
if (name.length() == 0) {
name = "_";
}
return name;
}
@Override
@NotNull
public String getName() {
return getStub().getName();
List<PsiElement> children = ContainerUtil.newArrayList();
ContainerUtil.addAll(children, getChildren(getDocComment(), getModifierList(), getNameIdentifier(), getExtendsList(), getImplementsList()));
ContainerUtil.addAll(children, getOwnFields());
ContainerUtil.addAll(children, getOwnMethods());
ContainerUtil.addAll(children, getOwnInnerClasses());
return PsiUtilCore.toPsiElementArray(children);
}
@Override
@@ -137,12 +75,6 @@ public class ClsClassImpl extends ClsRepositoryPsiElement<PsiClassStub<?>> imple
return PsiImplUtil.hasTypeParameters(this);
}
@Override
public PsiElement setName(@NotNull String name) throws IncorrectOperationException {
PsiImplUtil.setName(getNameIdentifier(), name);
return this;
}
@Override
@NotNull
public String getQualifiedName() {
@@ -352,11 +284,6 @@ public class ClsClassImpl extends ClsRepositoryPsiElement<PsiClassStub<?>> imple
return name.substring(0, i) + ".java";
}
@Override
public PsiDocComment getDocComment() {
return myDocComment;
}
@Override
public PsiJavaToken getLBrace() {
return null;
@@ -383,145 +310,109 @@ public class ClsClassImpl extends ClsRepositoryPsiElement<PsiClassStub<?>> imple
}
@Override
public void appendMirrorText(final int indentLevel, @NonNls final StringBuilder buffer) {
ClsDocCommentImpl docComment = (ClsDocCommentImpl)getDocComment();
if (docComment != null) {
docComment.appendMirrorText(indentLevel, buffer);
goNextLine(indentLevel, buffer);
}
((ClsElementImpl)getModifierList()).appendMirrorText(indentLevel, buffer);
public void appendMirrorText(final int indentLevel, @NotNull @NonNls final StringBuilder buffer) {
appendText(getDocComment(), indentLevel, buffer, NEXT_LINE);
appendText(getModifierList(), indentLevel, buffer);
buffer.append(isEnum() ? "enum " : isAnnotationType() ? "@interface " : isInterface() ? "interface " : "class ");
((ClsElementImpl)getNameIdentifier()).appendMirrorText(indentLevel, buffer);
((ClsElementImpl)getTypeParameterList()).appendMirrorText(indentLevel, buffer);
buffer.append(' ');
appendText(getNameIdentifier(), indentLevel, buffer, " ");
appendText(getTypeParameterList(), indentLevel, buffer, " ");
if (!isEnum() && !isAnnotationType()) {
((ClsElementImpl)getExtendsList()).appendMirrorText(indentLevel, buffer);
buffer.append(' ');
}
if (!isInterface()) {
((ClsElementImpl)getImplementsList()).appendMirrorText(indentLevel, buffer);
appendText(getExtendsList(), indentLevel, buffer, " ");
}
appendText(getImplementsList(), indentLevel, buffer, " ");
buffer.append('{');
final int newIndentLevel = indentLevel + getIndentSize();
int newIndentLevel = indentLevel + getIndentSize();
List<PsiField> fields = getOwnFields();
List<PsiMethod> methods = getOwnMethods();
List<PsiClass> classes = getOwnInnerClasses();
if (fields.size() > 0) {
goNextLine(newIndentLevel, buffer);
for (int i = 0; i < fields.size(); i++) {
PsiField field = fields.get(i);
((ClsElementImpl)field).appendMirrorText(newIndentLevel, buffer);
appendText(field, newIndentLevel, buffer);
if (field instanceof ClsEnumConstantImpl) {
if (i < fields.size() - 1 && fields.get(i + 1) instanceof ClsEnumConstantImpl) {
buffer.append(", ");
}
else {
buffer.append(";");
buffer.append(';');
if (i < fields.size() - 1) {
buffer.append('\n');
goNextLine(newIndentLevel, buffer);
}
}
} else if (i < fields.size() - 1) {
}
else if (i < fields.size() - 1) {
goNextLine(newIndentLevel, buffer);
}
}
}
else if (isEnum() && methods.size() + classes.size() > 0) {
goNextLine(newIndentLevel, buffer);
buffer.append(";");
}
List<PsiMethod> methods = getOwnMethods();
if (methods.size() > 0) {
goNextLine(newIndentLevel, buffer);
if (isEnum() && fields.size() == 0) {
buffer.append(";"); // avoid parse errors due to no fields in enum
if (isEnum() || fields.size() > 0) {
buffer.append('\n');
}
goNextLine(newIndentLevel, buffer);
for (int i = 0; i < methods.size(); i++) {
PsiMethod method = methods.get(i);
((ClsElementImpl)method).appendMirrorText(newIndentLevel, buffer);
appendText(methods.get(i), newIndentLevel, buffer);
if (i < methods.size() - 1) {
goNextLine(newIndentLevel, buffer);
buffer.append('\n');
goNextLine(newIndentLevel, buffer);
}
}
}
List<PsiClass> classes = getOwnInnerClasses();
if (classes.size() > 0) {
if (fields.size() + methods.size() > 0) {
buffer.append('\n');
}
goNextLine(newIndentLevel, buffer);
goNextLine(newIndentLevel, buffer);
for (int i = 0; i < classes.size(); i++) {
PsiClass aClass = classes.get(i);
((ClsElementImpl)aClass).appendMirrorText(newIndentLevel, buffer);
appendText(classes.get(i), newIndentLevel, buffer);
if (i < classes.size() - 1) {
goNextLine(newIndentLevel, buffer);
buffer.append('\n');
goNextLine(newIndentLevel, buffer);
}
}
}
goNextLine(indentLevel, buffer);
buffer.append('}');
}
@Override
public void setMirror(@NotNull TreeElement element) {
public void setMirror(@NotNull TreeElement element) throws InvalidMirrorException {
setMirrorCheckingType(element, null);
PsiClass mirror = SourceTreeToPsiMap.treeToPsiNotNull(element);
PsiDocComment docComment = getDocComment();
if (docComment != null) {
((ClsElementImpl)docComment).setMirror((TreeElement)SourceTreeToPsiMap.psiElementToTree(mirror.getDocComment()));
}
((ClsElementImpl)getModifierList()).setMirror((TreeElement)SourceTreeToPsiMap.psiElementToTree(mirror.getModifierList()));
((ClsElementImpl)getNameIdentifier()).setMirror((TreeElement)SourceTreeToPsiMap.psiElementToTree(mirror.getNameIdentifier()));
if (!isAnnotationType() && !isEnum()) {
((ClsElementImpl)getExtendsList()).setMirror((TreeElement)SourceTreeToPsiMap.psiElementToTree(mirror.getExtendsList()));
}
((ClsElementImpl)getImplementsList()).setMirror((TreeElement)SourceTreeToPsiMap.psiElementToTree(mirror.getImplementsList()));
((ClsElementImpl)getTypeParameterList()).setMirror((TreeElement)SourceTreeToPsiMap.psiElementToTree(mirror.getTypeParameterList()));
setMirrorIfPresent(getDocComment(), mirror.getDocComment());
Ref<Boolean> extLog = Ref.create(true);
setMirror(getModifierList(), mirror.getModifierList());
setMirror(getNameIdentifier(), mirror.getNameIdentifier());
setMirror(getTypeParameterList(), mirror.getTypeParameterList());
if (!isEnum() && !isAnnotationType()) {
setMirror(getExtendsList(), mirror.getExtendsList());
}
setMirror(getImplementsList(), mirror.getImplementsList());
List<PsiField> fields = getOwnFields();
PsiField[] mirrorFields = mirror.getFields();
if (fields.size() == mirrorFields.length) {
for (int i = 0; i < fields.size(); i++) {
((ClsElementImpl)fields.get(i)).setMirror(SourceTreeToPsiMap.psiToTreeNotNull(mirrorFields[i]));
}
}
else {
log(this, mirror, "fields:" + fields.size() + "!=" + mirrorFields.length, extLog);
}
List<PsiMethod> methods = getOwnMethods();
PsiMethod[] mirrorMethods = mirror.getMethods();
if (methods.size() == mirrorMethods.length) {
for (int i = 0; i < methods.size(); i++) {
((ClsElementImpl)methods.get(i)).setMirror(SourceTreeToPsiMap.psiToTreeNotNull(mirrorMethods[i]));
}
}
else {
log(this, mirror, "methods:" + methods.size() + "!=" + mirrorMethods.length, extLog);
}
List<PsiClass> classes = getOwnInnerClasses();
PsiClass[] mirrorClasses = mirror.getInnerClasses();
if (classes.size() == mirrorClasses.length) {
for (int i = 0; i < classes.size(); i++) {
((ClsElementImpl)classes.get(i)).setMirror(SourceTreeToPsiMap.psiToTreeNotNull(mirrorClasses[i]));
}
}
else {
log(this, mirror, "classes:" + classes.size() + "!=" + mirrorClasses.length, extLog);
}
}
private static void log(@NotNull ClsClassImpl stub, @NotNull PsiClass mirror, @NotNull String message, @NotNull Ref<Boolean> verbose) {
LOG.error(message + "; file:" + stub.getContainingFile().getName());
if (verbose.get()) {
StringBuilder builder = new StringBuilder();
stub.appendMirrorText(0, builder);
LOG.error("\nStub:\n" + builder.toString() + "\nMirror:\n" + mirror.getText());
verbose.set(false);
}
setMirrors(getOwnFields(), mirror.getFields());
setMirrors(getOwnMethods(), mirror.getMethods());
setMirrors(getOwnInnerClasses(), mirror.getInnerClasses());
}
@Override
@@ -564,13 +455,13 @@ public class ClsClassImpl extends ClsRepositoryPsiElement<PsiClassStub<?>> imple
@Nullable
public PsiClass getSourceMirrorClass() {
PsiClass delegate = getUserData(DELEGATE_KEY);
final PsiClass delegate = getUserData(DELEGATE_KEY);
if (delegate instanceof ClsClassImpl) {
return ((ClsClassImpl)delegate).getSourceMirrorClass();
}
PsiElement parent = getParent();
final String name = getName();
final PsiElement parent = getParent();
if (parent instanceof PsiFile) {
if (!(parent instanceof PsiClassOwner)) return null;
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -15,7 +15,6 @@
*/
package com.intellij.psi.impl.compiled;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.psi.*;
import com.intellij.psi.impl.ElementBase;
import com.intellij.psi.impl.PsiImplUtil;
@@ -23,7 +22,6 @@ import com.intellij.psi.impl.source.SourceTreeToPsiMap;
import com.intellij.psi.impl.source.tree.TreeElement;
import com.intellij.ui.RowIcon;
import com.intellij.util.PlatformIcons;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import javax.swing.*;
@@ -32,28 +30,24 @@ import javax.swing.*;
* @author ven
*/
public class ClsClassObjectAccessExpressionImpl extends ClsElementImpl implements PsiClassObjectAccessExpression {
private static final Logger LOG = Logger.getInstance("com.intellij.psi.impl.compiled.ClsClassObjectAccessExpressionImpl");
private final ClsTypeElementImpl myTypeElement;
private final ClsElementImpl myParent;
@NonNls private static final String CLASS_ENDING = ".class";
private final ClsTypeElementImpl myTypeElement;
public ClsClassObjectAccessExpressionImpl(String canonicalClassText, ClsElementImpl parent) {
public ClsClassObjectAccessExpressionImpl(ClsElementImpl parent, String canonicalClassText) {
myParent = parent;
myTypeElement = new ClsTypeElementImpl(this, canonicalClassText, ClsTypeElementImpl.VARIANCE_NONE);
}
@Override
public void appendMirrorText(final int indentLevel, final StringBuilder buffer) {
public void appendMirrorText(int indentLevel, @NotNull StringBuilder buffer) {
myTypeElement.appendMirrorText(0, buffer);
buffer.append(CLASS_ENDING);
buffer.append('.').append(PsiKeyword.CLASS);
}
@Override
public void setMirror(@NotNull TreeElement element) {
public void setMirror(@NotNull TreeElement element) throws InvalidMirrorException {
setMirrorCheckingType(element, null);
PsiClassObjectAccessExpression mirror = (PsiClassObjectAccessExpression)SourceTreeToPsiMap.treeElementToPsi(element);
((ClsElementImpl)getOperand()).setMirror((TreeElement)SourceTreeToPsiMap.psiElementToTree(mirror.getOperand()));
setMirror(getOperand(), SourceTreeToPsiMap.<PsiClassObjectAccessExpression>treeToPsiNotNull(element).getOperand());
}
@Override
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -28,16 +28,13 @@ class ClsDocCommentImpl extends ClsElementImpl implements PsiDocComment, JavaTok
private final PsiDocCommentOwner myParent;
private final PsiDocTag[] myTags;
ClsDocCommentImpl(PsiDocCommentOwner parent) {
ClsDocCommentImpl(@NotNull PsiDocCommentOwner parent) {
myParent = parent;
PsiDocTag[] tags = new PsiDocTag[1];
tags[0] = new ClsDocTagImpl(this, "@deprecated");
myTags = tags;
myTags = new PsiDocTag[]{new ClsDocTagImpl(this, "@deprecated")};
}
@Override
public void appendMirrorText(final int indentLevel, final StringBuilder buffer) {
public void appendMirrorText(final int indentLevel, @NotNull final StringBuilder buffer) {
buffer.append("/**");
for (PsiDocTag tag : getTags()) {
goNextLine(indentLevel + 1, buffer);
@@ -49,7 +46,7 @@ class ClsDocCommentImpl extends ClsElementImpl implements PsiDocComment, JavaTok
}
@Override
public void setMirror(@NotNull TreeElement element) {
public void setMirror(@NotNull TreeElement element) throws InvalidMirrorException {
setMirrorCheckingType(element, JavaDocElementType.DOC_COMMENT);
}
@@ -83,15 +80,13 @@ class ClsDocCommentImpl extends ClsElementImpl implements PsiDocComment, JavaTok
@Override
public PsiDocTag findTagByName(@NonNls String name) {
if (!name.equals("deprecated")) return null;
return getTags()[0];
return name.equals("deprecated") ? getTags()[0] : null;
}
@Override
@NotNull
public PsiDocTag[] findTagsByName(@NonNls String name) {
if (!name.equals("deprecated")) return PsiDocTag.EMPTY_ARRAY;
return getTags();
return name.equals("deprecated") ? getTags() : PsiDocTag.EMPTY_ARRAY;
}
@Override
@@ -108,5 +103,4 @@ class ClsDocCommentImpl extends ClsElementImpl implements PsiDocComment, JavaTok
visitor.visitElement(this);
}
}
}
@@ -1,6 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,12 +15,11 @@
*/
package com.intellij.psi.impl.compiled;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.psi.JavaElementVisitor;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiElementVisitor;
import com.intellij.psi.impl.PsiImplUtil;
import com.intellij.psi.impl.source.tree.ElementType;
import com.intellij.psi.impl.source.tree.JavaDocElementType;
import com.intellij.psi.impl.source.tree.TreeElement;
import com.intellij.psi.javadoc.PsiDocComment;
import com.intellij.psi.javadoc.PsiDocTag;
@@ -31,24 +29,22 @@ import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
class ClsDocTagImpl extends ClsElementImpl implements PsiDocTag {
private static final Logger LOG = Logger.getInstance("#com.intellij.psi.impl.compiled.ClsDocTagImpl");
private final ClsDocCommentImpl myDocComment;
private final PsiElement myNameElement;
public ClsDocTagImpl(ClsDocCommentImpl docComment, @NonNls String name) {
myDocComment = docComment;
myNameElement = new NameElement(name);
myNameElement = new NameElement(this, name);
}
@Override
public void appendMirrorText(final int indentLevel, final StringBuilder buffer) {
public void appendMirrorText(int indentLevel, @NotNull StringBuilder buffer) {
buffer.append(myNameElement.getText());
}
@Override
public void setMirror(@NotNull TreeElement element) {
setMirrorCheckingType(element, ElementType.DOC_TAG);
public void setMirror(@NotNull TreeElement element) throws InvalidMirrorException {
setMirrorCheckingType(element, JavaDocElementType.DOC_TAG);
}
@Override
@@ -58,11 +54,12 @@ class ClsDocTagImpl extends ClsElementImpl implements PsiDocTag {
@Override
@NotNull
public char[] textToCharArray(){
public char[] textToCharArray() {
return myNameElement.textToCharArray();
}
@Override
@NotNull
public String getName() {
return getNameElement().getText().substring(1);
}
@@ -78,7 +75,7 @@ class ClsDocTagImpl extends ClsElementImpl implements PsiDocTag {
}
@Override
public int getTextLength(){
public int getTextLength() {
return myNameElement.getTextLength();
}
@@ -123,10 +120,18 @@ class ClsDocTagImpl extends ClsElementImpl implements PsiDocTag {
}
}
private class NameElement extends ClsElementImpl {
@Override
public PsiElement setName(@NotNull String name) throws IncorrectOperationException {
PsiImplUtil.setName(getNameElement(), name);
return this;
}
private static class NameElement extends ClsElementImpl {
private final ClsDocTagImpl myParent;
private final String myText;
public NameElement(String text) {
public NameElement(ClsDocTagImpl parent, String text) {
myParent = parent;
myText = text;
}
@@ -137,28 +142,28 @@ class ClsDocTagImpl extends ClsElementImpl implements PsiDocTag {
@Override
@NotNull
public char[] textToCharArray(){
public char[] textToCharArray() {
return myText.toCharArray();
}
@Override
@NotNull
public PsiElement[] getChildren(){
public PsiElement[] getChildren() {
return PsiElement.EMPTY_ARRAY;
}
@Override
public void appendMirrorText(final int indentLevel, final StringBuilder buffer) {
public void appendMirrorText(int indentLevel, @NotNull StringBuilder buffer) {
}
@Override
public void setMirror(@NotNull TreeElement element) {
public void setMirror(@NotNull TreeElement element) throws InvalidMirrorException {
setMirrorCheckingType(element, null);
}
@Override
public PsiElement getParent() {
return ClsDocTagImpl.this;
return myParent;
}
@Override
@@ -166,9 +171,4 @@ class ClsDocTagImpl extends ClsElementImpl implements PsiDocTag {
visitor.visitElement(this);
}
}
@Override
public PsiElement setName(@NotNull String name) throws IncorrectOperationException{
PsiImplUtil.setName(getNameElement(), name);
return this;
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2011 JetBrains s.r.o.
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -29,16 +29,25 @@ import com.intellij.psi.impl.source.SourceTreeToPsiMap;
import com.intellij.psi.impl.source.tree.TreeElement;
import com.intellij.psi.tree.IElementType;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.PsiUtilCore;
import com.intellij.util.IncorrectOperationException;
import com.intellij.util.StringBuilderSpinAllocator;
import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Arrays;
import java.util.List;
public abstract class ClsElementImpl extends PsiElementBase implements PsiCompiledElement {
private static final Logger LOG = Logger.getInstance("#com.intellij.psi.impl.compiled.ClsElementImpl");
private static final boolean CHECK_MIRROR_ENABLED = false;
protected static final Object LAZY_BUILT_LOCK = new String("lazy cls tree initialization lock");
public static final Key<PsiCompiledElement> COMPILED_ELEMENT = Key.create("COMPILED_ELEMENT");
protected static final Object LAZY_BUILT_LOCK = new String("lazy cls tree initialization lock");
protected static final String CAN_NOT_MODIFY_MESSAGE = JavaCoreBundle.message("psi.error.attempt.to.edit.class.file");
private static final Logger LOG = Logger.getInstance("#com.intellij.psi.impl.compiled.ClsElementImpl");
private volatile TreeElement myMirror = null;
@Override
@@ -56,8 +65,7 @@ public abstract class ClsElementImpl extends PsiElementBase implements PsiCompil
public PsiFile getContainingFile() {
PsiElement parent = getParent();
if (parent == null) {
if (!isValid()) throw new PsiInvalidElementAccessException(this);
return null;
throw new PsiInvalidElementAccessException(this);
}
return parent.getContainingFile();
}
@@ -83,6 +91,21 @@ public abstract class ClsElementImpl extends PsiElementBase implements PsiCompil
return this;
}
@NotNull
protected PsiElement[] getChildren(@Nullable PsiElement... children) {
if (children == null) {
return PsiElement.EMPTY_ARRAY;
}
List<PsiElement> list = ContainerUtil.newArrayListWithExpectedSize(children.length);
for (PsiElement child : children) {
if (child != null) {
list.add(child);
}
}
return PsiUtilCore.toPsiElementArray(list);
}
@Override
public void checkAdd(@NotNull PsiElement element) throws IncorrectOperationException {
throw new IncorrectOperationException(CAN_NOT_MODIFY_MESSAGE);
@@ -118,20 +141,13 @@ public abstract class ClsElementImpl extends PsiElementBase implements PsiCompil
throw new IncorrectOperationException(CAN_NOT_MODIFY_MESSAGE);
}
protected static final String CAN_NOT_MODIFY_MESSAGE = JavaCoreBundle.message("psi.error.attempt.to.edit.class.file");
public abstract void appendMirrorText(final int indentLevel, final StringBuilder buffer);
protected static void goNextLine(int indentLevel, StringBuilder buffer) {
buffer.append('\n');
for (int i = 0; i < indentLevel; i++) buffer.append(' ');
}
public abstract void appendMirrorText(int indentLevel, @NotNull StringBuilder buffer);
protected int getIndentSize() {
return JavaCodeStyleSettingsFacade.getInstance(getProject()).getIndentSize();
}
public abstract void setMirror(@NotNull TreeElement element);
public abstract void setMirror(@NotNull TreeElement element) throws InvalidMirrorException;
@Override
public PsiElement getMirror() {
@@ -158,36 +174,18 @@ public abstract class ClsElementImpl extends PsiElementBase implements PsiCompil
@Override
public int getTextLength() {
String text = getText();
if (text == null){
LOG.error("getText() == null, element = " + this + ", parent = " + getParent());
return 0;
}
return text.length();
return text == null ? 0 : text.length();
}
@Override
public PsiElement findElementAt(int offset) {
PsiElement mirrorAt = getMirror().findElementAt(offset);
while(true){
while (true) {
if (mirrorAt == null) return null;
PsiElement elementAt = mirrorToElement(mirrorAt);
if (elementAt != null) return elementAt;
mirrorAt = mirrorAt.getParent();
}
/*
PsiElement[] children = getChildren();
if (children.length == 0) return this;
for(int i = 0; i < children.length; i++){
int start = children[i].getStartOffsetInParent();
if (offset < start) return null;
int end = start + children[i].getTextLength();
if (offset < end){
return children[i].findElementAt(offset - start);
}
}
return null;
*/
}
@Override
@@ -200,6 +198,7 @@ public abstract class ClsElementImpl extends PsiElementBase implements PsiCompil
return element.getReference();
}
@Nullable
private PsiElement mirrorToElement(PsiElement mirror) {
final PsiElement m = getMirror();
if (m == mirror) return this;
@@ -227,7 +226,17 @@ public abstract class ClsElementImpl extends PsiElementBase implements PsiCompil
@Override
public String getText() {
PsiElement mirror = getMirror();
return mirror != null ? mirror.getText() : null;
if (mirror != null) return mirror.getText();
StringBuilder buffer = StringBuilderSpinAllocator.alloc();
try {
appendMirrorText(0, buffer);
LOG.error("Mirror wasn't set for " + this + ", expected text:\n" + buffer);
return buffer.toString();
}
finally {
StringBuilderSpinAllocator.dispose(buffer);
}
}
@Override
@@ -251,16 +260,94 @@ public abstract class ClsElementImpl extends PsiElementBase implements PsiCompil
return null;
}
protected void setMirrorCheckingType(@NotNull TreeElement element, @Nullable IElementType type) {
if (CHECK_MIRROR_ENABLED) {
LOG.assertTrue(myMirror == null);
}
protected static void goNextLine(int indentLevel, @NotNull StringBuilder buffer) {
buffer.append('\n');
for (int i = 0; i < indentLevel; i++) buffer.append(' ');
}
if (type != null) {
LOG.assertTrue(element.getElementType() == type, element.getElementType() + " != " + type);
protected static void appendText(@NotNull PsiElement stub, int indentLevel, @NotNull StringBuilder buffer) {
((ClsElementImpl)stub).appendMirrorText(indentLevel, buffer);
}
protected static final String NEXT_LINE = "go_to_next_line_and_indent";
protected static void appendText(@Nullable PsiElement stub, int indentLevel, @NotNull StringBuilder buffer, @NotNull String separator) {
if (stub == null) return;
int pos = buffer.length();
((ClsElementImpl)stub).appendMirrorText(indentLevel, buffer);
if (buffer.length() != pos) {
if (separator == NEXT_LINE) {
goNextLine(indentLevel, buffer);
}
else {
buffer.append(separator);
}
}
}
protected void setMirrorCheckingType(@NotNull TreeElement element, @Nullable IElementType type) throws InvalidMirrorException {
// uncomment for extended consistency check
//if (myMirror != null) {
// throw new InvalidMirrorException("Mirror should be null: " + myMirror);
//}
if (type != null && element.getElementType() != type) {
throw new InvalidMirrorException(element.getElementType() + " != " + type);
}
element.putUserData(COMPILED_ELEMENT, this);
myMirror = element;
}
protected static <T extends PsiElement> void setMirror(@Nullable T stub, @Nullable T mirror) throws InvalidMirrorException {
if (stub == null || mirror == null) {
throw new InvalidMirrorException(stub, mirror);
}
((ClsElementImpl)stub).setMirror(SourceTreeToPsiMap.psiToTreeNotNull(mirror));
}
protected static <T extends PsiElement> void setMirrorIfPresent(@Nullable T stub, @Nullable T mirror) throws InvalidMirrorException {
if ((stub == null) != (mirror == null)) {
throw new InvalidMirrorException(stub, mirror);
}
else if (stub != null) {
((ClsElementImpl)stub).setMirror(SourceTreeToPsiMap.psiToTreeNotNull(mirror));
}
}
protected static <T extends PsiElement> void setMirrors(@NotNull T[] stubs, @NotNull T[] mirrors) throws InvalidMirrorException {
if (stubs.length != mirrors.length) {
throw new InvalidMirrorException(stubs, mirrors);
}
for (int i = 0; i < stubs.length; i++) {
setMirror(stubs[i], mirrors[i]);
}
}
protected static <T extends PsiElement> void setMirrors(@NotNull List<T> stubs, @NotNull T[] mirrors) throws InvalidMirrorException {
if (stubs.size() != mirrors.length) {
throw new InvalidMirrorException(stubs, mirrors);
}
for (int i = 0; i < stubs.size(); i++) {
setMirror(stubs.get(i), mirrors[i]);
}
}
protected static class InvalidMirrorException extends RuntimeException {
public InvalidMirrorException(@NotNull @NonNls String message) {
super(message);
}
public InvalidMirrorException(@Nullable PsiElement stubElement, @Nullable PsiElement mirrorElement) {
this("stub:" + stubElement + "; mirror:" + mirrorElement);
}
public InvalidMirrorException(@NotNull PsiElement[] stubElements, @NotNull PsiElement[] mirrorElements) {
this("stub:" + Arrays.toString(stubElements) + "; mirror:" + Arrays.toString(mirrorElements));
}
public InvalidMirrorException(@NotNull List<? extends PsiElement> stubElements, @NotNull PsiElement[] mirrorElements) {
this("stub:" + stubElements + "; mirror:" + Arrays.toString(mirrorElements));
}
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -15,7 +15,6 @@
*/
package com.intellij.psi.impl.compiled;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.psi.*;
import com.intellij.psi.impl.java.stubs.PsiFieldStub;
import com.intellij.psi.impl.source.SourceTreeToPsiMap;
@@ -27,35 +26,25 @@ import org.jetbrains.annotations.NotNull;
* @author ven
*/
public class ClsEnumConstantImpl extends ClsFieldImpl implements PsiEnumConstant {
private static final Logger LOG = Logger.getInstance("#com.intellij.psi.impl.compiled.ClsEnumConstantImpl");
public ClsEnumConstantImpl(final PsiFieldStub stub) {
public ClsEnumConstantImpl(@NotNull PsiFieldStub stub) {
super(stub);
}
@Override
public void appendMirrorText(final int indentLevel, final StringBuilder buffer) {
ClsDocCommentImpl docComment = (ClsDocCommentImpl)getDocComment();
if (docComment != null) {
docComment.appendMirrorText(indentLevel, buffer);
goNextLine(indentLevel, buffer);
}
((ClsElementImpl)getModifierList()).appendMirrorText(indentLevel, buffer);
buffer.append(' ');
((ClsElementImpl)getNameIdentifier()).appendMirrorText(indentLevel, buffer);
public void appendMirrorText(int indentLevel, @NotNull StringBuilder buffer) {
appendText(getDocComment(), indentLevel, buffer, NEXT_LINE);
appendText(getModifierList(), indentLevel, buffer, "");
appendText(getNameIdentifier(), indentLevel, buffer);
}
@Override
public void setMirror(@NotNull TreeElement element) {
public void setMirror(@NotNull TreeElement element) throws InvalidMirrorException {
setMirrorCheckingType(element, null);
PsiField mirror = (PsiField)SourceTreeToPsiMap.treeElementToPsi(element);
if (getDocComment() != null) {
((ClsElementImpl)getDocComment()).setMirror((TreeElement)SourceTreeToPsiMap.psiElementToTree(mirror.getDocComment()));
}
((ClsElementImpl)getModifierList()).setMirror((TreeElement)SourceTreeToPsiMap.psiElementToTree(mirror.getModifierList()));
((ClsElementImpl)getNameIdentifier()).setMirror((TreeElement)SourceTreeToPsiMap.psiElementToTree(mirror.getNameIdentifier()));
PsiField mirror = SourceTreeToPsiMap.treeToPsiNotNull(element);
setMirrorIfPresent(getDocComment(), mirror.getDocComment());
setMirror(getModifierList(), mirror.getModifierList());
setMirror(getNameIdentifier(), mirror.getNameIdentifier());
}
@Override
@@ -90,7 +79,6 @@ public class ClsEnumConstantImpl extends ClsFieldImpl implements PsiEnumConstant
return null;
}
@Override
@NotNull
public PsiType getType() {
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -17,7 +17,6 @@ package com.intellij.psi.impl.compiled;
import com.intellij.navigation.ItemPresentation;
import com.intellij.navigation.ItemPresentationProviders;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.extensions.Extensions;
import com.intellij.openapi.util.Comparing;
import com.intellij.psi.*;
@@ -27,7 +26,6 @@ import com.intellij.psi.impl.java.stubs.JavaStubElementTypes;
import com.intellij.psi.impl.java.stubs.PsiFieldStub;
import com.intellij.psi.impl.source.SourceTreeToPsiMap;
import com.intellij.psi.impl.source.tree.TreeElement;
import com.intellij.psi.javadoc.PsiDocComment;
import com.intellij.psi.search.SearchScope;
import com.intellij.ui.RowIcon;
import com.intellij.util.IncorrectOperationException;
@@ -39,51 +37,19 @@ import org.jetbrains.annotations.NotNull;
import javax.swing.*;
import java.util.Set;
public class ClsFieldImpl extends ClsRepositoryPsiElement<PsiFieldStub> implements PsiField, PsiVariableEx, ClsModifierListOwner {
private static final Logger LOG = Logger.getInstance("#com.intellij.psi.impl.compiled.ClsFieldImpl");
public class ClsFieldImpl extends ClsMemberImpl<PsiFieldStub> implements PsiField, PsiVariableEx, ClsModifierListOwner {
private PsiTypeElement myType = null;
private PsiExpression myInitializer = null;
private boolean myInitializerInitialized = false;
private final PsiIdentifier myNameIdentifier;
private final PsiDocComment myDocComment;
private PsiTypeElement myType = null; //guarded by PsiLock.LOCK
private PsiExpression myInitializer = null; //guarded by PsiLock.LOCK
private boolean myInitializerInitialized = false; //guarded by PsiLock.LOCK
public ClsFieldImpl(final PsiFieldStub stub) {
public ClsFieldImpl(@NotNull PsiFieldStub stub) {
super(stub);
myDocComment = isDeprecated() ? new ClsDocCommentImpl(this) : null;
myNameIdentifier = new ClsIdentifierImpl(this, getName());
}
@Override
@NotNull
public PsiElement[] getChildren() {
PsiDocComment docComment = getDocComment();
PsiModifierList modifierList = getModifierList();
PsiTypeElement type = getTypeElement();
PsiIdentifier name = getNameIdentifier();
int count =
(docComment != null ? 1 : 0)
+ (modifierList != null ? 1 : 0)
+ (type != null ? 1 : 0)
+ (name != null ? 1 : 0);
PsiElement[] children = new PsiElement[count];
int offset = 0;
if (docComment != null) {
children[offset++] = docComment;
}
if (modifierList != null) {
children[offset++] = modifierList;
}
if (type != null) {
children[offset++] = type;
}
if (name != null) {
children[offset++] = name;
}
return children;
return getChildren(getDocComment(), getModifierList(), getTypeElement(), getNameIdentifier());
}
@Override
@@ -91,25 +57,6 @@ public class ClsFieldImpl extends ClsRepositoryPsiElement<PsiFieldStub> implemen
return (PsiClass)getParent();
}
@Override
@NotNull
public PsiIdentifier getNameIdentifier() {
return myNameIdentifier;
}
@Override
@NotNull
@NonNls
public String getName() {
return getStub().getName();
}
@Override
public PsiElement setName(@NotNull String name) throws IncorrectOperationException {
PsiImplUtil.setName(getNameIdentifier(), name);
return this;
}
@Override
@NotNull
public PsiType getType() {
@@ -117,10 +64,13 @@ public class ClsFieldImpl extends ClsRepositoryPsiElement<PsiFieldStub> implemen
}
@Override
@NotNull
public PsiTypeElement getTypeElement() {
synchronized (LAZY_BUILT_LOCK) {
if (myType == null) {
String typeText = TypeInfo.createTypeText(getStub().getType(false));
PsiFieldStub stub = getStub();
String typeText = TypeInfo.createTypeText(stub.getType(false));
assert typeText != null : stub;
myType = new ClsTypeElementImpl(this, typeText, ClsTypeElementImpl.VARIANCE_NONE);
}
return myType;
@@ -191,44 +141,35 @@ public class ClsFieldImpl extends ClsRepositoryPsiElement<PsiFieldStub> implemen
return getStub().isDeprecated();
}
@Override
public PsiDocComment getDocComment() {
return myDocComment;
}
@Override
public void normalizeDeclaration() throws IncorrectOperationException {
}
@Override
public void appendMirrorText(final int indentLevel, final StringBuilder buffer) {
ClsDocCommentImpl docComment = (ClsDocCommentImpl)getDocComment();
if (docComment != null) {
docComment.appendMirrorText(indentLevel, buffer);
goNextLine(indentLevel, buffer);
}
((ClsElementImpl)getModifierList()).appendMirrorText(indentLevel, buffer);
((ClsElementImpl)getTypeElement()).appendMirrorText(indentLevel, buffer);
buffer.append(' ');
((ClsElementImpl)getNameIdentifier()).appendMirrorText(indentLevel, buffer);
if (getInitializer() != null) {
public void appendMirrorText(int indentLevel, @NotNull StringBuilder buffer) {
appendText(getDocComment(), indentLevel, buffer, NEXT_LINE);
appendText(getModifierList(), indentLevel, buffer, "");
appendText(getTypeElement(), indentLevel, buffer, " ");
appendText(getNameIdentifier(), indentLevel, buffer);
PsiExpression initializer = getInitializer();
if (initializer != null) {
buffer.append(" = ");
buffer.append(getInitializer().getText());
buffer.append(initializer.getText());
}
buffer.append(';');
}
@Override
public void setMirror(@NotNull TreeElement element) {
public void setMirror(@NotNull TreeElement element) throws InvalidMirrorException {
setMirrorCheckingType(element, null);
PsiField mirror = (PsiField)SourceTreeToPsiMap.treeElementToPsi(element);
if (getDocComment() != null) {
((ClsElementImpl)getDocComment()).setMirror((TreeElement)SourceTreeToPsiMap.psiElementToTree(mirror.getDocComment()));
}
((ClsElementImpl)getModifierList()).setMirror((TreeElement)SourceTreeToPsiMap.psiElementToTree(mirror.getModifierList()));
((ClsElementImpl)getTypeElement()).setMirror((TreeElement)SourceTreeToPsiMap.psiElementToTree(mirror.getTypeElement()));
((ClsElementImpl)getNameIdentifier()).setMirror((TreeElement)SourceTreeToPsiMap.psiElementToTree(mirror.getNameIdentifier()));
PsiField mirror = SourceTreeToPsiMap.treeToPsiNotNull(element);
setMirrorIfPresent(getDocComment(), mirror.getDocComment());
setMirror(getModifierList(), mirror.getModifierList());
setMirror(getTypeElement(), mirror.getTypeElement());
setMirror(getNameIdentifier(), mirror.getNameIdentifier());
}
@Override
@@ -241,10 +182,6 @@ public class ClsFieldImpl extends ClsRepositoryPsiElement<PsiFieldStub> implemen
}
}
public String toString() {
return "PsiField:" + getName();
}
@Override
@NotNull
public PsiElement getNavigationElement() {
@@ -297,4 +234,8 @@ public class ClsFieldImpl extends ClsRepositoryPsiElement<PsiFieldStub> implemen
return true;
}
@Override
public String toString() {
return "PsiField:" + getName();
}
}
@@ -23,21 +23,15 @@ import com.intellij.lang.FileASTNode;
import com.intellij.lang.java.JavaLanguage;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.extensions.Extensions;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.openapi.progress.NonCancelableSection;
import com.intellij.openapi.progress.ProgressIndicatorProvider;
import com.intellij.openapi.ui.Queryable;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.pom.java.LanguageLevel;
import com.intellij.psi.*;
import com.intellij.psi.impl.JavaPsiImplementationHelper;
import com.intellij.psi.impl.PsiFileEx;
import com.intellij.psi.impl.PsiManagerEx;
import com.intellij.psi.impl.PsiManagerImpl;
import com.intellij.psi.impl.*;
import com.intellij.psi.impl.java.stubs.PsiClassStub;
import com.intellij.psi.impl.java.stubs.impl.PsiJavaFileStubImpl;
import com.intellij.psi.impl.source.PsiFileImpl;
@@ -65,22 +59,21 @@ public class ClsFileImpl extends ClsRepositoryPsiElement<PsiClassHolderFileStub>
private static final MirrorLock MIRROR_LOCK = new MirrorLock();
private static class MirrorLock {}
private static final Key<Document> DOCUMENT_IN_MIRROR_KEY = Key.create("DOCUMENT_IN_MIRROR_KEY");
private final PsiManagerImpl myManager;
private final boolean myIsForDecompiling;
private final FileViewProvider myViewProvider;
private volatile SoftReference<StubTree> myStub;
private TreeElement myMirrorFileElement; // guarded by MIRROR_LOCK
private TreeElement myMirrorFileElement;
private volatile ClsPackageStatementImpl myPackageStatement = null;
private boolean myIsPhysical = true;
private ClsFileImpl(@NotNull PsiManagerImpl manager, @NotNull FileViewProvider viewProvider, boolean forDecompiling) {
//noinspection ConstantConditions
super(null);
myManager = manager;
JavaElementType.CLASS.getIndex(); // Initialize java stubs...
myIsForDecompiling = forDecompiling;
myViewProvider = viewProvider;
JavaElementType.CLASS.getIndex(); // Initialize java stubs...
}
public ClsFileImpl(PsiManagerImpl manager, FileViewProvider viewProvider) {
@@ -143,8 +136,7 @@ public class ClsFileImpl extends ClsRepositoryPsiElement<PsiClassHolderFileStub>
@Override
@NotNull
public PsiClass[] getClasses() {
final PsiClassHolderFileStub fileStub = getStub();
return fileStub != null ? fileStub.getClasses() : PsiClass.EMPTY_ARRAY;
return getStub().getClasses();
}
@Override
@@ -235,53 +227,30 @@ public class ClsFileImpl extends ClsRepositoryPsiElement<PsiClassHolderFileStub>
}
@Override
public void appendMirrorText(final int indentLevel, final StringBuilder buffer) {
buffer.append("\n // IntelliJ API Decompiler stub source generated from a class file\n // Implementation of methods is not available");
goNextLine(indentLevel, buffer);
goNextLine(indentLevel, buffer);
final PsiPackageStatement packageStatement = getPackageStatement();
if (packageStatement != null) {
((ClsElementImpl)packageStatement).appendMirrorText(0, buffer);
goNextLine(indentLevel, buffer);
goNextLine(indentLevel, buffer);
}
public void appendMirrorText(final int indentLevel, @NotNull final StringBuilder buffer) {
buffer.append("\n");
buffer.append(" // IntelliJ API Decompiler stub source generated from a class file\n");
buffer.append(" // Implementation of methods is not available\n");
buffer.append("\n");
final PsiClass[] classes = getClasses();
appendText(getPackageStatement(), 0, buffer, "\n\n");
PsiClass[] classes = getClasses();
if (classes.length > 0) {
PsiClass aClass = classes[0];
((ClsElementImpl)aClass).appendMirrorText(0, buffer);
appendText(classes[0], 0, buffer);
}
}
@Override
public void setMirror(@NotNull TreeElement element) {
PsiElement mirrorFile = SourceTreeToPsiMap.treeElementToPsi(element);
if (mirrorFile instanceof PsiJavaFile) {
PsiPackageStatement packageStatementMirror = ((PsiJavaFile)mirrorFile).getPackageStatement();
final PsiPackageStatement packageStatement = getPackageStatement();
if (packageStatementMirror != null && packageStatement != null) {
((ClsElementImpl)packageStatement).setMirror((TreeElement)packageStatementMirror.getNode());
}
PsiClass[] classes = getClasses();
// Can happen for package-info.class, or classes compiled from languages, that support different class naming scheme, like Scala.
if (classes.length != 1 || JavaPsiFacade.getInstance(getProject()).getNameHelper().isIdentifier(classes[0].getName())) {
PsiClass[] mirrorClasses = ((PsiJavaFile)mirrorFile).getClasses();
if (classes.length != mirrorClasses.length) {
LOG.error("file: " + mirrorFile + " classes: " + Arrays.toString(classes) + " mirrors: " + Arrays.toString(mirrorClasses));
}
else {
for (int i = 0; i < classes.length; i++) {
PsiClass mirrorClass = mirrorClasses[i];
assert mirrorClass != null : this +"; mirror classes: " + Arrays.asList(mirrorClasses);
PsiClass aClass = classes[i];
assert aClass != null : this +"; classes: " + Arrays.asList(classes);
((ClsElementImpl)aClass).setMirror((TreeElement)mirrorClass.getNode());
}
}
}
public void setMirror(@NotNull TreeElement element) throws InvalidMirrorException {
PsiElement mirrorElement = SourceTreeToPsiMap.treeToPsiNotNull(element);
if (!(mirrorElement instanceof PsiJavaFile)) {
throw new InvalidMirrorException("Unexpected mirror file: " + mirrorElement);
}
myMirrorFileElement = element;
PsiJavaFile mirrorFile = (PsiJavaFile)mirrorElement;
setMirrorIfPresent(getPackageStatement(), mirrorFile.getPackageStatement());
setMirrors(getClasses(), mirrorFile.getClasses());
}
@Override
@@ -294,29 +263,29 @@ public class ClsFileImpl extends ClsRepositoryPsiElement<PsiClassHolderFileStub>
public PsiElement getMirror() {
synchronized (MIRROR_LOCK) {
if (myMirrorFileElement == null) {
VirtualFile virtualFile = getVirtualFile();
String mirrorText = decompile(getManager(), virtualFile);
VirtualFile file = getVirtualFile();
String mirrorText = decompile(getManager(), file);
String ext = JavaFileType.INSTANCE.getDefaultExtension();
PsiClass[] classes = getClasses();
String fileName = (classes.length > 0 ? classes[0].getName(): virtualFile.getNameWithoutExtension()) + "." + ext;
PsiManager manager = getManager();
PsiFile mirror = PsiFileFactory.getInstance(manager.getProject()).createFileFromText(fileName, JavaLanguage.INSTANCE, mirrorText, false, false);
final ASTNode mirrorTreeElement = SourceTreeToPsiMap.psiElementToTree(mirror);
String fileName = (classes.length > 0 ? classes[0].getName() : file.getNameWithoutExtension()) + "." + ext;
PsiFileFactory factory = PsiFileFactory.getInstance(getManager().getProject());
PsiFile mirror = factory.createFileFromText(fileName, JavaLanguage.INSTANCE, mirrorText, false, false);
TreeElement mirrorTreeElement = SourceTreeToPsiMap.psiToTreeNotNull(mirror);
//IMPORTANT: do not take lock too early - FileDocumentManager.getInstance().saveToString() can run write action...
final NonCancelableSection section = ProgressIndicatorProvider.startNonCancelableSectionIfSupported();
try {
setMirror((TreeElement)mirrorTreeElement);
// TODO this code should be removed after 11.1 release. It is left just in case.
// Document is not actually used, maybe it is stored in mirror just to avoid garbage collecting
Document document = FileDocumentManager.getInstance().getDocument(virtualFile);
myMirrorFileElement.putUserData(DOCUMENT_IN_MIRROR_KEY, document);
setMirror(mirrorTreeElement);
}
catch (InvalidMirrorException e) {
// todo[r.sh] use logging API once available (to attach .class file)
LOG.error(file.getPath(), e);
}
finally {
section.done();
}
myMirrorFileElement = mirrorTreeElement;
}
return myMirrorFileElement.getPsi();
@@ -382,11 +351,12 @@ public class ClsFileImpl extends ClsRepositoryPsiElement<PsiClassHolderFileStub>
}
public static String decompile(PsiManager manager, VirtualFile file) {
final FileViewProvider provider = ((PsiManagerEx)manager).getFileManager().findViewProvider(file);
ClsFileImpl psiFile = null;
final FileViewProvider provider = ((PsiManagerEx)manager).getFileManager().findViewProvider(file);
if (provider != null) {
final PsiFile psi = provider.getPsi(provider.getBaseLanguage());
if (psi instanceof PsiCompiledFile) {
if (psi instanceof ClsFileImpl) {
psiFile = (ClsFileImpl)psi;
}
}
@@ -423,7 +393,7 @@ public class ClsFileImpl extends ClsRepositoryPsiElement<PsiClassHolderFileStub>
StubTree stubHolder = (StubTree)StubTreeLoader.getInstance().readOrBuild(getProject(), getVirtualFile(), this);
if (stubHolder == null) {
// Must be corrupted classfile
// Must be corrupted .class file
LOG.info("Class file is corrupted: " + getVirtualFile().getPresentableUrl());
StubTree emptyTree = new StubTree(new PsiJavaFileStubImpl("corrupted.classfiles", true));
@@ -512,6 +482,7 @@ public class ClsFileImpl extends ClsRepositoryPsiElement<PsiClassHolderFileStub>
return myIsPhysical;
}
@SuppressWarnings("UnusedDeclaration") // used by Kotlin compiler
public void setPhysical(boolean isPhysical) {
myIsPhysical = isPhysical;
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -18,7 +18,6 @@ package com.intellij.psi.impl.compiled;
import com.intellij.lexer.JavaLexer;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.*;
import com.intellij.psi.impl.source.tree.ElementType;
import com.intellij.psi.impl.source.tree.TreeElement;
import com.intellij.psi.tree.IElementType;
import org.jetbrains.annotations.NotNull;
@@ -27,7 +26,7 @@ class ClsIdentifierImpl extends ClsElementImpl implements PsiIdentifier, PsiJava
private final PsiElement myParent;
private final String myText;
ClsIdentifierImpl(PsiElement parent, String text) {
ClsIdentifierImpl(@NotNull PsiElement parent, String text) {
myParent = parent;
myText = text;
}
@@ -44,23 +43,23 @@ class ClsIdentifierImpl extends ClsElementImpl implements PsiIdentifier, PsiJava
@Override
@NotNull
public PsiElement[] getChildren(){
public PsiElement[] getChildren() {
return PsiElement.EMPTY_ARRAY;
}
@Override
public PsiElement getParent(){
public PsiElement getParent() {
return myParent;
}
private boolean isCorrectName(String name) {
if (name == null) return false;
return StringUtil.isJavaIdentifier(name) && !JavaLexer.isKeyword(name, ((PsiJavaFile)getContainingFile()).getLanguageLevel());
return name != null &&
StringUtil.isJavaIdentifier(name) &&
!JavaLexer.isKeyword(name, ((PsiJavaFile)getContainingFile()).getLanguageLevel());
}
@Override
public void appendMirrorText(final int indentLevel, final StringBuilder buffer){
public void appendMirrorText(int indentLevel, @NotNull StringBuilder buffer) {
String original = getText();
if (isCorrectName(original)) {
buffer.append(original);
@@ -71,12 +70,12 @@ class ClsIdentifierImpl extends ClsElementImpl implements PsiIdentifier, PsiJava
}
@Override
public void setMirror(@NotNull TreeElement element){
setMirrorCheckingType(element, ElementType.IDENTIFIER);
public void setMirror(@NotNull TreeElement element) throws InvalidMirrorException {
setMirrorCheckingType(element, JavaTokenType.IDENTIFIER);
}
@Override
public void accept(@NotNull PsiElementVisitor visitor){
public void accept(@NotNull PsiElementVisitor visitor) {
if (visitor instanceof JavaElementVisitor) {
((JavaElementVisitor)visitor).visitIdentifier(this);
}
@@ -85,6 +84,7 @@ class ClsIdentifierImpl extends ClsElementImpl implements PsiIdentifier, PsiJava
}
}
@Override
public String toString() {
return "PsiIdentifier:" + getText();
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -36,14 +36,15 @@ import org.jetbrains.annotations.Nullable;
import java.util.Map;
public class ClsJavaCodeReferenceElementImpl extends ClsElementImpl implements PsiJavaCodeReferenceElement {
private static final ClsTypeElementImpl[] EMPTY_ARRAY = new ClsTypeElementImpl[0];
@NonNls private static final String EXTENDS_PREFIX = "?extends";
@NonNls private static final String SUPER_PREFIX = "?super";
private final PsiElement myParent;
private final String myCanonicalText;
private final String myQualifiedName;
private final ClsTypeElementImpl[] myTypeParameters; // in right-to-left order
private volatile PsiType[] myTypeParametersCachedTypes = null; // in left-to-right-order
@NonNls private static final String EXTENDS_PREFIX = "?extends";
@NonNls private static final String SUPER_PREFIX = "?super";
public static final ClsJavaCodeReferenceElementImpl[] EMPTY_ARRAY = new ClsJavaCodeReferenceElementImpl[0];
public ClsJavaCodeReferenceElementImpl(PsiElement parent, String canonicalText) {
myParent = parent;
@@ -51,7 +52,7 @@ public class ClsJavaCodeReferenceElementImpl extends ClsElementImpl implements P
myCanonicalText = canonicalText;
final String[] classParametersText = PsiNameHelper.getClassParametersText(canonicalText);
int length = classParametersText.length;
myTypeParameters = length == 0 ? ClsTypeElementImpl.EMPTY_ARRAY : new ClsTypeElementImpl[length];
myTypeParameters = length == 0 ? EMPTY_ARRAY : new ClsTypeElementImpl[length];
for (int i = 0; i < length; i++) {
String s = classParametersText[length - i - 1];
char variance = ClsTypeElementImpl.VARIANCE_NONE;
@@ -138,7 +139,6 @@ public class ClsJavaCodeReferenceElementImpl extends ClsElementImpl implements P
}
}
@Override
@NotNull
public JavaResolveResult advancedResolve(boolean incompleteCode) {
@@ -240,8 +240,7 @@ public class ClsJavaCodeReferenceElementImpl extends ClsElementImpl implements P
public boolean isReferenceTo(PsiElement element) {
if (!(element instanceof PsiClass)) return false;
PsiClass aClass = (PsiClass)element;
return myCanonicalText.equals(aClass.getQualifiedName())
|| getManager().areElementsEquivalent(resolve(), element);
return myCanonicalText.equals(aClass.getQualifiedName()) || getManager().areElementsEquivalent(resolve(), element);
}
@Override
@@ -256,12 +255,12 @@ public class ClsJavaCodeReferenceElementImpl extends ClsElementImpl implements P
}
@Override
public void appendMirrorText(final int indentLevel, final StringBuilder buffer) {
public void appendMirrorText(final int indentLevel, @NotNull final StringBuilder buffer) {
buffer.append(getCanonicalText());
}
@Override
public void setMirror(@NotNull TreeElement element) {
public void setMirror(@NotNull TreeElement element) throws InvalidMirrorException {
setMirrorCheckingType(element, JavaElementType.JAVA_CODE_REFERENCE);
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2011 JetBrains s.r.o.
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -57,12 +57,12 @@ public class ClsJavaTokenImpl extends ClsElementImpl implements PsiJavaToken {
}
@Override
public void appendMirrorText(final int indentLevel, final StringBuilder buffer) {
public void appendMirrorText(int indentLevel, @NotNull StringBuilder buffer) {
buffer.append(getText());
}
@Override
public void setMirror(@NotNull TreeElement element) {
public void setMirror(@NotNull TreeElement element) throws InvalidMirrorException {
setMirrorCheckingType(element, myTokenType);
}
@@ -53,12 +53,12 @@ public class ClsLiteralExpressionImpl extends ClsElementImpl implements PsiLiter
}
@Override
public void appendMirrorText(final int indentLevel, final StringBuilder buffer) {
public void appendMirrorText(int indentLevel, @NotNull StringBuilder buffer) {
buffer.append(getText());
}
@Override
public void setMirror(@NotNull TreeElement element) {
public void setMirror(@NotNull TreeElement element) throws InvalidMirrorException {
setMirrorCheckingType(element, JavaElementType.LITERAL_EXPRESSION);
}
@@ -0,0 +1,74 @@
/*
* Copyright 2000-2012 JetBrains s.r.o.
*
* 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 com.intellij.psi.impl.compiled;
import com.intellij.psi.PsiDocCommentOwner;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiIdentifier;
import com.intellij.psi.PsiNameIdentifierOwner;
import com.intellij.psi.impl.PsiImplUtil;
import com.intellij.psi.javadoc.PsiDocComment;
import com.intellij.psi.stubs.NamedStub;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
public abstract class ClsMemberImpl<T extends NamedStub>
extends ClsRepositoryPsiElement<T>
implements PsiDocCommentOwner, PsiNameIdentifierOwner {
private PsiDocComment myDocComment;
private PsiIdentifier myNameIdentifier;
protected ClsMemberImpl(T stub) {
super(stub);
}
@Override
public PsiDocComment getDocComment() {
if (!isDeprecated()) return null;
synchronized (LAZY_BUILT_LOCK) {
if (myDocComment == null) {
myDocComment = new ClsDocCommentImpl(this);
}
return myDocComment;
}
}
@Override
@NotNull
public PsiIdentifier getNameIdentifier() {
synchronized (LAZY_BUILT_LOCK) {
if (myNameIdentifier == null) {
myNameIdentifier = new ClsIdentifierImpl(this, getName());
}
return myNameIdentifier;
}
}
@Override
@NotNull
public String getName() {
//noinspection ConstantConditions
return getStub().getName();
}
@Override
public PsiElement setName(@NotNull String name) throws IncorrectOperationException {
PsiImplUtil.setName(getNameIdentifier(), name);
return this;
}
}
@@ -17,7 +17,6 @@ package com.intellij.psi.impl.compiled;
import com.intellij.navigation.ItemPresentation;
import com.intellij.navigation.ItemPresentationProviders;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.extensions.Extensions;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.*;
@@ -30,7 +29,6 @@ import com.intellij.psi.impl.java.stubs.JavaStubElementTypes;
import com.intellij.psi.impl.java.stubs.PsiMethodStub;
import com.intellij.psi.impl.source.SourceTreeToPsiMap;
import com.intellij.psi.impl.source.tree.TreeElement;
import com.intellij.psi.javadoc.PsiDocComment;
import com.intellij.psi.scope.PsiScopeProcessor;
import com.intellij.psi.scope.util.PsiScopesUtil;
import com.intellij.psi.search.SearchScope;
@@ -38,22 +36,16 @@ import com.intellij.psi.util.MethodSignature;
import com.intellij.psi.util.MethodSignatureBackedByPsiMethod;
import com.intellij.psi.util.MethodSignatureUtil;
import com.intellij.ui.RowIcon;
import com.intellij.util.IncorrectOperationException;
import com.intellij.util.PlatformIcons;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import javax.swing.*;
import java.util.List;
public class ClsMethodImpl extends ClsRepositoryPsiElement<PsiMethodStub> implements PsiAnnotationMethod {
private static final Logger LOG = Logger.getInstance("#com.intellij.psi.impl.compiled.ClsMethodImpl");
private PsiIdentifier myNameIdentifier = null; //protected by PsiLock
private PsiTypeElement myReturnType = null; //protected by PsiLock
private PsiDocComment myDocComment = null; //protected by PsiLock
private PsiAnnotationMemberValue myDefaultValue = null; //protected by PsiLock
public class ClsMethodImpl extends ClsMemberImpl<PsiMethodStub> implements PsiAnnotationMethod {
private PsiTypeElement myReturnType = null;
private PsiAnnotationMemberValue myDefaultValue = null;
public ClsMethodImpl(final PsiMethodStub stub) {
super(stub);
@@ -62,49 +54,8 @@ public class ClsMethodImpl extends ClsRepositoryPsiElement<PsiMethodStub> implem
@Override
@NotNull
public PsiElement[] getChildren() {
PsiDocComment docComment = getDocComment();
PsiModifierList modifierList = getModifierList();
PsiTypeElement returnType = getReturnTypeElement();
PsiIdentifier name = getNameIdentifier();
PsiParameterList parameterList = getParameterList();
PsiReferenceList throwsList = getThrowsList();
PsiAnnotationMemberValue defaultValue = getDefaultValue();
int count =
(docComment != null ? 1 : 0)
+ (modifierList != null ? 1 : 0)
+ (returnType != null ? 1 : 0)
+ (name != null ? 1 : 0)
+ (parameterList != null ? 1 : 0)
+ (throwsList != null ? 1 : 0)
+ (defaultValue != null ? 1 : 0);
PsiElement[] children = new PsiElement[count];
int offset = 0;
if (docComment != null) {
children[offset++] = docComment;
}
if (modifierList != null) {
children[offset++] = modifierList;
}
if (returnType != null) {
children[offset++] = returnType;
}
if (name != null) {
children[offset++] = name;
}
if (parameterList != null) {
children[offset++] = parameterList;
}
if (throwsList != null) {
children[offset++] = throwsList;
}
if (defaultValue != null) {
children[offset++] = defaultValue;
}
return children;
return getChildren(getDocComment(), getModifierList(), getReturnTypeElement(), getNameIdentifier(), getParameterList(),
getThrowsList(), getDefaultValue());
}
@Override
@@ -112,16 +63,6 @@ public class ClsMethodImpl extends ClsRepositoryPsiElement<PsiMethodStub> implem
return (PsiClass)getParent();
}
@Override
public PsiIdentifier getNameIdentifier() {
synchronized (LAZY_BUILT_LOCK) {
if (myNameIdentifier == null) {
myNameIdentifier = new ClsIdentifierImpl(this, getName());
}
return myNameIdentifier;
}
}
@Override
@NotNull
public PsiMethod[] findSuperMethods() {
@@ -157,31 +98,21 @@ public class ClsMethodImpl extends ClsRepositoryPsiElement<PsiMethodStub> implem
return PsiSuperMethodImplUtil.findDeepestSuperMethods(this);
}
@Override
@NotNull
public String getName() {
return getStub().getName();
}
@Override
@NotNull
public HierarchicalMethodSignature getHierarchicalMethodSignature() {
return PsiSuperMethodImplUtil.getHierarchicalMethodSignature(this);
}
@Override
public PsiElement setName(@NotNull String name) throws IncorrectOperationException {
PsiImplUtil.setName(getNameIdentifier(), name);
return this;
}
@Override
public PsiTypeElement getReturnTypeElement() {
if (isConstructor()) return null;
synchronized (LAZY_BUILT_LOCK) {
if (myReturnType == null) {
String typeText = TypeInfo.createTypeText(getStub().getReturnTypeText(false));
PsiMethodStub stub = getStub();
String typeText = TypeInfo.createTypeText(stub.getReturnTypeText(false));
assert typeText != null : stub;
myReturnType = new ClsTypeElementImpl(this, typeText, ClsTypeElementImpl.VARIANCE_NONE);
}
return myReturnType;
@@ -217,6 +148,11 @@ public class ClsMethodImpl extends ClsRepositoryPsiElement<PsiMethodStub> implem
return getStub().findChildStubByType(JavaStubElementTypes.THROWS_LIST).getPsi();
}
@Override
public PsiTypeParameterList getTypeParameterList() {
return getStub().findChildStubByType(JavaStubElementTypes.TYPE_PARAMETER_LIST).getPsi();
}
@Override
public PsiCodeBlock getBody() {
return null;
@@ -232,27 +168,13 @@ public class ClsMethodImpl extends ClsRepositoryPsiElement<PsiMethodStub> implem
synchronized (LAZY_BUILT_LOCK) {
if (myDefaultValue == null) {
final String text = getStub().getDefaultValueText();
if (StringUtil.isEmpty(text)) return null;
if (text == null || StringUtil.isEmpty(text)) return null;
myDefaultValue = ClsParsingUtil.createMemberValueFromText(text, getManager(), this);
}
return myDefaultValue;
}
}
@Override
public PsiDocComment getDocComment() {
if (!isDeprecated()) return null;
synchronized (LAZY_BUILT_LOCK) {
if (myDocComment == null) {
myDocComment = new ClsDocCommentImpl(this);
}
return myDocComment;
}
}
@Override
public boolean isConstructor() {
return getStub().isConstructor();
@@ -275,8 +197,27 @@ public class ClsMethodImpl extends ClsRepositoryPsiElement<PsiMethodStub> implem
}
@Override
public void appendMirrorText(final int indentLevel, final StringBuilder buffer) {
appendMethodHeader(buffer, indentLevel);
public void appendMirrorText(int indentLevel, @NotNull StringBuilder buffer) {
appendText(getDocComment(), indentLevel, buffer, NEXT_LINE);
appendText(getModifierList(), indentLevel, buffer, "");
appendText(getTypeParameterList(), indentLevel, buffer, " ");
if (!isConstructor()) {
appendText(getReturnTypeElement(), indentLevel, buffer, " ");
}
appendText(getNameIdentifier(), indentLevel, buffer, "");
appendText(getParameterList(), indentLevel, buffer);
PsiReferenceList throwsList = getThrowsList();
if (throwsList.getReferencedTypes().length > 0) {
buffer.append(' ');
appendText(throwsList, indentLevel, buffer);
}
PsiAnnotationMemberValue defaultValue = getDefaultValue();
if (defaultValue != null) {
buffer.append(" default ");
appendText(defaultValue, indentLevel, buffer);
}
if (hasModifierProperty(PsiModifier.ABSTRACT) || hasModifierProperty(PsiModifier.NATIVE)) {
buffer.append(";");
@@ -286,54 +227,26 @@ public class ClsMethodImpl extends ClsRepositoryPsiElement<PsiMethodStub> implem
}
}
private void appendMethodHeader(@NonNls StringBuilder buffer, final int indentLevel) {
ClsDocCommentImpl docComment = (ClsDocCommentImpl)getDocComment();
if (docComment != null) {
docComment.appendMirrorText(indentLevel, buffer);
goNextLine(indentLevel, buffer);
}
((ClsElementImpl)getModifierList()).appendMirrorText(indentLevel, buffer);
((ClsElementImpl)getTypeParameterList()).appendMirrorText(indentLevel, buffer);
@Override
public void setMirror(@NotNull TreeElement element) throws InvalidMirrorException {
setMirrorCheckingType(element, null);
PsiMethod mirror = SourceTreeToPsiMap.treeToPsiNotNull(element);
setMirrorIfPresent(getDocComment(), mirror.getDocComment());
setMirror(getModifierList(), mirror.getModifierList());
setMirror(getTypeParameterList(), mirror.getTypeParameterList());
if (!isConstructor()) {
((ClsElementImpl)getReturnTypeElement()).appendMirrorText(indentLevel, buffer);
buffer.append(' ');
}
((ClsElementImpl)getNameIdentifier()).appendMirrorText(indentLevel, buffer);
((ClsElementImpl)getParameterList()).appendMirrorText(indentLevel, buffer);
final PsiReferenceList throwsList = getThrowsList();
if (throwsList.getReferencedTypes().length > 0) {
buffer.append(' ');
((ClsElementImpl)throwsList).appendMirrorText(indentLevel, buffer);
setMirror(getReturnTypeElement(), mirror.getReturnTypeElement());
}
setMirror(getNameIdentifier(), mirror.getNameIdentifier());
setMirror(getParameterList(), mirror.getParameterList());
setMirror(getThrowsList(), mirror.getThrowsList());
PsiAnnotationMemberValue defaultValue = getDefaultValue();
if (defaultValue != null) {
buffer.append(" default ");
((ClsElementImpl)defaultValue).appendMirrorText(indentLevel, buffer);
}
}
@Override
public void setMirror(@NotNull TreeElement element) {
setMirrorCheckingType(element, null);
PsiMethod mirror = (PsiMethod)SourceTreeToPsiMap.treeElementToPsi(element);
if (getDocComment() != null) {
((ClsElementImpl)getDocComment()).setMirror((TreeElement)SourceTreeToPsiMap.psiElementToTree(mirror.getDocComment()));
}
((ClsElementImpl)getModifierList()).setMirror((TreeElement)SourceTreeToPsiMap.psiElementToTree(mirror.getModifierList()));
if (!isConstructor() && mirror.getReturnTypeElement() != null) {
((ClsElementImpl)getReturnTypeElement()).setMirror(
(TreeElement)SourceTreeToPsiMap.psiElementToTree(mirror.getReturnTypeElement()));
}
((ClsElementImpl)getNameIdentifier()).setMirror((TreeElement)SourceTreeToPsiMap.psiElementToTree(mirror.getNameIdentifier()));
((ClsElementImpl)getParameterList()).setMirror((TreeElement)SourceTreeToPsiMap.psiElementToTree(mirror.getParameterList()));
((ClsElementImpl)getThrowsList()).setMirror((TreeElement)SourceTreeToPsiMap.psiElementToTree(mirror.getThrowsList()));
((ClsElementImpl)getTypeParameterList()).setMirror(
(TreeElement)SourceTreeToPsiMap.psiElementToTree(mirror.getTypeParameterList()));
if (getDefaultValue() != null) {
LOG.assertTrue(mirror instanceof PsiAnnotationMethod);
((ClsElementImpl)getDefaultValue()).setMirror((TreeElement)SourceTreeToPsiMap.psiElementToTree(((PsiAnnotationMethod)mirror).getDefaultValue()));
assert mirror instanceof PsiAnnotationMethod : this;
setMirror(defaultValue, ((PsiAnnotationMethod)mirror).getDefaultValue());
}
}
@@ -347,10 +260,6 @@ public class ClsMethodImpl extends ClsRepositoryPsiElement<PsiMethodStub> implem
}
}
public String toString() {
return "PsiMethod:" + getName();
}
@Override
public boolean processDeclarations(@NotNull PsiScopeProcessor processor,
@NotNull ResolveState state,
@@ -395,11 +304,6 @@ public class ClsMethodImpl extends ClsRepositoryPsiElement<PsiMethodStub> implem
return method != null ? method.getNavigationElement() : this;
}
@Override
public PsiTypeParameterList getTypeParameterList() {
return getStub().findChildStubByType(JavaStubElementTypes.TYPE_PARAMETER_LIST).getPsi();
}
@Override
public boolean hasTypeParameters() {
return PsiImplUtil.hasTypeParameters(this);
@@ -442,4 +346,9 @@ public class ClsMethodImpl extends ClsRepositoryPsiElement<PsiMethodStub> implem
protected boolean isVisibilitySupported() {
return true;
}
@Override
public String toString() {
return "PsiMethod:" + getName();
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -15,7 +15,6 @@
*/
package com.intellij.psi.impl.compiled;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.psi.*;
import com.intellij.psi.impl.PsiImplUtil;
import com.intellij.psi.impl.java.stubs.JavaStubElementTypes;
@@ -28,9 +27,8 @@ import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
@SuppressWarnings("ForLoopReplaceableByForEach")
public class ClsModifierListImpl extends ClsRepositoryPsiElement<PsiModifierListStub> implements PsiModifierList {
private static final Logger LOG = Logger.getInstance("#com.intellij.psi.impl.compiled.ClsModifierListImpl");
public ClsModifierListImpl(final PsiModifierListStub stub) {
super(stub);
}
@@ -90,28 +88,15 @@ public class ClsModifierListImpl extends ClsRepositoryPsiElement<PsiModifierList
throw new IncorrectOperationException(CAN_NOT_MODIFY_MESSAGE);
}
private boolean isAnnotationFormattingAllowed() {
final PsiElement element = getParent();
return element instanceof PsiClass
|| element instanceof PsiMethod
|| element instanceof PsiField;
}
@Override
public void appendMirrorText(final int indentLevel, final StringBuilder buffer) {
final PsiAnnotation[] annotations = getAnnotations();
final boolean formattingAllowed = isAnnotationFormattingAllowed();
for (PsiAnnotation annotation : annotations) {
((ClsAnnotationImpl)annotation).appendMirrorText(indentLevel, buffer);
if (formattingAllowed) {
goNextLine(indentLevel, buffer);
}
else {
buffer.append(' ');
}
}
public void appendMirrorText(int indentLevel, @NotNull StringBuilder buffer) {
final PsiElement parent = getParent();
final PsiAnnotation[] annotations = getAnnotations();
final boolean separateAnnotations = parent instanceof PsiClass || parent instanceof PsiMethod || parent instanceof PsiField;
for (int i = 0; i < annotations.length; i++) {
appendText(annotations[i], indentLevel, buffer, separateAnnotations ? NEXT_LINE : " ");
}
//TODO : filtering & ordering modifiers can go to CodeStyleManager
final boolean isClass = parent instanceof PsiClass;
@@ -169,15 +154,9 @@ public class ClsModifierListImpl extends ClsRepositoryPsiElement<PsiModifierList
}
@Override
public void setMirror(@NotNull TreeElement element) {
public void setMirror(@NotNull TreeElement element) throws InvalidMirrorException {
setMirrorCheckingType(element, JavaElementType.MODIFIER_LIST);
PsiElement[] mirrorAnnotations = SourceTreeToPsiMap.<PsiModifierList>treeToPsiNotNull(element).getAnnotations();
PsiAnnotation[] annotations = getAnnotations();
LOG.assertTrue(annotations.length == mirrorAnnotations.length);
for (int i = 0; i < annotations.length; i++) {
((ClsElementImpl)annotations[i]).setMirror(SourceTreeToPsiMap.psiToTreeNotNull(mirrorAnnotations[i]));
}
setMirrors(getAnnotations(), SourceTreeToPsiMap.<PsiModifierList>treeToPsiNotNull(element).getAnnotations());
}
@Override
@@ -190,6 +169,7 @@ public class ClsModifierListImpl extends ClsRepositoryPsiElement<PsiModifierList
}
}
@Override
public String toString() {
return "PsiModifierList";
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -15,53 +15,51 @@
*/
package com.intellij.psi.impl.compiled;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.psi.*;
import com.intellij.psi.impl.source.SourceTreeToPsiMap;
import com.intellij.psi.impl.source.tree.TreeElement;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* @author ven
*/
public class ClsNameValuePairImpl extends ClsElementImpl implements PsiNameValuePair {
private static final Logger LOG = Logger.getInstance("com.intellij.psi.impl.compiled.ClsNameValuePairImpl");
private final ClsElementImpl myParent;
private final ClsIdentifierImpl myNameIdentifier;
private final PsiAnnotationMemberValue myMemberValue;
public ClsNameValuePairImpl(ClsElementImpl parent, String name, PsiAnnotationMemberValue value) {
public ClsNameValuePairImpl(@NotNull ClsElementImpl parent, @Nullable String name, @NotNull PsiAnnotationMemberValue value) {
myParent = parent;
myNameIdentifier = new ClsIdentifierImpl(this, name);
myNameIdentifier = name != null ? new ClsIdentifierImpl(this, name) : null;
myMemberValue = ClsParsingUtil.getMemberValue(value, this);
}
@Override
public void appendMirrorText(final int indentLevel, final StringBuilder buffer) {
if (myNameIdentifier.getText() != null) {
myNameIdentifier.appendMirrorText(0, buffer);
buffer.append(" = ");
}
((ClsElementImpl)myMemberValue).appendMirrorText(0, buffer);
public void appendMirrorText(int indentLevel, @NotNull StringBuilder buffer) {
appendText(myNameIdentifier, 0, buffer, " = ");
appendText(myMemberValue, 0, buffer);
}
@Override
public void setMirror(@NotNull TreeElement element) {
public void setMirror(@NotNull TreeElement element) throws InvalidMirrorException {
setMirrorCheckingType(element, null);
PsiNameValuePair mirror = (PsiNameValuePair)SourceTreeToPsiMap.treeElementToPsi(element);
final PsiIdentifier mirrorIdentifier = mirror.getNameIdentifier();
if (mirrorIdentifier != null) {
((ClsElementImpl)getNameIdentifier()).setMirror((TreeElement)SourceTreeToPsiMap.psiElementToTree(mirrorIdentifier));
}
((ClsElementImpl)getValue()).setMirror((TreeElement)SourceTreeToPsiMap.psiElementToTree(mirror.getValue()));
PsiNameValuePair mirror = SourceTreeToPsiMap.treeToPsiNotNull(element);
setMirrorIfPresent(getNameIdentifier(), mirror.getNameIdentifier());
setMirrorIfPresent(getValue(), mirror.getValue());
}
@Override
@NotNull
public PsiElement[] getChildren() {
return new PsiElement[]{myNameIdentifier, myMemberValue};
if (myNameIdentifier != null) {
return new PsiElement[]{myNameIdentifier, myMemberValue};
}
else {
return new PsiElement[]{myMemberValue};
}
}
@Override
@@ -86,7 +84,7 @@ public class ClsNameValuePairImpl extends ClsElementImpl implements PsiNameValue
@Override
public String getName() {
return myNameIdentifier.getText();
return myNameIdentifier != null ? myNameIdentifier.getText() : null;
}
@Override
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -15,24 +15,29 @@
*/
package com.intellij.psi.impl.compiled;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.psi.*;
import com.intellij.psi.impl.source.tree.ElementType;
import com.intellij.psi.impl.source.tree.JavaElementType;
import com.intellij.psi.impl.source.tree.TreeElement;
import org.jetbrains.annotations.NotNull;
class ClsPackageStatementImpl extends ClsElementImpl implements PsiPackageStatement {
private static final Logger LOG = Logger.getInstance("#com.intellij.psi.impl.compiled.ClsPackageStatementImpl");
private final ClsFileImpl myFile;
private final String myPackageName;
public ClsPackageStatementImpl(@NotNull ClsFileImpl file) {
myFile = file;
final PsiClass[] psiClasses = file.getClasses();
String className = psiClasses.length > 0 ? psiClasses[0].getQualifiedName() : "";
int index = className.lastIndexOf('.');
myPackageName = index < 0 ? null : className.substring(0, index);
String packageName = null;
PsiClass[] psiClasses = file.getClasses();
if (psiClasses.length > 0) {
String className = psiClasses[0].getQualifiedName();
if (className != null) {
int index = className.lastIndexOf('.');
if (index >= 0) {
packageName = className.substring(0, index);
}
}
}
myPackageName = packageName;
}
@Override
@@ -45,8 +50,7 @@ class ClsPackageStatementImpl extends ClsElementImpl implements PsiPackageStatem
*/
@Override
public PsiJavaCodeReferenceElement getPackageReference() {
LOG.error("method not implemented");
return null;
throw new UnsupportedOperationException("Method not implemented");
}
/**
@@ -54,8 +58,7 @@ class ClsPackageStatementImpl extends ClsElementImpl implements PsiPackageStatem
*/
@Override
public PsiModifierList getAnnotationList() {
LOG.error("method not implemented");
return null;
throw new UnsupportedOperationException("Method not implemented");
}
/**
@@ -64,8 +67,7 @@ class ClsPackageStatementImpl extends ClsElementImpl implements PsiPackageStatem
@Override
@NotNull
public PsiElement[] getChildren() {
LOG.error("method not implemented");
return null;
throw new UnsupportedOperationException("Method not implemented");
}
@Override
@@ -74,15 +76,15 @@ class ClsPackageStatementImpl extends ClsElementImpl implements PsiPackageStatem
}
@Override
public void appendMirrorText(final int indentLevel, final StringBuilder buffer) {
buffer.append("package ");
buffer.append(getPackageName());
buffer.append(";");
public void appendMirrorText(final int indentLevel, @NotNull final StringBuilder buffer) {
if (myPackageName != null) {
buffer.append("package ").append(getPackageName()).append(";");
}
}
@Override
public void setMirror(@NotNull TreeElement element) {
setMirrorCheckingType(element, ElementType.PACKAGE_STATEMENT);
public void setMirror(@NotNull TreeElement element) throws InvalidMirrorException {
setMirrorCheckingType(element, JavaElementType.PACKAGE_STATEMENT);
}
@Override
@@ -95,6 +97,7 @@ class ClsPackageStatementImpl extends ClsElementImpl implements PsiPackageStatem
}
}
@Override
public String toString() {
return "PsiPackageStatement:" + getPackageName();
}
@@ -15,13 +15,11 @@
*/
package com.intellij.psi.impl.compiled;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.DumbService;
import com.intellij.psi.*;
import com.intellij.psi.codeStyle.JavaCodeStyleManager;
import com.intellij.psi.codeStyle.VariableKind;
import com.intellij.psi.impl.ElementPresentationUtil;
import com.intellij.psi.impl.PsiImplUtil;
import com.intellij.psi.impl.cache.TypeInfo;
import com.intellij.psi.impl.java.stubs.JavaStubElementTypes;
import com.intellij.psi.impl.java.stubs.PsiParameterStub;
@@ -38,17 +36,13 @@ import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import javax.swing.*;
import java.util.Arrays;
public class ClsParameterImpl extends ClsRepositoryPsiElement<PsiParameterStub> implements PsiParameter {
private static final Logger LOG = Logger.getInstance("#com.intellij.psi.impl.compiled.ClsParameterImpl");
private PsiTypeElement myType = null;
private String myName;
private String myMirrorName = null;
private volatile PsiTypeElement myType; //guarded by PsiLock
private String myMirrorName = null; //guarded by PsiLock
private String myName = null; //no point guarding
public static final ClsParameterImpl[] EMPTY_ARRAY = new ClsParameterImpl[0];
public ClsParameterImpl(final PsiParameterStub stub) {
public ClsParameterImpl(@NotNull PsiParameterStub stub) {
super(stub);
}
@@ -81,22 +75,21 @@ public class ClsParameterImpl extends ClsRepositoryPsiElement<PsiParameterStub>
@Override
public PsiElement setName(@NotNull String name) throws IncorrectOperationException {
PsiImplUtil.setName(getNameIdentifier(), name);
return this;
throw new IncorrectOperationException(CAN_NOT_MODIFY_MESSAGE);
}
@Override
@NotNull
public PsiTypeElement getTypeElement() {
if (myType == null) {
synchronized (LAZY_BUILT_LOCK) {
if (myType == null) {
myType = new ClsTypeElementImpl(this, TypeInfo.createTypeText(getStub().getType(false)), ClsTypeElementImpl.VARIANCE_NONE);
}
synchronized (LAZY_BUILT_LOCK) {
if (myType == null) {
PsiParameterStub stub = getStub();
String typeText = TypeInfo.createTypeText(stub.getType(false));
assert typeText != null : stub;
myType = new ClsTypeElementImpl(this, typeText, ClsTypeElementImpl.VARIANCE_NONE);
}
return myType;
}
return myType;
}
@Override
@@ -138,52 +131,52 @@ public class ClsParameterImpl extends ClsRepositoryPsiElement<PsiParameterStub>
}
@Override
public void appendMirrorText(final int indentLevel, final StringBuilder buffer) {
public void appendMirrorText(int indentLevel, @NotNull StringBuilder buffer) {
PsiAnnotation[] annotations = getModifierList().getAnnotations();
for (PsiAnnotation annotation : annotations) {
((ClsAnnotationImpl)annotation).appendMirrorText(indentLevel, buffer);
buffer.append(" ");
appendText(annotation, indentLevel, buffer);
buffer.append(' ');
}
((ClsElementImpl)getTypeElement()).appendMirrorText(indentLevel, buffer);
buffer.append(" ");
appendText(getTypeElement(), indentLevel, buffer, " ");
buffer.append(getMirrorName());
}
private String getMirrorName() {
synchronized (LAZY_BUILT_LOCK) {
if (myMirrorName == null) {
final PsiParameterStubImpl parameterStub = (PsiParameterStubImpl) getStub();
if (!parameterStub.isAutoGeneratedName()) {
myMirrorName = parameterStub.getName();
return myMirrorName;
}
PsiParameter[] parms = ((PsiParameterList) getParent()).getParameters();
if (DumbService.getInstance(getProject()).isDumb()) {
return "p" + Arrays.asList(parms).indexOf(this);
}
@NonNls String name;
String[] nameSuggestions = JavaCodeStyleManager.getInstance(getProject()).suggestVariableName(VariableKind.PARAMETER, null,
null, getType())
.names;
@NonNls String name = "p";
if (nameSuggestions.length > 0) {
name = nameSuggestions[0];
final PsiParameterStubImpl stub = (PsiParameterStubImpl)getStub();
if (!stub.isAutoGeneratedName() || DumbService.getInstance(getProject()).isDumb()) {
name = stub.getName();
}
else {
JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(getProject());
String[] nameSuggestions = codeStyleManager.suggestVariableName(VariableKind.PARAMETER, null, null, getType()).names;
AttemptsLoop:
while (true) {
for (PsiParameter parm : parms) {
if (parm == this) break AttemptsLoop;
String name1 = ((ClsParameterImpl) parm).getMirrorName();
if (name.equals(name1)) {
name = nextName(name);
continue AttemptsLoop;
name = "p";
if (nameSuggestions.length > 0) {
name = nameSuggestions[0];
}
PsiParameter[] parameters = ((PsiParameterList) getParent()).getParameters();
AttemptsLoop:
while (true) {
for (PsiParameter parameter : parameters) {
if (parameter == this) break AttemptsLoop;
String name1 = ((ClsParameterImpl) parameter).getMirrorName();
if (name.equals(name1)) {
name = nextName(name);
continue AttemptsLoop;
}
}
}
}
assert name != null : stub;
myMirrorName = name;
}
return myMirrorName;
}
}
@@ -207,18 +200,18 @@ public class ClsParameterImpl extends ClsRepositoryPsiElement<PsiParameterStub>
return name.substring(0, name.length() - count) + n;
}
catch (NumberFormatException e) {
LOG.assertTrue(false);
assert false : e.getMessage();
return null;
}
}
@Override
public void setMirror(@NotNull TreeElement element) {
public void setMirror(@NotNull TreeElement element) throws InvalidMirrorException {
setMirrorCheckingType(element, null);
PsiParameter mirror = (PsiParameter)SourceTreeToPsiMap.treeElementToPsi(element);
((ClsElementImpl)getModifierList()).setMirror((TreeElement)SourceTreeToPsiMap.psiElementToTree(mirror.getModifierList()));
((ClsElementImpl)getTypeElement()).setMirror((TreeElement)SourceTreeToPsiMap.psiElementToTree(mirror.getTypeElement()));
PsiParameter mirror = SourceTreeToPsiMap.treeToPsiNotNull(element);
setMirror(getModifierList(), mirror.getModifierList());
setMirror(getTypeElement(), mirror.getTypeElement());
}
@Override
@@ -231,10 +224,6 @@ public class ClsParameterImpl extends ClsRepositoryPsiElement<PsiParameterStub>
}
}
public String toString() {
return "PsiParameter";
}
@Override
@NotNull
public PsiElement getDeclarationScope() {
@@ -275,4 +264,9 @@ public class ClsParameterImpl extends ClsRepositoryPsiElement<PsiParameterStub>
public PsiType getTypeNoResolve() {
return getType();
}
@Override
public String toString() {
return "PsiParameter";
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -15,7 +15,6 @@
*/
package com.intellij.psi.impl.compiled;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.psi.JavaElementVisitor;
import com.intellij.psi.PsiElementVisitor;
import com.intellij.psi.PsiParameter;
@@ -25,13 +24,10 @@ import com.intellij.psi.impl.java.stubs.JavaStubElementTypes;
import com.intellij.psi.impl.java.stubs.PsiParameterListStub;
import com.intellij.psi.impl.source.SourceTreeToPsiMap;
import com.intellij.psi.impl.source.tree.TreeElement;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
public class ClsParameterListImpl extends ClsRepositoryPsiElement<PsiParameterListStub> implements PsiParameterList {
private static final Logger LOG = Logger.getInstance("#com.intellij.psi.impl.compiled.ClsParameterListImpl");
public ClsParameterListImpl(final PsiParameterListStub stub) {
public ClsParameterListImpl(@NotNull PsiParameterListStub stub) {
super(stub);
}
@@ -43,7 +39,7 @@ public class ClsParameterListImpl extends ClsRepositoryPsiElement<PsiParameterLi
@Override
public int getParameterIndex(PsiParameter parameter) {
LOG.assertTrue(parameter.getParent() == this);
assert parameter.getParent() == this;
return PsiImplUtil.getParameterIndex(parameter, this);
}
@@ -53,29 +49,20 @@ public class ClsParameterListImpl extends ClsRepositoryPsiElement<PsiParameterLi
}
@Override
public void appendMirrorText(final int indentLevel, final StringBuilder buffer) {
public void appendMirrorText(int indentLevel, @NotNull StringBuilder buffer) {
buffer.append('(');
PsiParameter[] parameters = getParameters();
for (int i = 0; i < parameters.length; i++) {
PsiParameter parm = parameters[i];
if (i > 0) buffer.append(", ");
((ClsElementImpl)parm).appendMirrorText(indentLevel, buffer);
appendText(parameters[i], indentLevel, buffer);
}
buffer.append(')');
}
@Override
public void setMirror(@NotNull TreeElement element) {
public void setMirror(@NotNull TreeElement element) throws InvalidMirrorException {
setMirrorCheckingType(element, null);
PsiParameter[] parms = getParameters();
PsiParameter[] parmMirrors = ((PsiParameterList)SourceTreeToPsiMap.treeElementToPsi(element)).getParameters();
LOG.assertTrue(parms.length == parmMirrors.length);
if (parms.length == parmMirrors.length) {
for (int i = 0; i < parms.length; i++) {
((ClsElementImpl)parms[i]).setMirror((TreeElement)SourceTreeToPsiMap.psiElementToTree(parmMirrors[i]));
}
}
setMirrors(getParameters(), SourceTreeToPsiMap.<PsiParameterList>treeToPsiNotNull(element).getParameters());
}
@Override
@@ -88,7 +75,7 @@ public class ClsParameterListImpl extends ClsRepositoryPsiElement<PsiParameterLi
}
}
@NonNls
@Override
public String toString() {
return "PsiParameterList";
}
@@ -132,7 +132,7 @@ public class ClsParsingUtil {
}
else if (expr instanceof PsiClassObjectAccessExpression) {
final String canonicalClassText = ((PsiClassObjectAccessExpression)expr).getOperand().getType().getCanonicalText();
return new ClsClassObjectAccessExpressionImpl(canonicalClassText, parent);
return new ClsClassObjectAccessExpressionImpl(parent, canonicalClassText);
}
else if (expr instanceof PsiReferenceExpression) {
return new ClsReferenceExpressionImpl(parent, (PsiReferenceExpression)expr);
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -79,12 +79,12 @@ public class ClsPrefixExpressionImpl extends ClsElementImpl implements PsiPrefix
}
@Override
public void appendMirrorText(final int indentLevel, final StringBuilder buffer) {
public void appendMirrorText(int indentLevel, @NotNull StringBuilder buffer) {
buffer.append(getText());
}
@Override
public void setMirror(@NotNull TreeElement element) {
public void setMirror(@NotNull TreeElement element) throws InvalidMirrorException {
setMirrorCheckingType(element, JavaElementType.PREFIX_EXPRESSION);
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -15,10 +15,9 @@
*/
package com.intellij.psi.impl.compiled;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.*;
import com.intellij.psi.impl.source.tree.ElementType;
import com.intellij.psi.impl.source.tree.JavaElementType;
import com.intellij.psi.impl.source.tree.TreeElement;
import com.intellij.psi.scope.PsiScopeProcessor;
import com.intellij.util.IncorrectOperationException;
@@ -26,8 +25,6 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class ClsReferenceExpressionImpl extends ClsElementImpl implements PsiReferenceExpression {
private static final Logger LOG = Logger.getInstance("#com.intellij.psi.impl.compiled.ClsReferenceExpressionImpl");
private final ClsElementImpl myParent;
private final PsiReferenceExpression myPatternExpression;
private final PsiReferenceExpression myQualifier;
@@ -39,10 +36,10 @@ public class ClsReferenceExpressionImpl extends ClsElementImpl implements PsiRef
myPatternExpression = patternExpression;
PsiReferenceExpression patternQualifier = (PsiReferenceExpression)myPatternExpression.getQualifierExpression();
if (patternQualifier != null){
if (patternQualifier != null) {
myQualifier = new ClsReferenceExpressionImpl(this, patternQualifier);
}
else{
else {
myQualifier = null;
}
@@ -83,10 +80,10 @@ public class ClsReferenceExpressionImpl extends ClsElementImpl implements PsiRef
@Override
@NotNull
public PsiElement[] getChildren() {
if (myQualifier != null){
if (myQualifier != null) {
return new PsiElement[]{myQualifier, myNameElement};
}
else{
else {
return new PsiElement[]{myNameElement};
}
}
@@ -113,19 +110,17 @@ public class ClsReferenceExpressionImpl extends ClsElementImpl implements PsiRef
@Override
@NotNull
public JavaResolveResult advancedResolve(boolean incompleteCode){
public JavaResolveResult advancedResolve(boolean incompleteCode) {
return myPatternExpression.advancedResolve(incompleteCode);
}
@Override
@NotNull
public JavaResolveResult[] multiResolve(boolean incompleteCode){
public JavaResolveResult[] multiResolve(boolean incompleteCode) {
final JavaResolveResult result = advancedResolve(incompleteCode);
if(result != JavaResolveResult.EMPTY) return new JavaResolveResult[]{result};
return JavaResolveResult.EMPTY_ARRAY;
return result != JavaResolveResult.EMPTY ? new JavaResolveResult[]{result} : JavaResolveResult.EMPTY_ARRAY;
}
@Override
public PsiElement getElement() {
return this;
@@ -184,13 +179,13 @@ public class ClsReferenceExpressionImpl extends ClsElementImpl implements PsiRef
}
@Override
public void appendMirrorText(final int indentLevel, final StringBuilder buffer) {
public void appendMirrorText(int indentLevel, @NotNull StringBuilder buffer) {
buffer.append(getText());
}
@Override
public void setMirror(@NotNull TreeElement element) {
setMirrorCheckingType(element, ElementType.REFERENCE_EXPRESSION);
public void setMirror(@NotNull TreeElement element) throws InvalidMirrorException {
setMirrorCheckingType(element, JavaElementType.REFERENCE_EXPRESSION);
}
@Override
@@ -203,10 +198,6 @@ public class ClsReferenceExpressionImpl extends ClsElementImpl implements PsiRef
}
}
public String toString() {
return "PsiReferenceExpression:" + getText();
}
@Override
@NotNull
public PsiType[] getTypeParameters() {
@@ -217,4 +208,9 @@ public class ClsReferenceExpressionImpl extends ClsElementImpl implements PsiRef
public PsiElement getQualifier() {
return getQualifierExpression();
}
@Override
public String toString() {
return "PsiReferenceExpression:" + getText();
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -15,7 +15,6 @@
*/
package com.intellij.psi.impl.compiled;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.psi.*;
import com.intellij.psi.impl.java.stubs.PsiClassReferenceListStub;
import com.intellij.psi.impl.source.SourceTreeToPsiMap;
@@ -24,10 +23,11 @@ import org.jetbrains.annotations.NotNull;
@SuppressWarnings({"HardCodedStringLiteral"})
public class ClsReferenceListImpl extends ClsRepositoryPsiElement<PsiClassReferenceListStub> implements PsiReferenceList {
private static final Logger LOG = Logger.getInstance("#com.intellij.psi.impl.compiled.ClsReferenceListImpl");
private ClsJavaCodeReferenceElementImpl[] myRefs; //guarded by PsiLock
private static final ClsJavaCodeReferenceElementImpl[] EMPTY_REFS_ARRAY = new ClsJavaCodeReferenceElementImpl[0];
public ClsReferenceListImpl(final PsiClassReferenceListStub stub) {
private ClsJavaCodeReferenceElementImpl[] myRefs;
public ClsReferenceListImpl(@NotNull PsiClassReferenceListStub stub) {
super(stub);
}
@@ -37,13 +37,15 @@ public class ClsReferenceListImpl extends ClsRepositoryPsiElement<PsiClassRefere
synchronized (LAZY_BUILT_LOCK) {
if (myRefs == null) {
final String[] strings = getStub().getReferencedNames();
ClsJavaCodeReferenceElementImpl[] res = strings.length == 0 ?
ClsJavaCodeReferenceElementImpl.EMPTY_ARRAY :
new ClsJavaCodeReferenceElementImpl[strings.length];
for (int i = 0; i < res.length; i++) {
res[i] = new ClsJavaCodeReferenceElementImpl(this, strings[i]);
if (strings.length > 0) {
myRefs = new ClsJavaCodeReferenceElementImpl[strings.length];
for (int i = 0; i < strings.length; i++) {
myRefs[i] = new ClsJavaCodeReferenceElementImpl(this, strings[i]);
}
}
else {
myRefs = EMPTY_REFS_ARRAY;
}
myRefs = res;
}
return myRefs;
}
@@ -67,7 +69,7 @@ public class ClsReferenceListImpl extends ClsRepositoryPsiElement<PsiClassRefere
}
@Override
public void appendMirrorText(final int indentLevel, final StringBuilder buffer) {
public void appendMirrorText(int indentLevel, @NotNull StringBuilder buffer) {
final String[] names = getStub().getReferencedNames();
if (names.length != 0) {
final Role role = getStub().getRole();
@@ -91,17 +93,9 @@ public class ClsReferenceListImpl extends ClsRepositoryPsiElement<PsiClassRefere
}
@Override
public void setMirror(@NotNull TreeElement element) {
public void setMirror(@NotNull TreeElement element) throws InvalidMirrorException {
setMirrorCheckingType(element, null);
PsiJavaCodeReferenceElement[] refs = getReferenceElements();
PsiJavaCodeReferenceElement[] refMirrors = ((PsiReferenceList)SourceTreeToPsiMap.treeElementToPsi(element)).getReferenceElements();
LOG.assertTrue(refs.length == refMirrors.length);
if (refs.length == refMirrors.length) {
for (int i = 0; i < refs.length; i++) {
((ClsElementImpl)refs[i]).setMirror((TreeElement)SourceTreeToPsiMap.psiElementToTree(refMirrors[i]));
}
}
setMirrors(getReferenceElements(), SourceTreeToPsiMap.<PsiReferenceList>treeToPsiNotNull(element).getReferenceElements());
}
@Override
@@ -114,9 +108,8 @@ public class ClsReferenceListImpl extends ClsRepositoryPsiElement<PsiClassRefere
}
}
@Override
public String toString() {
return "PsiReferenceList";
}
}
@@ -1,98 +0,0 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
*
* 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 com.intellij.psi.impl.compiled;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.psi.*;
import com.intellij.psi.impl.PsiImplUtil;
import com.intellij.psi.impl.source.SourceTreeToPsiMap;
import com.intellij.psi.impl.source.tree.TreeElement;
import org.jetbrains.annotations.NotNull;
/**
* @author max
*/
public class ClsReferenceParametersListImpl extends ClsElementImpl implements PsiReferenceParameterList {
private static final Logger LOG = Logger.getInstance("#com.intellij.psi.impl.compiled.ClsReferenceParametersListImpl");
private final PsiElement myParent;
private final PsiTypeElement[] myTypeElements;
public ClsReferenceParametersListImpl(PsiElement parent, PsiTypeElement[] typeElements) {
myParent = parent;
myTypeElements = typeElements;
}
@Override
public void appendMirrorText(final int indentLevel, final StringBuilder buffer) {
if (myTypeElements.length != 0) {
buffer.append('<');
for (int i = 0; i < myTypeElements.length; i++) {
if (i > 0) buffer.append(" ,");
ClsTypeElementImpl typeElement = (ClsTypeElementImpl)myTypeElements[i];
typeElement.appendMirrorText(indentLevel, buffer);
}
buffer.append('>');
}
}
@Override
public void setMirror(@NotNull TreeElement element) {
setMirrorCheckingType(element, null);
PsiTypeElement[] typeElements = getTypeParameterElements();
PsiTypeElement[] typeMirrors = ((PsiReferenceParameterList)SourceTreeToPsiMap.treeElementToPsi(element)).getTypeParameterElements();
LOG.assertTrue(typeElements.length == typeMirrors.length);
if (typeElements.length == typeMirrors.length) {
for (int i = 0; i < typeElements.length; i++) {
((ClsElementImpl)typeElements[i]).setMirror((TreeElement)SourceTreeToPsiMap.psiElementToTree(typeMirrors[i]));
}
}
}
@Override
@NotNull
public PsiElement[] getChildren() {
return myTypeElements;
}
@Override
@NotNull
public PsiTypeElement[] getTypeParameterElements() {
return myTypeElements;
}
@Override
@NotNull
public PsiType[] getTypeArguments() {
return PsiImplUtil.typesByTypeElements(myTypeElements);
}
@Override
public PsiElement getParent() {
return myParent;
}
@Override
public void accept(@NotNull PsiElementVisitor visitor) {
if (visitor instanceof JavaElementVisitor) {
((JavaElementVisitor)visitor).visitReferenceParameterList(this);
}
else {
visitor.visitElement(this);
}
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -15,7 +15,6 @@
*/
package com.intellij.psi.impl.compiled;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.psi.*;
import com.intellij.psi.impl.PsiImplUtil;
import com.intellij.psi.impl.source.PsiClassReferenceType;
@@ -25,24 +24,21 @@ import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
public class ClsTypeElementImpl extends ClsElementImpl implements PsiTypeElement {
public static final ClsTypeElementImpl[] EMPTY_ARRAY = new ClsTypeElementImpl[0];
private static final Logger LOG = Logger.getInstance("#com.intellij.psi.impl.compiled.ClsTypeImpl");
static final char VARIANCE_NONE = '\0';
static final char VARIANCE_EXTENDS = '+';
static final char VARIANCE_SUPER = '-';
static final char VARIANCE_INVARIANT = '*';
private final PsiElement myParent;
private final String myTypeText;
private volatile ClsElementImpl myChild = null;
private boolean myChildSet = false; //guarded by PsiLock
private volatile PsiType myCachedType;
private final char myVariance;
@NonNls private static final String VARIANCE_EXTENDS_PREFIX = "? extends ";
@NonNls private static final String VARIANCE_SUPER_PREFIX = "? super ";
public ClsTypeElementImpl(PsiElement parent, String typeText, char variance) {
private final PsiElement myParent;
private final String myTypeText;
private volatile ClsElementImpl myChild = null;
private boolean myChildSet = false;
private volatile PsiType myCachedType;
private final char myVariance;
public ClsTypeElementImpl(@NotNull PsiElement parent, String typeText, char variance) {
myParent = parent;
myTypeText = typeText;
myVariance = variance;
@@ -50,25 +46,24 @@ public class ClsTypeElementImpl extends ClsElementImpl implements PsiTypeElement
@Override
@NotNull
public PsiElement[] getChildren(){
public PsiElement[] getChildren() {
loadChild();
if (myChild == null) return PsiElement.EMPTY_ARRAY;
return new PsiElement[] {myChild};
return myChild != null ? new PsiElement[]{myChild} : PsiElement.EMPTY_ARRAY;
}
@Override
public PsiElement getParent(){
public PsiElement getParent() {
return myParent;
}
@Override
public String getText(){
public String getText() {
final String shortClassName = PsiNameHelper.getShortClassName(myTypeText);
return decorateTypeText(shortClassName);
}
private String decorateTypeText(final String shortClassName) {
switch(myVariance) {
switch (myVariance) {
case VARIANCE_NONE:
return shortClassName;
case VARIANCE_EXTENDS:
@@ -78,22 +73,22 @@ public class ClsTypeElementImpl extends ClsElementImpl implements PsiTypeElement
case VARIANCE_INVARIANT:
return "?";
default:
LOG.assertTrue(false);
assert false : myVariance;
return null;
}
}
public String getCanonicalText(){
public String getCanonicalText() {
return decorateTypeText(myTypeText);
}
@Override
public void appendMirrorText(final int indentLevel, final StringBuilder buffer){
public void appendMirrorText(int indentLevel, @NotNull StringBuilder buffer) {
buffer.append(decorateTypeText(myTypeText));
}
@Override
public void setMirror(@NotNull TreeElement element){
public void setMirror(@NotNull TreeElement element) throws InvalidMirrorException {
setMirrorCheckingType(element, JavaElementType.TYPE);
loadChild();
@@ -172,7 +167,7 @@ public class ClsTypeElementImpl extends ClsElementImpl implements PsiTypeElement
case VARIANCE_SUPER:
return PsiWildcardType.createSuper(getManager(), ((PsiTypeElement)myChild).getType());
default:
LOG.assertTrue(false);
assert false : myVariance;
return null;
}
}
@@ -200,7 +195,7 @@ public class ClsTypeElementImpl extends ClsElementImpl implements PsiTypeElement
case VARIANCE_INVARIANT:
return PsiWildcardType.createUnbounded(getManager());
default:
LOG.assertTrue(false);
assert false : myVariance;
return null;
}
}
@@ -231,7 +226,7 @@ public class ClsTypeElementImpl extends ClsElementImpl implements PsiTypeElement
myChild = new ClsTypeElementImpl(this, myTypeText.substring(0, myTypeText.length() - 3), myVariance);
}
else {
LOG.assertTrue(false);
assert false : myTypeText;
}
myChildSet = true;
}
@@ -239,7 +234,7 @@ public class ClsTypeElementImpl extends ClsElementImpl implements PsiTypeElement
}
@Override
public void accept(@NotNull PsiElementVisitor visitor){
public void accept(@NotNull PsiElementVisitor visitor) {
if (visitor instanceof JavaElementVisitor) {
((JavaElementVisitor)visitor).visitTypeElement(this);
}
@@ -248,10 +243,6 @@ public class ClsTypeElementImpl extends ClsElementImpl implements PsiTypeElement
}
}
public String toString() {
return "PsiTypeElement:" + getText();
}
@Override
@NotNull
public PsiAnnotation[] getAnnotations() {
@@ -274,4 +265,9 @@ public class ClsTypeElementImpl extends ClsElementImpl implements PsiTypeElement
public PsiAnnotation[] getApplicableAnnotations() {
return getAnnotations();
}
@Override
public String toString() {
return "PsiTypeElement:" + getText();
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -15,7 +15,6 @@
*/
package com.intellij.psi.impl.compiled;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.util.Pair;
import com.intellij.psi.*;
import com.intellij.psi.impl.InheritanceImplUtil;
@@ -43,11 +42,9 @@ import java.util.List;
* @author max
*/
public class ClsTypeParameterImpl extends ClsRepositoryPsiElement<PsiTypeParameterStub> implements PsiTypeParameter {
private static final Logger LOG = Logger.getInstance("#com.intellij.psi.impl.compiled.ClsTypeParameterImpl");
static final ClsTypeParameterImpl[] EMPTY_ARRAY = new ClsTypeParameterImpl[0];
private final LightEmptyImplementsList myLightEmptyImplementsList;
public ClsTypeParameterImpl(final PsiTypeParameterStub stub) {
public ClsTypeParameterImpl(@NotNull PsiTypeParameterStub stub) {
super(stub);
myLightEmptyImplementsList = new LightEmptyImplementsList(getManager());
}
@@ -313,25 +310,23 @@ public class ClsTypeParameterImpl extends ClsRepositoryPsiElement<PsiTypeParamet
}
@Override
public void appendMirrorText(final int indentLevel, @NonNls final StringBuilder buffer) {
public void appendMirrorText(int indentLevel, @NotNull StringBuilder buffer) {
buffer.append(getName());
PsiJavaCodeReferenceElement[] bounds = getExtendsList().getReferenceElements();
if (bounds.length > 0) {
buffer.append(" extends ");
for (int i = 0; i < bounds.length; i++) {
PsiJavaCodeReferenceElement bound = bounds[i];
if (i > 0) buffer.append(" & ");
buffer.append(bound.getCanonicalText());
buffer.append(bounds[i].getCanonicalText());
}
}
}
@Override
public void setMirror(@NotNull TreeElement element) {
public void setMirror(@NotNull TreeElement element) throws InvalidMirrorException {
setMirrorCheckingType(element, null);
PsiTypeParameter mirror = (PsiTypeParameter)SourceTreeToPsiMap.treeElementToPsi(element);
((ClsReferenceListImpl)getExtendsList()).setMirror((TreeElement)SourceTreeToPsiMap.psiElementToTree(mirror.getExtendsList()));
setMirror(getExtendsList(), SourceTreeToPsiMap.<PsiTypeParameter>treeToPsiNotNull(element).getExtendsList());
}
@Override
@@ -345,11 +340,10 @@ public class ClsTypeParameterImpl extends ClsRepositoryPsiElement<PsiTypeParamet
return (PsiTypeParameterListOwner)getParent().getParent();
}
@Override
public int getIndex() {
final PsiTypeParameterStub st = getStub();
return st.getParentStub().getChildrenStubs().indexOf(st);
final PsiTypeParameterStub stub = getStub();
return stub.getParentStub().getChildrenStubs().indexOf(stub);
}
public PsiMetaData getMetaData() {
@@ -389,6 +383,7 @@ public class ClsTypeParameterImpl extends ClsRepositoryPsiElement<PsiTypeParamet
public PsiAnnotation addAnnotation(@NotNull @NonNls String qualifiedName) {
throw new IncorrectOperationException();
}
@Override
@NotNull
public PsiAnnotation[] getApplicableAnnotations() {
@@ -1,216 +0,0 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
*
* 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 com.intellij.psi.impl.compiled;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.*;
import com.intellij.psi.impl.source.tree.ElementType;
import com.intellij.psi.impl.source.tree.TreeElement;
import com.intellij.psi.infos.CandidateInfo;
import com.intellij.psi.scope.PsiScopeProcessor;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
/**
* @author max
*/
public class ClsTypeParameterReferenceImpl extends ClsElementImpl implements PsiJavaCodeReferenceElement {
private static final Logger LOG = Logger.getInstance("#com.intellij.psi.impl.compiled.ClsTypeParameterReferenceImpl");
private final PsiElement myParent;
private final String myName;
public ClsTypeParameterReferenceImpl(PsiElement parent, String name) {
myParent = parent;
myName = name;
}
@Override
public void processVariants(PsiScopeProcessor processor) {
throw new RuntimeException("Variants are not available for light references");
}
@Override
public PsiElement getReferenceNameElement() {
return null;
}
@Override
public PsiReferenceParameterList getParameterList() {
return null;
}
@Override
public String getQualifiedName() {
return myName;
}
@Override
public String getReferenceName() {
return myName;
}
@Override
public PsiElement resolve() {
LOG.assertTrue(myParent.isValid());
PsiElement parent = myParent;
while (!(parent instanceof PsiFile)) {
PsiTypeParameterList parameterList = null;
if (parent instanceof PsiClass) {
parameterList = ((PsiClass) parent).getTypeParameterList();
}
else if (parent instanceof PsiMethod) {
parameterList = ((PsiMethod) parent).getTypeParameterList();
}
if (parameterList != null) {
PsiTypeParameter[] parameters = parameterList.getTypeParameters();
for (PsiTypeParameter parameter : parameters) {
if (myName.equals(parameter.getName())) return parameter;
}
}
parent = parent.getParent();
}
return null;
}
@Override
@NotNull
public JavaResolveResult advancedResolve(boolean incompleteCode){
return new CandidateInfo(resolve(), PsiSubstitutor.EMPTY);
}
@Override
@NotNull
public JavaResolveResult[] multiResolve(boolean incompleteCode){
final JavaResolveResult result = advancedResolve(incompleteCode);
if(result != JavaResolveResult.EMPTY) return new JavaResolveResult[]{result};
return JavaResolveResult.EMPTY_ARRAY;
}
@Override
@NotNull
public PsiType[] getTypeParameters() {
return PsiType.EMPTY_ARRAY;
}
@Override
public PsiElement getQualifier() {
return null;
}
@Override
public boolean isQualified() {
return false;
}
@Override
@NotNull
public String getCanonicalText() {
return myName;
}
@Override
public boolean isReferenceTo(PsiElement element) {
if (!(element instanceof ClsTypeParameterImpl)) return false;
return element == resolve();
}
@Override
public String getText() {
return myName;
}
@Override
public int getTextLength() {
return getText().length();
}
@Override
public PsiReference getReference() {
return this;
}
@Override
@NotNull
public PsiElement[] getChildren(){
return PsiElement.EMPTY_ARRAY;
}
@Override
public PsiElement getParent(){
return myParent;
}
@Override
public PsiElement handleElementRename(String newElementName) throws IncorrectOperationException {
throw new IncorrectOperationException(CAN_NOT_MODIFY_MESSAGE);
}
@Override
public PsiElement bindToElement(@NotNull PsiElement element) throws IncorrectOperationException {
throw new IncorrectOperationException(CAN_NOT_MODIFY_MESSAGE);
}
@Override
@NotNull
public Object[] getVariants() {
throw new RuntimeException("Variants are not available for references to compiled code");
}
@Override
public boolean isSoft(){
return false;
}
@Override
public void appendMirrorText(final int indentLevel, final StringBuilder buffer){
buffer.append(getCanonicalText());
}
@Override
public void setMirror(@NotNull TreeElement element){
setMirrorCheckingType(element, ElementType.JAVA_CODE_REFERENCE);
}
@Override
public void accept(@NotNull PsiElementVisitor visitor){
if (visitor instanceof JavaElementVisitor) {
((JavaElementVisitor)visitor).visitReferenceElement(this);
}
else {
visitor.visitElement(this);
}
}
public String toString() {
return "PsiJavaCodeReferenceElement:" + getText();
}
@Override
public TextRange getRangeInElement() {
final PsiElement mirror = getMirror();
return mirror != null ? mirror.getTextRange() : new TextRange(0, getTextLength());
}
@Override
public PsiElement getElement() {
return this;
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -15,7 +15,6 @@
*/
package com.intellij.psi.impl.compiled;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.psi.*;
import com.intellij.psi.impl.PsiImplUtil;
import com.intellij.psi.impl.java.stubs.JavaStubElementTypes;
@@ -29,41 +28,29 @@ import org.jetbrains.annotations.NotNull;
* @author max
*/
public class ClsTypeParametersListImpl extends ClsRepositoryPsiElement<PsiTypeParameterListStub> implements PsiTypeParameterList {
private static final Logger LOG = Logger.getInstance("#com.intellij.psi.impl.compiled.ClsTypeParametersListImpl");
public ClsTypeParametersListImpl(final PsiTypeParameterListStub stub) {
public ClsTypeParametersListImpl(@NotNull PsiTypeParameterListStub stub) {
super(stub);
}
@Override
public void appendMirrorText(final int indentLevel, final StringBuilder buffer) {
public void appendMirrorText(int indentLevel, @NotNull StringBuilder buffer) {
final PsiTypeParameter[] params = getTypeParameters();
if (params.length != 0) {
buffer.append('<');
for (int i = 0; i < params.length; i++) {
ClsTypeParameterImpl parameter = (ClsTypeParameterImpl)params[i];
if (i > 0) buffer.append(", ");
parameter.appendMirrorText(indentLevel, buffer);
appendText(params[i], indentLevel, buffer);
}
buffer.append("> ");
}
}
@Override
public void setMirror(@NotNull TreeElement element) {
public void setMirror(@NotNull TreeElement element) throws InvalidMirrorException {
setMirrorCheckingType(element, null);
PsiTypeParameter[] parms = getTypeParameters();
PsiTypeParameter[] parmMirrors = ((PsiTypeParameterList)SourceTreeToPsiMap.treeElementToPsi(element)).getTypeParameters();
LOG.assertTrue(parms.length == parmMirrors.length);
if (parms.length == parmMirrors.length) {
for (int i = 0; i < parms.length; i++) {
((ClsElementImpl)parms[i]).setMirror((TreeElement)SourceTreeToPsiMap.psiElementToTree(parmMirrors[i]));
}
}
setMirrors(getTypeParameters(), SourceTreeToPsiMap.<PsiTypeParameterList>treeToPsiNotNull(element).getTypeParameters());
}
@Override
public void accept(@NotNull PsiElementVisitor visitor) {
if (visitor instanceof JavaElementVisitor) {
@@ -81,14 +68,10 @@ public class ClsTypeParametersListImpl extends ClsRepositoryPsiElement<PsiTypePa
@Override
public int getTypeParameterIndex(PsiTypeParameter typeParameter) {
LOG.assertTrue(typeParameter.getParent() == this);
assert typeParameter.getParent() == this;
return PsiImplUtil.getTypeParameterIndex(typeParameter, this);
}
public String toString() {
return "PsiTypeParameterList";
}
@Override
public boolean processDeclarations(@NotNull PsiScopeProcessor processor,
@NotNull ResolveState state,
@@ -100,4 +83,9 @@ public class ClsTypeParametersListImpl extends ClsRepositoryPsiElement<PsiTypePa
}
return true;
}
@Override
public String toString() {
return "PsiTypeParameterList";
}
}
@@ -13,10 +13,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* @author max
*/
package com.intellij.psi.impl.compiled;
import com.intellij.openapi.util.text.StringUtil;
@@ -31,6 +27,7 @@ import com.intellij.psi.impl.java.stubs.impl.*;
import com.intellij.psi.stubs.PsiFileStub;
import com.intellij.psi.stubs.StubElement;
import com.intellij.util.ArrayUtil;
import com.intellij.util.StringBuilderSpinAllocator;
import com.intellij.util.cls.ClsFormatException;
import com.intellij.util.io.StringRef;
import org.jetbrains.annotations.NonNls;
@@ -38,6 +35,7 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.asm4.*;
import java.lang.reflect.Array;
import java.text.CharacterIterator;
import java.text.StringCharacterIterator;
import java.util.ArrayList;
@@ -45,6 +43,9 @@ import java.util.Collections;
import java.util.List;
import java.util.regex.Pattern;
/**
* @author max
*/
public class StubBuildingVisitor<T> extends ClassVisitor {
private static final Pattern REGEX_PATTERN = Pattern.compile("(?<=[^\\$\\.])\\${1}(?=[^\\$])"); // disallow .$ or $$
@@ -325,7 +326,7 @@ public class StubBuildingVisitor<T> extends ClassVisitor {
if (!isCorrectName(name)) return null;
final byte flags = PsiFieldStubImpl.packFlags((access & Opcodes.ACC_ENUM) != 0, (access & Opcodes.ACC_DEPRECATED) != 0, false);
PsiFieldStub stub = new PsiFieldStubImpl(myResult, name, fieldType(desc, signature), constToString(value), flags);
final PsiFieldStub stub = new PsiFieldStubImpl(myResult, name, fieldType(desc, signature), constToString(value), flags);
final PsiModifierListStub modList = new PsiModifierListStubImpl(stub, packFieldFlags(access));
return new AnnotationCollectingVisitor(modList);
}
@@ -677,9 +678,8 @@ public class StubBuildingVisitor<T> extends ClassVisitor {
}
}
@SuppressWarnings({"HardCodedStringLiteral"})
@Nullable
private static String constToString(final Object value) {
private static String constToString(@Nullable Object value) {
if (value == null) return null;
if (value instanceof String) return "\"" + StringUtil.escapeStringCharacters((String)value) + "\"";
@@ -711,6 +711,22 @@ public class StubBuildingVisitor<T> extends ClassVisitor {
}
}
if (value.getClass().isArray()) {
StringBuilder buffer = StringBuilderSpinAllocator.alloc();
try {
buffer.append('{');
for (int i = 0, length = Array.getLength(value); i < length; i++) {
if (i > 0) buffer.append(", ");
buffer.append(Array.get(value, i));
}
buffer.append('}');
return buffer.toString();
}
finally {
StringBuilderSpinAllocator.dispose(buffer);
}
}
return null;
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -13,17 +13,16 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* @author max
*/
package com.intellij.psi.impl.java.stubs;
import com.intellij.psi.PsiAnnotation;
import com.intellij.psi.impl.source.tree.CompositeElement;
import com.intellij.psi.stubs.StubElement;
/**
* @author max
*/
public interface PsiAnnotationStub extends StubElement<PsiAnnotation> {
String getText();
CompositeElement getTreeElement();
PsiAnnotation getPsiElement();
}
@@ -19,9 +19,9 @@ import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.util.Pair;
import com.intellij.psi.JavaPsiFacade;
import com.intellij.psi.PsiAnnotation;
import com.intellij.psi.PsiJavaParserFacade;
import com.intellij.psi.impl.java.stubs.JavaStubElementTypes;
import com.intellij.psi.impl.java.stubs.PsiAnnotationStub;
import com.intellij.psi.impl.source.tree.CompositeElement;
import com.intellij.psi.stubs.StubBase;
import com.intellij.psi.stubs.StubElement;
import com.intellij.util.IncorrectOperationException;
@@ -38,7 +38,7 @@ public class PsiAnnotationStubImpl extends StubBase<PsiAnnotation> implements Ps
private static final Logger LOG = Logger.getInstance("#com.intellij.psi.impl.java.stubs.impl.PsiAnnotationStubImpl");
private final String myText;
private PatchedSoftReference<CompositeElement> myParsedFromRepository;
private PatchedSoftReference<PsiAnnotation> myParsedFromRepository;
public PsiAnnotationStubImpl(final StubElement parent, final String text) {
this(parent, text, null);
@@ -61,18 +61,21 @@ public class PsiAnnotationStubImpl extends StubBase<PsiAnnotation> implements Ps
}
@Override
public CompositeElement getTreeElement() {
public PsiAnnotation getPsiElement() {
if (myParsedFromRepository != null) {
CompositeElement parsed = myParsedFromRepository.get();
if (parsed != null) return parsed;
PsiAnnotation annotation = myParsedFromRepository.get();
if (annotation != null) {
return annotation;
}
}
final String text = getText();
try {
CompositeElement parsed = (CompositeElement)JavaPsiFacade.getInstance(getProject()).getParserFacade().createAnnotationFromText(text, getPsi()).getNode();
myParsedFromRepository = new PatchedSoftReference<CompositeElement>(parsed);
assert parsed != null;
return parsed;
PsiJavaParserFacade facade = JavaPsiFacade.getInstance(getProject()).getParserFacade();
PsiAnnotation annotation = facade.createAnnotationFromText(text, getPsi());
myParsedFromRepository = new PatchedSoftReference<PsiAnnotation>(annotation);
assert annotation != null : text;
return annotation;
}
catch (IncorrectOperationException e) {
LOG.error("Bad annotation in repository!", e);
@@ -80,6 +83,7 @@ public class PsiAnnotationStubImpl extends StubBase<PsiAnnotation> implements Ps
}
}
@Override
@SuppressWarnings({"HardCodedStringLiteral"})
public String toString() {
return "PsiAnnotationStub[" + myText + "]";
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -13,7 +13,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.psi.impl.java.stubs.impl;
import com.intellij.pom.java.LanguageLevel;
@@ -23,8 +22,9 @@ import com.intellij.psi.impl.java.stubs.PsiClassStub;
import com.intellij.psi.stubs.StubBase;
import com.intellij.psi.stubs.StubElement;
import com.intellij.util.io.StringRef;
import org.jetbrains.annotations.Nullable;
/*
/**
* @author max
*/
public class PsiClassStubImpl<T extends PsiClass> extends StubBase<T> implements PsiClassStub<T> {
@@ -49,7 +49,7 @@ public class PsiClassStubImpl<T extends PsiClass> extends StubBase<T> implements
final StubElement parent,
final String qualifiedName,
final String name,
final String baseRefText,
@Nullable final String baseRefText,
final byte flags) {
this(type, parent, StringRef.fromString(qualifiedName), StringRef.fromString(name), StringRef.fromString(baseRefText), flags);
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -13,10 +13,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* @author max
*/
package com.intellij.psi.impl.java.stubs.impl;
import com.intellij.psi.PsiField;
@@ -30,7 +26,11 @@ import com.intellij.psi.stubs.StubBase;
import com.intellij.psi.stubs.StubElement;
import com.intellij.util.io.StringRef;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* @author max
*/
public class PsiFieldStubImpl extends StubBase<PsiField> implements PsiFieldStub {
private final StringRef myName;
private final TypeInfo myType;
@@ -41,7 +41,7 @@ public class PsiFieldStubImpl extends StubBase<PsiField> implements PsiFieldStub
private static final int DEPRECATED = 0x02;
private static final int DEPRECATED_ANNOTATION = 0x04;
public PsiFieldStubImpl(final StubElement parent, final String name, @NotNull TypeInfo type, final String initializer, final byte flags) {
public PsiFieldStubImpl(final StubElement parent, final String name, @NotNull TypeInfo type, @Nullable String initializer, final byte flags) {
this(parent, StringRef.fromString(name), type, StringRef.fromString(initializer), flags);
}
@@ -69,7 +69,7 @@ public class PsiFieldStubImpl extends StubBase<PsiField> implements PsiFieldStub
for (StubElement child: modifierList.getChildrenStubs()){
if (!(child instanceof PsiAnnotationStub)) continue;
PsiAnnotationStub annotationStub = (PsiAnnotationStub)child;
PsiAnnotationImpl annotation = (PsiAnnotationImpl)annotationStub.getTreeElement().getPsi();
PsiAnnotationImpl annotation = (PsiAnnotationImpl)annotationStub.getPsiElement();
if (PsiAnnotationImpl.isAnnotationApplicableTo(annotation, true, "TYPE_USE")) {
typeInfo.addAnnotation(annotationStub);
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -13,10 +13,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* @author max
*/
package com.intellij.psi.impl.java.stubs.impl;
import com.intellij.psi.PsiParameter;
@@ -31,6 +27,9 @@ import org.jetbrains.annotations.NotNull;
import java.util.List;
/**
* @author max
*/
public class PsiParameterStubImpl extends StubBase<PsiParameter> implements PsiParameterStub {
private StringRef myName;
private final TypeInfo myType;
@@ -74,16 +73,6 @@ public class PsiParameterStubImpl extends StubBase<PsiParameter> implements PsiP
return StringRef.toString(myName);
}
@SuppressWarnings({"HardCodedStringLiteral"})
public String toString() {
StringBuilder builder = new StringBuilder();
builder.
append("PsiParameterStub[").
append(myName).append(':').append(TypeInfo.createTypeText(getType(false))).
append(']');
return builder.toString();
}
public void setName(String name) {
myName = StringRef.fromString(name);
}
@@ -99,4 +88,14 @@ public class PsiParameterStubImpl extends StubBase<PsiParameter> implements PsiP
}
return ("p" + paramIndex).equals(getName());
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.
append("PsiParameterStub[").
append(myName).append(':').append(TypeInfo.createTypeText(getType(false))).
append(']');
return builder.toString();
}
}
@@ -24,7 +24,6 @@ import com.intellij.psi.impl.java.stubs.JavaStubElementTypes;
import com.intellij.psi.impl.java.stubs.PsiAnnotationStub;
import com.intellij.psi.impl.meta.MetaRegistry;
import com.intellij.psi.impl.source.JavaStubPsiElement;
import com.intellij.psi.impl.source.tree.ChildRole;
import com.intellij.psi.meta.PsiMetaData;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.util.PsiTreeUtil;
@@ -58,7 +57,7 @@ public class PsiAnnotationImpl extends JavaStubPsiElement<PsiAnnotationStub> imp
public PsiJavaCodeReferenceElement getNameReferenceElement() {
final PsiAnnotationStub stub = getStub();
if (stub != null) {
return (PsiJavaCodeReferenceElement)stub.getTreeElement().findChildByRoleAsPsiElement(ChildRole.CLASS_REFERENCE);
return PsiTreeUtil.getRequiredChildOfType(stub.getPsiElement(), PsiJavaCodeReferenceElement.class);
}
final Object result = PsiTreeUtil.getChildOfType(this, PsiJavaCodeReferenceElement.class);
@@ -4,9 +4,8 @@
package pkg;
class $BuckClass {
class $BuckClass {
$BuckClass() { /* compiled code */ }
static pkg.$BuckClass getInstance() { /* compiled code */ }
}
@@ -0,0 +1,58 @@
// IntelliJ API Decompiler stub source generated from a class file
// Implementation of methods is not available
package pkg;
abstract class Annotations {
Annotations() { /* compiled code */ }
@pkg.Annotations.A1
abstract void m1();
@pkg.Annotations.A2
abstract void m2a();
@pkg.Annotations.A2("+")
abstract void m2b();
@pkg.Annotations.A3(a1 = @pkg.Annotations.A1, a2 = @pkg.Annotations.A2)
abstract void m3();
@pkg.Annotations.A4
abstract void m4a();
@pkg.Annotations.A4(ids = {42, 84})
abstract void m4b();
static @interface IndeterminateAnno {
float f1() default -1.0f / 0.0;
float f2() default 0.0f / 0.0;
float f3() default 1.0f / 0.0;
double d1() default -1.0 / 0.0;
double d2() default 0.0d / 0.0;
double d3() default 1.0 / 0.0;
}
static @interface A4 {
int[] ids() default {};
}
static @interface A3 {
pkg.Annotations.A1 a1();
pkg.Annotations.A2 a2();
}
static @interface A2 {
java.lang.String value() default "-";
}
static @interface A1 {
}
}
@@ -0,0 +1,22 @@
// IntelliJ API Decompiler stub source generated from a class file
// Implementation of methods is not available
package pkg;
/**
* @deprecated
*/
class Deprecated {
/**
* @deprecated
*/
int f;
Deprecated() { /* compiled code */ }
/**
* @deprecated
*/
void m() { /* compiled code */ }
}
@@ -6,9 +6,10 @@ package pkg;
final enum EmptyEnum {
;
public static pkg.EmptyEnum[] values() { /* compiled code */ }
public static pkg.EmptyEnum valueOf(java.lang.String s) { /* compiled code */ }
private EmptyEnum() { /* compiled code */ }
}
}
@@ -0,0 +1,18 @@
// IntelliJ API Decompiler stub source generated from a class file
// Implementation of methods is not available
package pkg;
public final enum EnumWithFields {
public static final E1, public static final E2;
boolean f1;
boolean f2;
public static pkg.EnumWithFields[] values() { /* compiled code */ }
public static pkg.EnumWithFields valueOf(java.lang.String s) { /* compiled code */ }
private EnumWithFields() { /* compiled code */ }
}
@@ -4,9 +4,8 @@
package pkg;
interface ExtMethods {
interface ExtMethods {
void m1();
void m2() { /* compiled code */ }
}
@@ -4,12 +4,11 @@
package pkg;
class MethodReceiver {
class MethodReceiver {
MethodReceiver() { /* compiled code */ }
void m(int i) { /* compiled code */ }
static @interface A {
}
}
}
@@ -0,0 +1,17 @@
// IntelliJ API Decompiler stub source generated from a class file
// Implementation of methods is not available
package pkg;
class NormalClass {
private final java.lang.Object o;
NormalClass() { /* compiled code */ }
java.lang.Object get() { /* compiled code */ }
private static class Inner {
private Inner() { /* compiled code */ }
}
}
@@ -0,0 +1,13 @@
// IntelliJ API Decompiler stub source generated from a class file
// Implementation of methods is not available
package pkg;
public class ParameterNames {
public ParameterNames() { /* compiled code */ }
public void test(int i, int i1, java.lang.String s) { /* compiled code */ }
private void testImpl(int i, int i1, java.lang.String s, boolean b) { /* compiled code */ }
}
@@ -0,0 +1,15 @@
// IntelliJ API Decompiler stub source generated from a class file
// Implementation of methods is not available
package pkg;
public final enum SimpleEnum {
public static final E1, public static final E2;
public static pkg.SimpleEnum[] values() { /* compiled code */ }
public static pkg.SimpleEnum valueOf(java.lang.String s) { /* compiled code */ }
private SimpleEnum() { /* compiled code */ }
}
@@ -5,5 +5,5 @@
package pkg;
@pkg.PkgAnno
interface $$package-info /* Real name is 'package-info' */ {
interface $$package-info /* Real name is 'package-info' */ {
}
@@ -0,0 +1,37 @@
package pkg;
abstract class Annotations {
@interface A1 { }
@interface A2 {
String value() default "-";
}
@interface A3 {
A1 a1();
A2 a2();
}
@interface A4 {
int[] ids() default { };
}
@A1 abstract void m1();
@A2() abstract void m2a();
@A2("+") abstract void m2b();
@A3(a1 = @A1, a2 = @A2) abstract void m3();
@A4 abstract void m4a();
@A4(ids = {42, 84}) abstract void m4b();
@interface IndeterminateAnno {
float f1() default Float.NEGATIVE_INFINITY;
float f2() default Float.NaN;
float f3() default Float.POSITIVE_INFINITY;
double d1() default Double.NEGATIVE_INFINITY;
double d2() default Double.NaN;
double d3() default Double.POSITIVE_INFINITY;
}
}
@@ -0,0 +1,10 @@
package pkg;
/** @deprecated don't use */
class Deprecated {
/** @deprecated don't use */
int f;
/** @deprecated don't use */
void m() { }
}
@@ -0,0 +1,8 @@
package pkg;
public enum EnumWithFields {
E1, E2;
boolean f1;
boolean f2;
}
@@ -0,0 +1,11 @@
package pkg;
class NormalClass {
private final Object o = new Inner();
Object get() {
return o;
}
private static class Inner { }
}
@@ -0,0 +1,13 @@
package pkg;
public class ParameterNames {
public void test(int index, int id, String name) {
testImpl(index, id, name, true);
}
private void testImpl(int index, int id, String name, boolean print) {
if (print) {
System.out.println("[" + index + "] " + id + ":" + name);
}
}
}
@@ -0,0 +1,5 @@
package pkg;
public enum SimpleEnum {
E1, E2
}
@@ -19,12 +19,19 @@ import com.intellij.JavaTestUtil;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.impl.compiled.ClsFileImpl;
import com.intellij.testFramework.LightIdeaTestCase;
import com.intellij.testFramework.PlatformTestUtil;
import java.io.IOException;
public class ClsMirrorBuildingTest extends LightIdeaTestCase {
public void testSimpleEnum() { doTest(); }
public void testEnumWithFields() { doTest(); }
public void testNormalClass() { doTest(); }
public void testDeprecated() { doTest(); }
public void testAnnotations() { doTest(); }
public void testParameterNames() { doTest(); }
public void testEmptyEnum() { doTest(); }
public void test$BuckClass() { doTest(); }
public void testExtMethods() { doTest(); }
@@ -53,14 +60,6 @@ public class ClsMirrorBuildingTest extends LightIdeaTestCase {
fail(e.getMessage());
return;
}
assertEquals(expected, clsFile.getText());
/*String srcPath = testDir + "src/pkg/" + name + ".java";
vFile = LocalFileSystem.getInstance().findFileByPath(srcPath);
assertNotNull(srcPath, vFile);
PsiFile srcFile = getPsiManager().findFile(vFile);
assertNotNull(vFile.getPath(), srcFile);
((ClsFileImpl)clsFile).setMirror((TreeElement)srcFile.getNode());*/
assertEquals(expected, ((ClsFileImpl)clsFile).getMirror().getText());
}
}
@@ -19,8 +19,6 @@ import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.ex.PathManagerEx;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.projectRoots.impl.JavaSdkImpl;
import com.intellij.openapi.roots.ModifiableRootModel;
import com.intellij.openapi.roots.ModuleRootManager;
import com.intellij.openapi.roots.ModuleRootModificationUtil;
import com.intellij.openapi.roots.OrderRootType;
import com.intellij.openapi.util.io.FileUtil;
@@ -766,14 +764,14 @@ public class ClsRepositoryUseTest extends PsiTestCase {
public void testModifiers() throws Exception {
final PsiClass psiClass = myJavaFacade.findClass("pack.Modifiers", RESOLVE_SCOPE);
assertNotNull(psiClass);
assertEquals("public class Modifiers {\n" +
assertEquals("public class Modifiers {\n" +
" private transient int f1;\n" +
" private volatile int f2;\n" +
" \n" +
"\n" +
" public Modifiers() { /* compiled code */ }\n" +
" \n" +
"\n" +
" private void m1(int... i) { /* compiled code */ }\n" +
" \n" +
"\n" +
" private synchronized void m2() { /* compiled code */ }\n" +
"}",
psiClass.getText().trim());