Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@
import com.intellij.aop.psi.AopPointcutExpressionFile;
import com.intellij.java.language.psi.PsiClass;
import com.intellij.java.language.psi.PsiLiteralExpression;
import com.intellij.java.language.psi.PsiMethod;
import com.intellij.java.language.psi.PsiModifier;
import com.intellij.java.language.psi.PsiMember;
import consulo.annotation.access.RequiredReadAction;
import consulo.annotation.component.ExtensionImpl;
import consulo.aop.localize.AopLocalize;
import consulo.document.util.TextRange;
import consulo.language.editor.inspection.LocalQuickFix;
import consulo.language.editor.inspection.ProblemDescriptor;
import consulo.language.editor.inspection.ProblemHighlightType;
import consulo.language.editor.inspection.ProblemsHolder;
import consulo.language.editor.rawHighlight.HighlightDisplayLevel;
import consulo.language.inject.InjectedLanguageManager;
Expand All @@ -27,13 +26,11 @@
import consulo.logging.Logger;
import consulo.project.Project;
import consulo.util.collection.ContainerUtil;
import consulo.util.lang.function.Condition;
import consulo.virtualFileSystem.ReadonlyStatusHandler;
import consulo.xml.editor.XmlSuppressableInspectionTool;
import consulo.xml.language.psi.XmlAttributeValue;
import consulo.xml.language.psi.XmlElement;
import jakarta.annotation.Nonnull;
import org.jetbrains.annotations.NonNls;

