use of com.intellij.codeInspection.ProblemDescriptor in project qi4j-sdk by Qi4j.
the class AbstractInjectionAnnotationDeclarationOnFieldAndConstructorInspection method checkMethod.
@Override
public final ProblemDescriptor[] checkMethod(@NotNull PsiMethod method, @NotNull InspectionManager manager, boolean isOnTheFly) {
PsiParameterList parameterList = method.getParameterList();
PsiParameter[] parameters = parameterList.getParameters();
if (method.isConstructor()) {
List<ProblemDescriptor> problems = new LinkedList<ProblemDescriptor>();
for (PsiParameter parameter : parameters) {
PsiAnnotation annotation = getAnnotationToCheck(parameter);
if (annotation != null) {
ProblemDescriptor[] descriptors = verifyAnnotationDeclaredCorrectly(parameter, annotation, manager);
if (descriptors != null) {
problems.addAll(asList(descriptors));
}
}
}
return problems.toArray(new ProblemDescriptor[problems.size()]);
} else {
List<ProblemDescriptor> problems = new LinkedList<ProblemDescriptor>();
for (PsiParameter parameter : parameters) {
PsiAnnotation annotationToCheck = getAnnotationToCheck(parameter);
if (annotationToCheck != null) {
String message = getInjectionAnnotationValidDeclarationMessage();
AbstractFix removeAnnotationFix = createRemoveAnnotationFix(annotationToCheck);
ProblemDescriptor problemDescriptor = manager.createProblemDescriptor(annotationToCheck, message, removeAnnotationFix, GENERIC_ERROR_OR_WARNING);
problems.add(problemDescriptor);
}
}
return problems.toArray(new ProblemDescriptor[problems.size()]);
}
}
use of com.intellij.codeInspection.ProblemDescriptor in project qi4j-sdk by Qi4j.
the class MixinImplementsMixinType method checkClass.
@Override
public final ProblemDescriptor[] checkClass(@NotNull PsiClass psiClass, @NotNull InspectionManager manager, boolean isOnTheFly) {
// If psiClass is not an interface, ignore
if (!psiClass.isInterface()) {
return null;
}
// If @Mixins annotation is empty, ignore
List<PsiAnnotationMemberValue> mixinAnnotationValues = getMixinsAnnotationValue(psiClass);
if (mixinAnnotationValues.isEmpty()) {
return null;
}
// Get all valid mixin type
Set<PsiClass> validMixinsType = getAllValidMixinTypes(psiClass);
if (validMixinsType.isEmpty()) {
return null;
}
// For each mixin
List<ProblemDescriptor> problems = new LinkedList<ProblemDescriptor>();
for (PsiAnnotationMemberValue mixinAnnotationValue : mixinAnnotationValues) {
PsiJavaCodeReferenceElement mixinClassReference = getMixinClassReference(mixinAnnotationValue);
// If it's not a class reference, ignore
if (mixinClassReference == null) {
continue;
}
// If class reference can't be resolved, ignore
PsiClass mixinClass = (PsiClass) mixinClassReference.resolve();
if (mixinClass == null) {
continue;
}
String mixinQualifiedName = mixinClass.getQualifiedName();
boolean isMixinsDeclarationValid = false;
String message = "";
if (mixinClass.isInterface()) {
// Mixin can't be an interface
message = message("mixin.implements.mixin.type.error.mixin.is.an.interface", mixinQualifiedName);
} else if (isAConcern(mixinClass)) {
// Mixin can't be a concern
message = message("mixin.implements.mixin.type.error.mixin.is.a.concern", mixinQualifiedName);
} else if (isASideEffect(mixinClass)) {
// Mixin can't be a side effect
message = message("mixin.implements.mixin.type.error.mixin.is.a.side.effect", mixinQualifiedName);
} else {
// If doesn't implement any mixin type, it's a problem
if (!isImplementValidMixinType(mixinClass, validMixinsType)) {
message = message("mixin.implements.mixin.type.error.does.not.implement.any.mixin.type", mixinQualifiedName, psiClass.getQualifiedName());
} else {
isMixinsDeclarationValid = true;
}
}
if (!isMixinsDeclarationValid) {
ProblemDescriptor problemDescriptor = createProblemDescriptor(manager, mixinAnnotationValue, mixinClassReference, message);
problems.add(problemDescriptor);
}
}
return problems.toArray(new ProblemDescriptor[problems.size()]);
}
use of com.intellij.codeInspection.ProblemDescriptor in project google-cloud-intellij by GoogleCloudPlatform.
the class AppEngineForbiddenCodeInspection method checkFile.
@Override
public ProblemDescriptor[] checkFile(@NotNull PsiFile file, @NotNull final InspectionManager manager, final boolean isOnTheFly) {
final Project project = manager.getProject();
Module module = ModuleUtilCore.findModuleForPsiElement(file);
final AppEngineStandardFacet appEngineStandardFacet = AppEngineStandardFacet.getAppEngineFacetByModule(module);
if (appEngineStandardFacet == null) {
return null;
}
if (appEngineStandardFacet.getRuntimeJavaVersion().atLeast(JavaVersion.JAVA_1_8)) {
return null;
}
final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(project).getFileIndex();
final List<ProblemDescriptor> problems = new ArrayList<ProblemDescriptor>();
file.accept(new JavaRecursiveElementWalkingVisitor() {
CloudSdkInternals sdkInternals = CloudSdkInternals.getInstance();
@Override
public void visitDocComment(PsiDocComment comment) {
}
@Override
public void visitMethod(PsiMethod method) {
final PsiModifierList modifierList = method.getModifierList();
if (modifierList.hasModifierProperty(PsiModifier.NATIVE)) {
if (!isNativeMethodAllowed(method)) {
problems.add(manager.createProblemDescriptor(modifierList, "Native methods aren't allowed in App Engine application", isOnTheFly, LocalQuickFix.EMPTY_ARRAY, ProblemHighlightType.GENERIC_ERROR_OR_WARNING));
}
}
super.visitMethod(method);
}
@Override
public void visitNewExpression(PsiNewExpression expression) {
final PsiJavaCodeReferenceElement classReference = expression.getClassReference();
if (classReference != null) {
final PsiElement resolved = classReference.resolve();
if (resolved instanceof PsiClass) {
final String qualifiedName = ((PsiClass) resolved).getQualifiedName();
if (qualifiedName != null && sdkInternals.isMethodInBlacklist(qualifiedName, "new")) {
final String message = "App Engine application should not create new instances of '" + qualifiedName + "' class";
problems.add(manager.createProblemDescriptor(classReference, message, isOnTheFly, LocalQuickFix.EMPTY_ARRAY, ProblemHighlightType.GENERIC_ERROR_OR_WARNING));
}
}
}
super.visitNewExpression(expression);
}
@Override
public void visitMethodCallExpression(PsiMethodCallExpression expression) {
final PsiReferenceExpression methodExpression = expression.getMethodExpression();
final PsiElement element = methodExpression.resolve();
if (element instanceof PsiMethod) {
final PsiMethod method = (PsiMethod) element;
final PsiClass psiClass = method.getContainingClass();
if (psiClass != null) {
final String qualifiedName = psiClass.getQualifiedName();
final String methodName = method.getName();
if (qualifiedName != null && sdkInternals.isMethodInBlacklist(qualifiedName, methodName)) {
final String message = "AppEngine application should not call '" + StringUtil.getShortName(qualifiedName) + "" + methodName + "' method";
problems.add(manager.createProblemDescriptor(methodExpression, message, isOnTheFly, LocalQuickFix.EMPTY_ARRAY, ProblemHighlightType.GENERIC_ERROR_OR_WARNING));
}
}
}
super.visitMethodCallExpression(expression);
}
@Override
public void visitReferenceElement(PsiJavaCodeReferenceElement reference) {
final PsiElement resolved = reference.resolve();
if (resolved instanceof PsiClass) {
final PsiFile psiFile = resolved.getContainingFile();
if (psiFile != null) {
final VirtualFile virtualFile = psiFile.getVirtualFile();
if (virtualFile != null && !fileIndex.isInSource(virtualFile)) {
final List<OrderEntry> list = fileIndex.getOrderEntriesForFile(virtualFile);
for (OrderEntry entry : list) {
if (entry instanceof JdkOrderEntry) {
final String className = ClassUtil.getJVMClassName((PsiClass) resolved);
if (className != null && !sdkInternals.isClassInWhiteList(className)) {
problems.add(manager.createProblemDescriptor(reference, "Class '" + className + "' is not included in App Engine JRE White List", isOnTheFly, LocalQuickFix.EMPTY_ARRAY, ProblemHighlightType.GENERIC_ERROR_OR_WARNING));
}
}
}
}
}
}
super.visitReferenceElement(reference);
}
});
return problems.toArray(new ProblemDescriptor[problems.size()]);
}
use of com.intellij.codeInspection.ProblemDescriptor in project oxy-template-support-plugin by mutant-industries.
the class MissingIncludeInspection method checkFile.
@Nullable
public ProblemDescriptor[] checkFile(@NotNull final PsiFile file, @NotNull final InspectionManager manager, final boolean isOnTheFly) {
final List<ProblemDescriptor> result = new ArrayList<>();
for (Map.Entry<PsiElement, JSElement> pair : OxyTemplateHelper.getUsedJsMacros(file).entrySet()) {
PsiElement macroCall = pair.getKey();
PsiElement reference = pair.getValue();
if (OxyTemplateHelper.isJsMacroMissingInclude(file, reference)) {
result.add(manager.createProblemDescriptor(macroCall, TextRange.create(0, macroCall.getTextLength()), getDisplayName(), ProblemHighlightType.ERROR, isOnTheFly, new MissingIncludeDirectiveQuickFix(macroCall, reference, IncludeOnceDirective.NAME), new MissingIncludeDirectiveQuickFix(macroCall, reference, IncludeDirective.NAME)));
}
}
return result.isEmpty() ? super.checkFile(file, manager, isOnTheFly) : ArrayUtil.toObjectArray(result, ProblemDescriptor.class);
}
use of com.intellij.codeInspection.ProblemDescriptor in project Perl5-IDEA by Camelcade.
the class PerlSyntaxInspection method buildVisitor.
@Override
@NotNull
public PsiElementVisitor buildVisitor(@NotNull ProblemsHolder holder, boolean isOnTheFly) {
PerlVersion selectedVersion = PerlSharedSettings.getInstance(holder.getProject()).getTargetPerlVersion();
return new PerlVisitor() {
@Override
public void visitAssignExpr(@NotNull PsiPerlAssignExpr o) {
if (selectedVersion.lesserThan(V5_22)) {
PsiElement run = o.getFirstChild();
while (run != null) {
if (BITWISE_ASSIGN_OPERATORS_TOKENSET.contains(PsiUtilCore.getElementType(run)) && run.getTextLength() > 2) {
registerProblem(holder, o, PerlBundle.message("perl.inspection.string.binary.unavailable"), buildChangePerlVersionQuickFixes(GREATER_OR_EQUAL_V522));
break;
}
run = run.getNextSibling();
}
}
super.visitAssignExpr(o);
}
@Override
public void visitFileReadExpr(@NotNull PsiPerlFileReadExpr o) {
if (selectedVersion.lesserThan(V5_22)) {
PsiElement firstChild = o.getFirstChild();
PsiElement lastChild = o.getLastChild();
if (PsiUtilCore.getElementType(firstChild) == LEFT_ANGLE && firstChild.getTextLength() > 1 || PsiUtilCore.getElementType(lastChild) == RIGHT_ANGLE && lastChild.getTextLength() > 1) {
registerProblem(holder, o, PerlBundle.message("perl.inspection.double.diamond.unavailable"), buildChangePerlVersionQuickFixes(GREATER_OR_EQUAL_V522));
}
}
super.visitFileReadExpr(o);
}
@Override
public void visitBitwiseAndExpr(@NotNull PsiPerlBitwiseAndExpr o) {
processBinaryExpression(o);
super.visitBitwiseAndExpr(o);
}
private void processBinaryExpression(@NotNull PsiPerlExpr o) {
if (!selectedVersion.lesserThan(V5_22)) {
return;
}
PsiElement run = o.getFirstChild();
while (run != null) {
if (BITWISE_OPERATORS_TOKENSET.contains(PsiUtilCore.getElementType(run)) && run.getTextLength() > 1) {
registerProblem(holder, o, PerlBundle.message("perl.inspection.string.binary.unavailable"), buildChangePerlVersionQuickFixes(GREATER_OR_EQUAL_V522));
break;
}
run = run.getNextSibling();
}
}
@Override
public void visitBitwiseOrXorExpr(@NotNull PsiPerlBitwiseOrXorExpr o) {
processBinaryExpression(o);
super.visitBitwiseOrXorExpr(o);
}
@Override
public void visitEqualExpr(@NotNull PsiPerlEqualExpr o) {
if (o.getChildren().length > 2) {
reportOperatorsChainingAvailability(o);
PsiElement run = o.getFirstChild();
while (run != null) {
if (PerlTokenSets.UNCHAINABLE_OPERATORS.contains(PsiUtilCore.getElementType(run))) {
registerProblem(holder, o, PerlBundle.message("perl.inspection.chained.expr.invalid"));
break;
}
run = run.getNextSibling();
}
}
super.visitEqualExpr(o);
}
private void reportOperatorsChainingAvailability(@NotNull PsiElement o) {
if (selectedVersion.lesserThan(V5_32)) {
registerProblem(holder, o, PerlBundle.message("perl.inspection.chained.expr.unavailable"), buildChangePerlVersionQuickFixes(GREATER_OR_EQUAL_V532));
}
}
@Override
public void visitCompareExpr(@NotNull PsiPerlCompareExpr o) {
if (o.getChildren().length > 2) {
reportOperatorsChainingAvailability(o);
}
super.visitCompareExpr(o);
}
@Override
public void visitIsaExpr(@NotNull PsiPerlIsaExpr o) {
if (selectedVersion.lesserThan(V5_32)) {
registerProblem(holder, o, PerlBundle.message("perl.inspection.isa.expr.unavailable"), buildChangePerlVersionQuickFixes(GREATER_OR_EQUAL_V532));
}
super.visitIsaExpr(o);
}
@Override
public void visitHashHashSlice(@NotNull PsiPerlHashHashSlice o) {
if (selectedVersion.lesserThan(V5_20)) {
registerProblem(holder, o, PerlBundle.message("perl.inspection.hash.hash.slice.unavailable"), buildChangePerlVersionQuickFixes(PerlVersion.GREATER_OR_EQUAL_V520));
}
super.visitHashHashSlice(o);
}
@Override
public void visitHashArraySlice(@NotNull PsiPerlHashArraySlice o) {
if (selectedVersion.lesserThan(V5_20)) {
registerProblem(holder, o, PerlBundle.message("perl.inspection.hash.array.slice.unavailable"), buildChangePerlVersionQuickFixes(PerlVersion.GREATER_OR_EQUAL_V520));
}
super.visitHashArraySlice(o);
}
@Override
public void visitSignatureContent(@NotNull PsiPerlSignatureContent o) {
PsiElement signatureOwner = o.getParent();
if (!(signatureOwner instanceof PsiPerlSubDefinitionImpl) && !(signatureOwner instanceof PsiPerlSubExprImpl)) {
return;
}
if (selectedVersion.lesserThan(V5_20)) {
registerProblem(holder, o, PerlBundle.message("perl.inspection.sub.signatures", selectedVersion.getStrictDottedVersion()), buildChangePerlVersionQuickFixes(PerlVersion.GREATER_OR_EQUAL_V520));
} else if (selectedVersion.greaterThan(V5_20) && selectedVersion.lesserThan(V5_28)) {
PsiElement run = o.getPrevSibling();
while (run != null) {
if (run instanceof PsiPerlAttributes) {
LocalQuickFix flipQuickFix = getFlipElementsQuickFix(run);
registerProblem(holder, o, PerlBundle.message("perl.inspection.sub.signatures.after.attributes", selectedVersion.getStrictDottedVersion()), isOnTheFly ? ArrayUtil.prepend(flipQuickFix, buildChangePerlVersionQuickFixes(version -> version.equals(V5_20) || version.compareTo(V5_28) >= 0)) : new LocalQuickFix[] { flipQuickFix });
return;
}
run = run.getPrevSibling();
}
} else {
PsiElement run = o.getPrevSibling();
while (run != null) {
if (run instanceof PsiPerlAttributes) {
LocalQuickFix flipQuickFix = getFlipElementsQuickFix(run);
registerProblem(holder, o, PerlBundle.message("perl.inspection.sub.signatures.before.attributes", selectedVersion.getStrictDottedVersion()), isOnTheFly ? ArrayUtil.prepend(flipQuickFix, buildChangePerlVersionQuickFixes(version -> version.greaterThan(V5_20) && version.lesserThan(V5_28))) : new LocalQuickFix[] { flipQuickFix });
return;
}
run = run.getNextSibling();
}
}
}
@NotNull
private LocalQuickFix getFlipElementsQuickFix(@NotNull PsiElement attributes) {
SmartPsiElementPointer<?> attributesPointer = SmartPointerManager.getInstance(attributes.getProject()).createSmartPsiElementPointer(attributes);
return new MyTopQuickFix() {
@Override
@Nls
@NotNull
public String getFamilyName() {
return PerlBundle.message("perl.quickfix.switch.signature.with.attributes");
}
@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
PsiElement attributesElement = attributesPointer.getElement();
PsiElement signatureElement = descriptor.getPsiElement();
if (attributesElement == null || !attributesElement.isValid() || !signatureElement.isValid()) {
return;
}
PsiElement subDefinition = signatureElement.getParent();
if (attributesElement.getNode().getStartOffset() < signatureElement.getNode().getStartOffset()) {
PsiElement closeParen = PerlPsiUtil.getNextSignificantSibling(signatureElement);
if (PsiUtilCore.getElementType(closeParen) != RIGHT_PAREN) {
return;
}
subDefinition.addAfter(attributesElement, closeParen);
} else {
PsiElement openParen = PerlPsiUtil.getPrevSignificantSibling(signatureElement);
if (PsiUtilCore.getElementType(openParen) != LEFT_PAREN) {
return;
}
subDefinition.addBefore(attributesElement, openParen);
subDefinition.addBefore(PerlElementFactory.createSpace(project), openParen);
}
attributesElement.delete();
}
};
}
@NotNull
private LocalQuickFix[] buildChangePerlVersionQuickFixes(@NotNull Predicate<PerlVersion> versionPredicate) {
List<LocalQuickFix> result = new ArrayList<>();
result.add(new LocalQuickFix() {
@Override
@Nls
@NotNull
public String getFamilyName() {
return PerlBundle.message("perl.quickfix.change.language.level");
}
@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
Perl5SettingsConfigurable.open(project);
}
});
PerlVersion.ALL_VERSIONS.stream().filter(versionPredicate).forEach(it -> result.add(new LocalQuickFix() {
@Override
@Nls
@NotNull
public String getFamilyName() {
return PerlBundle.message("perl.quickfix.set.language.level", it.getStrictDottedVersion());
}
@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
PerlSharedSettings.getInstance(project).setTargetPerlVersion(it);
DaemonCodeAnalyzer.getInstance(project).restart();
}
}));
return result.toArray(new LocalQuickFix[0]);
}
};
}
Aggregations