private boolean isStructuralType( PsiTypeElement typeElem ) { if( typeElem != null ) { PsiClass psiClass = PsiUtil.resolveClassInType( typeElem.getType() ); if( psiClass == null ) { return false; } PsiAnnotation structuralAnno = psiClass.getModifierList() == null ? null : psiClass.getModifierList().findAnnotation( "manifold.ext.api.Structural" ); if( structuralAnno != null ) { return true; } } return false; }
@Nullable public PsiType inferType( PsiTypeElement typeElement ) { PsiType psiType = null; final PsiElement parent = typeElement.getParent(); if( (parent instanceof PsiLocalVariable && isVar( (PsiLocalVariable)parent )) || (parent instanceof PsiParameter && isVarForEach( (PsiParameter)parent )) ) { if( parent instanceof PsiLocalVariable ) { psiType = processLocalVariableInitializer( ((PsiLocalVariable)parent).getInitializer() ); } else { psiType = processForeach( ((PsiParameter)parent).getDeclarationScope() ); } if( null == psiType ) { psiType = PsiType.getJavaLangObject( typeElement.getManager(), GlobalSearchScope.allScope( typeElement.getProject() ) ); } } return psiType; }
@Override public void tokenize(@NotNull T element, TokenConsumer consumer) { final PsiIdentifier psiIdentifier = PsiTreeUtil.getChildOfType(element, PsiIdentifier.class); final PsiTypeElement psiType = PsiTreeUtil.getChildOfType(element, PsiTypeElement.class); if (psiIdentifier == null) { return; } final String identifier = psiIdentifier.getText(); final String type = psiType==null?null:psiType.getText(); if (identifier == null) { return; } if (type == null || !type.equalsIgnoreCase(identifier)) { myIdentifierTokenizer.tokenize(psiIdentifier, consumer); } if (psiType != null) { myTypeTokenizer.tokenize(psiType, consumer); } }
@Override protected void doFix(Project project, ProblemDescriptor descriptor) throws IncorrectOperationException { final PsiElement element = descriptor.getPsiElement(); final PsiElement parent = element.getParent(); if (!(parent instanceof PsiMethod)) { return; } final PsiMethod method = (PsiMethod)parent; final PsiTypeElement returnTypeElement = method.getReturnTypeElement(); if (returnTypeElement == null) { return; } returnTypeElement.delete(); }
@Override public void visitVariable(@NotNull PsiVariable variable) { super.visitVariable(variable); final PsiType type = variable.getType(); final PsiType componentType = type.getDeepComponentType(); if (!(componentType instanceof PsiClassType)) { return; } final String className = ((PsiClassType)componentType).getClassName(); @NonNls final String javaLangReflect = "java.lang.reflect."; if (!className.startsWith(javaLangReflect)) { return; } final PsiTypeElement typeElement = variable.getTypeElement(); if (typeElement == null) { return; } registerError(typeElement); }
@Override public void visitField(@NotNull PsiField field) { super.visitField(field); if (field.hasModifierProperty(PsiModifier.STATIC)) { return; } final PsiTypeElement typeElement = field.getTypeElement(); if (typeElement == null) { return; } if (!ConcreteClassUtil.typeIsConcreteClass(typeElement, ignoreAbstractClasses)) { return; } final String variableName = field.getName(); registerError(typeElement, variableName); }
@Override public void visitMethod(@NotNull PsiMethod method) { super.visitMethod(method); if (method.isConstructor()) { return; } final PsiTypeElement typeElement = method.getReturnTypeElement(); if (typeElement == null) { return; } if (!ConcreteClassUtil.typeIsConcreteClass(typeElement, ignoreAbstractClasses)) { return; } registerError(typeElement); }
@Override public void visitField(@NotNull PsiField field) { super.visitField(field); if (!field.hasModifierProperty(PsiModifier.STATIC)) { return; } final PsiTypeElement typeElement = field.getTypeElement(); if (typeElement == null) { return; } if (!ConcreteClassUtil.typeIsConcreteClass(typeElement, ignoreAbstractClasses)) { return; } final String variableName = field.getName(); registerError(typeElement, variableName); }
@Override public void visitCatchSection(PsiCatchSection section) { super.visitCatchSection(section); final PsiParameter parameter = section.getParameter(); if (parameter == null) { return; } final PsiTypeElement typeElement = parameter.getTypeElement(); if (typeElement == null) { return; } final PsiTypeElement[] childTypeElements = PsiTreeUtil.getChildrenOfType(typeElement, PsiTypeElement.class); if (childTypeElements != null) { for (PsiTypeElement childTypeElement : childTypeElements) { checkTypeElement(childTypeElement); } } else { checkTypeElement(typeElement); } }
public boolean satisfiedBy(PsiElement element) { if (!(element instanceof PsiVariable)) { return false; } if (ErrorUtil.containsError(element)) { return false; } final PsiVariable var = (PsiVariable)element; final PsiTypeElement typeElement = var.getTypeElement(); if (typeElement == null) { return false; // Could be true for enum constants. } final PsiType elementType = typeElement.getType(); final PsiType type = var.getType(); return elementType.getArrayDimensions() != type.getArrayDimensions(); }
@Override public void visitVariable(@NotNull PsiVariable var) { super.visitVariable(var); final PsiType declaredType = var.getType(); if (declaredType.getArrayDimensions() == 0) { return; } final PsiTypeElement typeElement = var.getTypeElement(); if (typeElement == null) { return; // Could be true for enum constants. } final PsiType elementType = typeElement.getType(); if (elementType.equals(declaredType)) { return; } registerVariableError(var); }
@Override public void visitParameter(@NotNull PsiParameter parameter) { super.visitParameter(parameter); if (parameter.getDeclarationScope() instanceof PsiCatchSection) { return; } final PsiTypeElement typeElement = parameter.getTypeElement(); if (typeElement == null) { return; } if (!ConcreteClassUtil.typeIsConcreteClass(typeElement, ignoreAbstractClasses)) { return; } final String variableName = parameter.getName(); registerError(typeElement, variableName); }
@Override public void tokenize(@NotNull PsiTypeElement element, TokenConsumer consumer) { final PsiType type = element.getType(); if (type instanceof PsiDisjunctionType) { tokenizeComplexType(element, consumer); return; } final PsiClass psiClass = PsiUtil.resolveClassInType(type); if (psiClass == null || psiClass.getContainingFile() == null || psiClass.getContainingFile().getVirtualFile() == null) { return; } final VirtualFile virtualFile = psiClass.getContainingFile().getVirtualFile(); final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(element.getProject()).getFileIndex(); final boolean isInSource = (virtualFile != null) && fileIndex.isInContent(virtualFile); if (isInSource) { consumer.consumeToken(element, element.getText(), true, 0, getRangeToCheck(element.getText(), psiClass.getName()), IdentifierSplitter.getInstance()); } }
@Nullable public PsiType inferType(PsiTypeElement typeElement) { PsiType psiType = null; final PsiElement parent = typeElement.getParent(); if ((parent instanceof PsiLocalVariable && isValOrVar((PsiLocalVariable) parent)) || (parent instanceof PsiParameter && isValOrVarForEach((PsiParameter) parent))) { if (parent instanceof PsiLocalVariable) { psiType = processLocalVariableInitializer(((PsiLocalVariable) parent).getInitializer()); } else { psiType = processParameterDeclaration(((PsiParameter) parent).getDeclarationScope()); } if (null == psiType) { psiType = PsiType.getJavaLangObject(typeElement.getManager(), GlobalSearchScope.allScope(typeElement.getProject())); } } return psiType; }
private boolean navigateToConstructorIfProvider(PsiParameter psiParameter) { PsiTypeElement declaringTypeElement = psiParameter.getTypeElement(); PsiClass classElement = JavaPsiFacade.getInstance(psiParameter.getProject()).findClass( declaringTypeElement.getType().getCanonicalText(), declaringTypeElement.getResolveScope()); if (classElement == null) { return false; } for (PsiMethod method : classElement.getConstructors()) { if (PsiConsultantImpl.hasAnnotation(method, CLASS_INJECT) && navigateToElement(method)) { return true; } } return false; }
/** * @return a {@link com.intellij.codeInsight.daemon.GutterIconNavigationHandler} if the element * is a PsiMethod annotated with @Provides. */ @Nullable @Override public LineMarkerInfo getLineMarkerInfo(@NotNull final PsiElement element) { // Check methods first (includes constructors). if (element instanceof PsiMethod) { PsiMethod methodElement = (PsiMethod) element; // Does it have an @Provides? if (hasAnnotation(element, CLASS_PROVIDES)) { PsiTypeElement returnTypeElement = methodElement.getReturnTypeElement(); if (returnTypeElement != null) { return new LineMarkerInfo<PsiElement>(element, returnTypeElement.getTextRange(), ICON, UPDATE_ALL, null, new ProvidesToInjectHandler(), LEFT); } } // Is it an @Inject-able constructor? if (methodElement.isConstructor() && hasAnnotation(element, CLASS_INJECT)) { return new LineMarkerInfo<PsiElement>(element, element.getTextRange(), ICON, UPDATE_ALL, null, new ConstructorInjectToInjectionPlaceHandler(), LEFT); } } return null; }
@Override public PsiMethod[] generateGetters(PsiField field) { final Project project = field.getProject(); final PsiElementFactory factory = JavaPsiFacade.getElementFactory(project); final PsiMethod getter = PropertyUtil.generateGetterPrototype(field); final PsiType wrappedType = JavaFxPsiUtil.getWrappedPropertyType(field, project, JavaFxCommonClassNames.ourReadOnlyMap); final PsiTypeElement returnTypeElement = getter.getReturnTypeElement(); LOG.assertTrue(returnTypeElement != null); returnTypeElement.replace(factory.createTypeElement(wrappedType)); final PsiCodeBlock getterBody = getter.getBody(); LOG.assertTrue(getterBody != null); getterBody.getStatements()[0].replace(factory.createStatementFromText("return " + field.getName() + ".get();", field)); final PsiMethod propertyGetter = PropertyUtil.generateGetterPrototype(field); propertyGetter.setName(JavaCodeStyleManager.getInstance(project).variableNameToPropertyName(field.getName(), VariableKind.FIELD) + "Property"); return new PsiMethod[] {getter, propertyGetter}; }
@Override public PsiMethod[] generateSetters(PsiField field) { final PsiMethod setter = PropertyUtil.generateSetterPrototype(field); final Project project = field.getProject(); final PsiType wrappedType = JavaFxPsiUtil.getWrappedPropertyType(field, project, JavaFxCommonClassNames.ourWritableMap); final PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(project); final PsiTypeElement newTypeElement = elementFactory.createTypeElement(wrappedType); final PsiParameter[] parameters = setter.getParameterList().getParameters(); LOG.assertTrue(parameters.length == 1); final PsiParameter parameter = parameters[0]; final PsiTypeElement typeElement = parameter.getTypeElement(); LOG.assertTrue(typeElement != null); typeElement.replace(newTypeElement); final PsiCodeBlock body = setter.getBody(); LOG.assertTrue(body != null); body.getStatements()[0].replace(elementFactory.createStatementFromText("this." + field.getName() + ".set(" + parameter.getName() + ");", field)); return new PsiMethod[] {setter}; }
public static Location getParameterizedLocation(PsiClass psiClass, String paramSetName, String parameterizedClassName) { final PsiAnnotation annotation = AnnotationUtil.findAnnotationInHierarchy(psiClass, Collections.singleton(JUnitUtil.RUN_WITH)); if(annotation != null) { final PsiAnnotationMemberValue attributeValue = annotation.findAttributeValue("value"); if(attributeValue instanceof PsiClassObjectAccessExpression) { final PsiTypeElement operand = ((PsiClassObjectAccessExpression) attributeValue).getOperand(); if(InheritanceUtil.isInheritor(operand.getType(), parameterizedClassName)) { return new PsiMemberParameterizedLocation(psiClass.getProject(), psiClass, null, paramSetName); } } } return null; }
@Override public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException { if(!FileModificationService.getInstance().prepareFileForWrite(file)) { return; } myConjuncts.remove(myConjunct); myConjuncts.add(0, myConjunct); final String intersectionTypeText = StringUtil.join(myConjuncts, new Function<PsiTypeElement, String>() { @Override public String fun(PsiTypeElement element) { return element.getText(); } }, " & "); final PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(project); final PsiTypeCastExpression fixedCast = (PsiTypeCastExpression) elementFactory.createExpressionFromText("(" + intersectionTypeText + ") a", myCastTypeElement); final PsiTypeElement fixedCastCastType = fixedCast.getCastType(); LOG.assertTrue(fixedCastCastType != null); final PsiElement flippedTypeElement = myCastTypeElement.replace(fixedCastCastType); CodeStyleManager.getInstance(project).reformat(flippedTypeElement); }
public void testAddParamChangeReturnType() { doTest(() -> { myFixture.type("int param"); PsiDocumentManager.getInstance(getProject()).commitAllDocuments(); CaretModel model = myFixture.getEditor().getCaretModel(); PsiElement element = myFixture.getElementAtCaret(); PsiMethod method = PsiTreeUtil.getParentOfType(element, PsiMethod.class, false); assertTrue(method != null); PsiTypeElement returnTypeElement = method.getReturnTypeElement(); assertTrue(returnTypeElement != null); model.moveToOffset(returnTypeElement.getTextRange().getEndOffset()); int i = returnTypeElement.getTextLength(); while(i-- > 0) { myFixture.type('\b'); } myFixture.type("boolean"); }); }