/**
* @author peter
Expand All @@ -42,11 +39,13 @@
public class DeclareParentsInspection extends XmlSuppressableInspectionTool {
private static final Logger LOG = Logger.getInstance(DeclareParentsInspection.class);

@Override
public boolean isEnabledByDefault() {
return true;
}

@Nonnull
@Override
public HighlightDisplayLevel getDefaultLevel() {
return HighlightDisplayLevel.ERROR;
}
Expand All @@ -64,9 +63,11 @@ public String getShortName() {
}

@Nonnull
@Override
public PsiElementVisitor buildVisitor(@Nonnull final ProblemsHolder holder, boolean isOnTheFly) {
return new PsiElementVisitor() {
@Override
@RequiredReadAction
public void visitElement(PsiElement element) {
if (element instanceof PsiLiteralExpression || element instanceof XmlAttributeValue) {
PsiElement injectedElement =
Expand All @@ -76,9 +77,8 @@ public void visitElement(PsiElement element) {
);

PsiFile file = injectedElement == null ? null : injectedElement.getContainingFile();
if (file instanceof AopPointcutExpressionFile) {
final IntroductionManipulator manipulator =
((AopPointcutExpressionFile) file).getAopModel().getIntroductionManipulator();
if (file instanceof AopPointcutExpressionFile pointcutExpressionFile) {
final IntroductionManipulator manipulator = pointcutExpressionFile.getAopModel().getIntroductionManipulator();
if (manipulator == null) {
return;
}
Expand All @@ -90,7 +90,7 @@ public void visitElement(PsiElement element) {
PsiClass intf = introduction.getImplementInterface().getValue();
if (intf == null && introduction.getImplementInterface().getStringValue() != null
|| intf != null && !intf.isInterface()) {
registerProblem(manipulator.getInterfaceElement(), AopLocalize.errorInterfaceExpected().get(), holder);
registerProblem(manipulator.getInterfaceElement(), AopLocalize.errorInterfaceExpected(), holder);
return;
}
if (intf == null) {
Expand All @@ -99,14 +99,8 @@ public void visitElement(PsiElement element) {

PsiClass defaultImpl = introduction.getDefaultImpl().getValue();
if (defaultImpl == null) {
if (!(element instanceof XmlElement) && !ContainerUtil.findAll(
intf.getAllMethods(),
new Condition<PsiMethod>() {
public boolean value(PsiMethod method) {
return method.hasModifierProperty(PsiModifier.ABSTRACT);
}
}
).isEmpty()) {
if (!(element instanceof XmlElement)
&& !ContainerUtil.findAll(intf.getAllMethods(), PsiMember::isAbstract).isEmpty()) {
holder.newProblem(AopLocalize.errorDefaultImplementationClassShouldBeSpecified())
.range(manipulator.getCommonProblemElement())
.withFix(new LocalQuickFix() {
Expand All @@ -116,6 +110,7 @@ public LocalizeValue getName() {
return AopLocalize.quickfixNameDefineAttribute(manipulator.getDefaultImplAttributeName());
}

@Override
public void applyFix(@Nonnull Project project, @Nonnull ProblemDescriptor descriptor) {
try {
if (ReadonlyStatusHandler.getInstance(project)
Expand All @@ -137,12 +132,12 @@ public void applyFix(@Nonnull Project project, @Nonnull ProblemDescriptor descri
}
return;
}
if (defaultImpl.hasModifierProperty(PsiModifier.ABSTRACT) || !defaultImpl.isInheritor(intf, true)) {
if (defaultImpl.isAbstract() || !defaultImpl.isInheritor(intf, true)) {
PsiElement defaultImplElement = manipulator.getDefaultImplElement();
assert defaultImplElement != null;
registerProblem(
defaultImplElement,
AopLocalize.errorNonAbstractClassImplemention0Expected(intf.getQualifiedName()).get(),
AopLocalize.errorNonAbstractClassImplemention0Expected(intf.getQualifiedName()),
holder
);
}
Expand All @@ -152,16 +147,12 @@ public void applyFix(@Nonnull Project project, @Nonnull ProblemDescriptor descri
};
}

private static void registerProblem(PsiElement element, String descriptionTemplate, ProblemsHolder holder) {
@RequiredReadAction
private static void registerProblem(PsiElement element, LocalizeValue descriptionTemplate, ProblemsHolder holder) {
int startOffset = element.getTextRange().getStartOffset();
int quotes = element.getText().startsWith("\"") ? 1 : 0;
TextRange range = TextRange.from(quotes, Math.max(element.getTextLength() - 2 * quotes, 1));
holder.registerProblem(holder.getManager().createProblemDescriptor(
element,
range,
descriptionTemplate,
ProblemHighlightType.GENERIC_ERROR_OR_WARNING
));
holder.registerProblem(holder.getManager().newProblemDescriptor(descriptionTemplate).range(element, range).create());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The old implementation passed ProblemHighlightType.GENERIC_ERROR_OR_WARNING explicitly. newProblemDescriptor(...).range(...).create() relies on the builder's default highlight type. Please confirm that default is still GENERIC_ERROR_OR_WARNING, otherwise the severity/appearance of this reported problem changes silently.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@.claude-review-dp.md

}

@Nonnull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package com.intellij.aop.psi;

import com.intellij.java.language.psi.*;
import consulo.annotation.access.RequiredReadAction;
import consulo.language.ast.ASTNode;
import consulo.language.psi.PsiElement;

Expand All @@ -22,66 +23,74 @@ public AopReferenceHolder(@Nonnull ASTNode node) {
}

@Nullable
@RequiredReadAction
public AopTypeExpression getTypeExpression() {
return findChildByClass(AopTypeExpression.class);
}

@Override
public String toString() {
return "AopReferenceHolder";
}

@Override
@RequiredReadAction
public PointcutMatchDegree accepts(@Nonnull PsiType psiType) {
AopTypeExpression typeExpression = getTypeExpression();
return typeExpression != null ? AopPsiTypePattern.accepts(typeExpression, psiType) : PointcutMatchDegree.FALSE;
}

@Nullable
@Override
@RequiredReadAction
public String getTypePattern() {
AopTypeExpression expression = getTypeExpression();
if (expression == null) return null;

return expression.getTypePattern();
}

@RequiredReadAction
public final Collection<AopPsiTypePattern> getPatterns() {
AopTypeExpression expression = getTypeExpression();
return expression == null ? Collections.<AopPsiTypePattern>emptyList() : expression.getPatterns();
}

@Nullable
@Override
@RequiredReadAction
public PsiClass findClass() {
AopTypeExpression expression = getTypeExpression();
if (expression instanceof AopReferenceExpression) {
PsiElement psiElement = ((AopReferenceExpression)expression).resolve();
if (psiElement instanceof PsiClass) {
return (PsiClass)psiElement;
}
if (expression instanceof AopReferenceExpression refExpr && refExpr.resolve() instanceof PsiClass psiClass) {
return psiClass;
}
return null;
}

@Override
@RequiredReadAction
public boolean isAssignableFrom(PsiType type) {
AopTypeExpression expression = getTypeExpression();
return expression != null && isAssignable(expression, type);
}

@RequiredReadAction
private static boolean isAssignable(@Nonnull AopTypeExpression expression, PsiType type) {
if (type instanceof PsiArrayType) {
PsiArrayType arrayType = (PsiArrayType)type;
if (type instanceof PsiArrayType arrayType) {
if (expression instanceof AopArrayExpression) {
AopArrayExpression arrayExpression = (AopArrayExpression)expression;
return arrayExpression.isVarargs() == arrayType instanceof PsiEllipsisType && isAssignable(arrayExpression.getTypeReference(), arrayType.getComponentType());
}
return false;
}
PsiType exprType;
if (expression instanceof AopReferenceExpression) {
PsiElement superClass = ((AopReferenceExpression)expression).resolve();
if (expression instanceof AopReferenceExpression refExpr) {
PsiElement superClass = refExpr.resolve();
if (!(superClass instanceof PsiClass)) return false;

exprType = JavaPsiFacade.getInstance(expression.getProject()).getElementFactory().createType((PsiClass)superClass);
} else if (expression instanceof AopPrimitiveTypeExpression) {
exprType = ((AopPrimitiveTypeExpression) expression).getPsiType();
} else if (expression instanceof AopPrimitiveTypeExpression primitiveTypeExpr) {
exprType = primitiveTypeExpr.getPsiType();
} else {
return false;
}
Expand All @@ -92,19 +101,22 @@ private static boolean isAssignable(@Nonnull AopTypeExpression expression, PsiTy
}

@Nonnull
@Override
@RequiredReadAction
public String getQualifiedName() {
PsiClass psiClass = findClass();
if (psiClass != null) {
String qname = psiClass.getQualifiedName();
if (qname != null) {
return qname;
String qName = psiClass.getQualifiedName();
if (qName != null) {
return qName;
}
}
return getText().trim();
}

@Override
@RequiredReadAction
public PointcutMatchDegree canBeInstance(PsiClass psiClass, boolean allowPatterns) {
return PsiTargetExpression.canBeInstanceOf(psiClass, allowPatterns, getTypeExpression());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,21 @@ public AopPsiTypePattern getRight() {
return myRight;
}

@Override
public boolean accepts(@Nonnull PsiType type) {
if (type instanceof PsiClassType) {
PsiClassType classType = (PsiClassType)type;
if (type instanceof PsiClassType classType) {
PsiClass psiClass = classType.resolve();
if (psiClass != null) {
String qname = psiClass.getQualifiedName();
if (qname != null && accepts(qname)) {
String qName = psiClass.getQualifiedName();
if (qName != null && accepts(qName)) {
return true;
}
}
}
return false;
}

@Override
public boolean accepts(@Nonnull String qualifiedName) {
String[] strings = qualifiedName.split("\\.");
int[] indices = new int[strings.length];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package com.intellij.aop.psi;

import com.intellij.java.language.psi.PsiMember;
import consulo.annotation.access.RequiredReadAction;
import consulo.language.ast.ASTNode;

import jakarta.annotation.Nonnull;
Expand All @@ -26,21 +27,25 @@ public String toString() {
}

@Nullable
@RequiredReadAction
public AopModifierList getModifierList() {
return findChildByClass(AopModifierList.class);
}

@Nullable
@RequiredReadAction
public AopAnnotationHolder getAnnotationHolder() {
return findChildByClass(AopAnnotationHolder.class);
}

@Nonnull
@Override
public PointcutMatchDegree acceptsSubject(PointcutContext context, PsiMember member) {
return PointcutMatchDegree.FALSE;
}

@Nonnull
@Override
public Collection<AopPsiTypePattern> getPatterns() {
return Collections.emptyList();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,35 @@
*/
package com.intellij.aop.psi;

