public static boolean isAvailable(@NotNull PsiModifierListOwner modifierListOwner, @NotNull String annotationFQN) { if(!modifierListOwner.isValid()) { return false; } if(!PsiUtil.isLanguageLevel5OrHigher(modifierListOwner)) { return false; } // e.g. PsiTypeParameterImpl doesn't have modifier list PsiModifierList modifierList = modifierListOwner.getModifierList(); return modifierList != null && !(modifierList instanceof LightElement) && !(modifierListOwner instanceof LightElement) && !AnnotationUtil.isAnnotated(modifierListOwner, annotationFQN, false, false, true); }
private static void addPhysicalElements(PsiElement[] elements, LinkedHashSet<PsiElement> to) { for (PsiElement element : elements) { PsiElement mirror = PsiImplUtil.handleMirror(element); if (!(mirror instanceof LightElement)) { to.add(mirror); } } }
public ConstructorParameterOnFieldRenameRenamer(PsiField aField, String newFieldName) { final JavaCodeStyleManager styleManager = JavaCodeStyleManager.getInstance(aField.getProject()); final String propertyName = styleManager.variableNameToPropertyName(aField.getName(), VariableKind.FIELD); if (!Comparing.strEqual(propertyName, styleManager.variableNameToPropertyName(newFieldName, VariableKind.FIELD))) { final String paramName = styleManager.propertyNameToVariableName(propertyName, VariableKind.PARAMETER); final PsiClass aClass = aField.getContainingClass(); Set<PsiParameter> toRename = new HashSet<PsiParameter>(); for (PsiMethod constructor : aClass.getConstructors()) { if (constructor instanceof PsiMirrorElement) { final PsiElement prototype = ((PsiMirrorElement)constructor).getPrototype(); if (prototype instanceof PsiMethod && ((PsiMethod)prototype).isConstructor()) { constructor = (PsiMethod)prototype; } else { continue; } } if (constructor instanceof LightElement) continue; final PsiParameter[] parameters = constructor.getParameterList().getParameters(); for (final PsiParameter parameter : parameters) { final String parameterName = parameter.getName(); if (paramName.equals(parameterName) || propertyName.equals(styleManager.variableNameToPropertyName(parameterName, VariableKind.PARAMETER))) { toRename.add(parameter); } } } myElements.addAll(toRename); suggestAllNames(aField.getName(), newFieldName); } }
@Override public void prepareRenaming(PsiElement element, String newName, Map<PsiElement, String> allRenames, SearchScope scope) { final PsiMethod[] constructors = ((PsiClass) element).getConstructors(); for (PsiMethod constructor : constructors) { if (constructor instanceof PsiMirrorElement) { final PsiElement prototype = ((PsiMirrorElement)constructor).getPrototype(); if (prototype instanceof PsiNamedElement) { allRenames.put(prototype, newName); } } else if (!(constructor instanceof LightElement)) { allRenames.put(constructor, newName); } } }
public static MultiMap<PsiElement, UsageInfo> classifyUsages(Collection<? extends PsiElement> elements, UsageInfo[] usages) { final MultiMap<PsiElement, UsageInfo> result = new MultiMap<PsiElement, UsageInfo>(); for (UsageInfo usage : usages) { LOG.assertTrue(usage instanceof MoveRenameUsageInfo); if (usage.getReference() instanceof LightElement) { continue; //filter out implicit references (e.g. from derived class to super class' default constructor) } MoveRenameUsageInfo usageInfo = (MoveRenameUsageInfo)usage; if (usage instanceof RelatedUsageInfo) { final PsiElement relatedElement = ((RelatedUsageInfo)usage).getRelatedElement(); if (elements.contains(relatedElement)) { result.putValue(relatedElement, usage); } } else { PsiElement referenced = usageInfo.getReferencedElement(); if (elements.contains(referenced)) { result.putValue(referenced, usage); } else if (referenced != null) { PsiElement indirect = referenced.getNavigationElement(); if (elements.contains(indirect)) { result.putValue(indirect, usage); } } } } return result; }
public boolean poke(final PsiElement what, final int rate) { if (what == null) return false; if (!(what instanceof LightElement) && !what.isValid()) { throw new PsiInvalidElementAccessException(what, "Trying to resolve a reference to an invalid element"); } super.add(new RatedResolveResult(rate, what)); return true; }
@SuppressWarnings("MethodMayBeStatic") private void setNavigation(LightElement element, PsiClass dependencyHandlerClass, String methodName, int parametersCount) { for (PsiMethod method : dependencyHandlerClass.findMethodsByName(methodName, false)) { int methodParameterCount = method.getParameterList().getParametersCount(); if (methodParameterCount == parametersCount && method.getParameterList().getParameters()[methodParameterCount - 1].getType().equalsToText(GROOVY_LANG_CLOSURE)) { element.setNavigationElement(method); } } }
@Override @Nullable public PsiElement getNavigationElement(@NotNull ClsMethodImpl clsMethod) { if (!isGroovyLanguage(clsMethod)) return null; PsiMethod source = clsMethod.getSourceMirrorMethod(); if (source instanceof LightElement) { return source.getNavigationElement(); } return null; }
@NotNull private static String getElementInfo(@NotNull PsiElement element) { String text; if (element instanceof LightElement) { final PsiElement context = element.getContext(); text = context instanceof LightElement ? context.toString() : context != null ? context.getText() : null; } else { text = element.getText(); } return "invalid resolve candidate: " + element.getClass() + ", text: " + text; }
public static boolean isAccessible(@NotNull PsiElement place, @NotNull PsiMember member) { if (member instanceof LightElement) { return true; } if (place instanceof GrReferenceExpression && ((GrReferenceExpression)place).getQualifierExpression() == null) { if (member.getContainingClass() instanceof GroovyScriptClass) { //calling top level script members from the same script file return true; } } if (PsiTreeUtil.getParentOfType(place, GrDocComment.class) != null) return true; return com.intellij.psi.util.PsiUtil.isAccessible(member, place, null); }
private static boolean isLightElementDeclaredDuringCompletion(Object object) { if (!(object instanceof LightElement && object instanceof PsiNamedElement)) return false; final String name = ((PsiNamedElement)object).getName(); if (name == null) return false; return name.contains(CompletionInitializationContext.DUMMY_IDENTIFIER_TRIMMED.trim()) || name.contains(GrDummyIdentifierProvider.DUMMY_IDENTIFIER_DECAPITALIZED.trim()); }
@Override public boolean canProcessElement(@NotNull PsiElement element) { if (!(element instanceof LightElement)) return false; if (element instanceof GrRenameableLightElement) return false; final Language language = element.getLanguage(); return GroovyLanguage.INSTANCE.equals(language); }
@Override @Nullable public PsiElement getNavigationElement(@NotNull ClsMethodImpl clsMethod) { PsiMethod source = clsMethod.getSourceMirrorMethod(); if (source instanceof LightElement && source.getLanguage() == GroovyFileType.GROOVY_LANGUAGE) { return source.getNavigationElement(); } return null; }
@NotNull private static String getElementInfo(@NotNull PsiElement element) { String text; if (element instanceof LightElement) { final PsiElement context = element.getContext(); text = context != null ? context.getText() : null; } else { text = element.getText(); } return "invalid resolve candidate: " + element.getClass() + ", text: " + text; }
@Override public boolean canProcessElement(@NotNull PsiElement element) { if (!(element instanceof LightElement)) return false; if (element instanceof GrRenameableLightElement) return false; final Language language = element.getLanguage(); return GroovyFileType.GROOVY_LANGUAGE.equals(language); }
@Override public PsiElement getDeclaration() { return new LightElement(PsiManager.getInstance(myProject), JspLanguage.INSTANCE) { @Override public String toString() { return myDirective.name; } }; }
@Override public PsiElement getDeclaration() { return new LightElement(PsiManager.getInstance(myProject), JspLanguage.INSTANCE) { @Override public String toString() { return myAttribute.name; } }; }
public static MultiMap<PsiElement, UsageInfo> classifyUsages(Collection<? extends PsiElement> elements, UsageInfo[] usages) { final MultiMap<PsiElement, UsageInfo> result = new MultiMap<>(); for (UsageInfo usage : usages) { LOG.assertTrue(usage instanceof MoveRenameUsageInfo); if (usage.getReference() instanceof LightElement) { continue; //filter out implicit references (e.g. from derived class to super class' default constructor) } MoveRenameUsageInfo usageInfo = (MoveRenameUsageInfo)usage; if (usage instanceof RelatedUsageInfo) { final PsiElement relatedElement = ((RelatedUsageInfo)usage).getRelatedElement(); if (elements.contains(relatedElement)) { result.putValue(relatedElement, usage); } } else { PsiElement referenced = usageInfo.getReferencedElement(); if (elements.contains(referenced)) { result.putValue(referenced, usage); } else if (referenced != null) { PsiElement indirect = referenced.getNavigationElement(); if (elements.contains(indirect)) { result.putValue(indirect, usage); } } } } return result; }
@Nullable public RefElement getReference(final PsiElement elem, final boolean ignoreScope) { if (ApplicationManager.getApplication().runReadAction(new Computable<Boolean>() { @Override public Boolean compute() { return elem == null || !elem.isValid() || elem instanceof LightElement || !(elem instanceof PsiDirectory) && !belongsToScope(elem, ignoreScope); } })) { return null; } return getFromRefTableOrCache( elem, new NullableFactory<RefElementImpl>() { @Override public RefElementImpl create() { return ApplicationManager.getApplication().runReadAction(new Computable<RefElementImpl>() { @Override @Nullable public RefElementImpl compute() { final RefManagerExtension extension = getExtension(elem.getLanguage()); if (extension != null) { final RefElement refElement = extension.createRefElement(elem); if (refElement != null) return (RefElementImpl)refElement; } if (elem instanceof PsiFile) { return new RefFileImpl((PsiFile)elem, RefManagerImpl.this); } if (elem instanceof PsiDirectory) { return new RefDirectoryImpl((PsiDirectory)elem, RefManagerImpl.this); } return null; } }); } }, new Consumer<RefElementImpl>() { @Override public void consume(RefElementImpl element) { element.initialize(); for (RefManagerExtension each : myExtensions.values()) { each.onEntityInitialized(element, elem); } fireNodeInitialized(element); } }); }
private static boolean isLightElement(Object o) { return o instanceof LightElement && !(o instanceof GrPropertyForCompletion) && !(o instanceof GrAccessorMethod); }
@Override public void writeConstructor(final StringBuilder text, PsiMethod constructor, boolean isEnum) { LOG.assertTrue(constructor.isConstructor()); if (!isEnum) { text.append("public "); //writeModifiers(text, constructor.getModifierList(), JAVA_MODIFIERS); } /************* name **********/ //append constructor name text.append(constructor.getName()); /************* parameters **********/ GenerationUtil.writeParameterList(text, constructor.getParameterList().getParameters(), classNameProvider, null); final Set<String> throwsTypes = collectThrowsTypes(constructor, new THashSet<PsiMethod>()); if (!throwsTypes.isEmpty()) { text.append("throws ").append(StringUtil.join(throwsTypes, ", ")).append(' '); } /************* body **********/ text.append("{\n"); if (constructor instanceof GrReflectedMethod) { constructor = ((GrReflectedMethod)constructor).getBaseMethod(); } if (constructor instanceof GrMethod) { final GrConstructorInvocation invocation = PsiImplUtil.getChainingConstructorInvocation((GrMethod)constructor); if (invocation != null) { final GroovyResolveResult resolveResult = resolveChainingConstructor((GrMethod)constructor); if (resolveResult != null) { text.append(invocation.isSuperCall() ? "super(" : "this("); writeStubConstructorInvocation(text, (PsiMethod)resolveResult.getElement(), resolveResult.getSubstitutor(), invocation); text.append(");"); } } else if (constructor instanceof LightElement) { writeStubConstructorInvocation(constructor, text); } } text.append("\n}\n"); }
private static boolean doInject(String languageId, PsiElement host) { final PsiElement target = getTopLevelInjectionTarget(host); final PsiElement parent = target.getParent(); final Project project = host.getProject(); if (parent instanceof GrReturnStatement) { final GrControlFlowOwner owner = ControlFlowUtils.findControlFlowOwner(parent); if (owner instanceof GrOpenBlock && owner.getParent() instanceof GrMethod) { return JavaLanguageInjectionSupport.doInjectInJavaMethod(project, (PsiMethod)owner.getParent(), -1, languageId); } } else if (parent instanceof GrMethod) { return JavaLanguageInjectionSupport.doInjectInJavaMethod(project, (GrMethod)parent, -1, languageId); } else if (parent instanceof GrAnnotationNameValuePair) { final PsiReference ref = parent.getReference(); if (ref != null) { final PsiElement resolved = ref.resolve(); if (resolved instanceof PsiMethod) { return JavaLanguageInjectionSupport.doInjectInJavaMethod(project, (PsiMethod)resolved, -1, languageId); } } } else if (parent instanceof GrArgumentList && parent.getParent() instanceof GrMethodCall) { final PsiMethod method = ((GrMethodCall)parent.getParent()).resolveMethod(); if (method != null) { final int index = findParameterIndex(target, ((GrMethodCall)parent.getParent())); if (index >= 0) { return JavaLanguageInjectionSupport.doInjectInJavaMethod(project, method, index, languageId); } } } else if (parent instanceof GrAssignmentExpression) { final GrExpression expr = ((GrAssignmentExpression)parent).getLValue(); if (expr instanceof GrReferenceExpression) { final PsiElement element = ((GrReferenceExpression)expr).resolve(); if (element != null) { return doInject(languageId, element); } } } else { if (parent instanceof PsiVariable) { if (JavaLanguageInjectionSupport.doAddLanguageAnnotation(project, (PsiModifierListOwner)parent, languageId)) return true; } else if (target instanceof PsiVariable && !(target instanceof LightElement)) { if (JavaLanguageInjectionSupport.doAddLanguageAnnotation(project, (PsiModifierListOwner)target, languageId)) return true; } } return false; }