import com.intellij.aop.AopBundle;
import consulo.aop.localize.AopLocalize;
import consulo.language.pratt.MutableMarker;
import consulo.language.pratt.PrattBuilder;
import org.jetbrains.annotations.NonNls;

import static com.intellij.aop.psi.AopElementTypes.*;
import static com.intellij.aop.psi.AopPrattParser.TYPE_PATTERN;

/**
* @author peter
*/
public abstract class FieldPointcutDescriptor extends PointcutDescriptor{

protected FieldPointcutDescriptor(@NonNls String tokenText) {
super(tokenText);
*/
public abstract class FieldPointcutDescriptor extends PointcutDescriptor {
protected FieldPointcutDescriptor(String tokenText) {
super(tokenText);
}

@Override
public void parseToken(PrattBuilder builder) {
if (builder.assertToken(AOP_LEFT_PAR, AopLocalize.error0Expected("(").get())) {
MethodPointcutDescriptor.parseAnnotationsWithModifiers(builder);

MutableMarker type = builder.mark();
builder.parseChildren(TYPE_PATTERN, AopLocalize.errorTypeNamePatternExpected().get());
type.finish(AOP_REFERENCE_HOLDER);

MutableMarker fieldName = builder.mark();
builder.parseChildren(TYPE_PATTERN, AopLocalize.errorFieldNamePatternExpected().get());
fieldName.finish(AOP_MEMBER_REFERENCE_EXPRESSION);

builder.assertToken(AOP_RIGHT_PAR, AopLocalize.error0Expected(")").get());
}
}
}

public void parseToken(PrattBuilder builder) {
if (builder.assertToken(AOP_LEFT_PAR, AopBundle.message("error.0.expected", "("))) {
MethodPointcutDescriptor.parseAnnotationsWithModifiers(builder);

MutableMarker type = builder.mark();
builder.parseChildren(TYPE_PATTERN, AopBundle.message("error.type.name.pattern.expected"));
type.finish(AOP_REFERENCE_HOLDER);

MutableMarker fieldName = builder.mark();
builder.parseChildren(TYPE_PATTERN, AopBundle.message("error.field.name.pattern.expected"));
fieldName.finish(AOP_MEMBER_REFERENCE_EXPRESSION);

builder.assertToken(AOP_RIGHT_PAR, AopBundle.message("error.0.expected", ")"));
}

}
}
Loading
Loading