public DataWriter(PsiFile file, Project project, PsiClass cls) { super(project, file); factory = JavaPsiFacade.getElementFactory(project); this.file = file; this.project = project; this.cls = cls; }
PsiClass currentClass, String generateClassName) { factory = JavaPsiFacade.getElementFactory(project); this.file = file; this.generateClassName = generateClassName;
/** * Returns the class type for qualified class name. * * @param qName qualified class name. * @param project * @param resolveScope the scope in which the class is searched. * @return the class instance. */ public static PsiClassType getTypeByName(String qName, Project project, GlobalSearchScope resolveScope) { PsiElementFactory factory = JavaPsiFacade.getInstance(project).getElementFactory(); return factory.createTypeByFQClassName(qName, resolveScope); }
private static PsiType replaceArrayType(PsiType type, @NotNull PsiElement context) { if (type instanceof PsiArrayType) { type = JavaPsiFacade.getElementFactory(context.getProject()) .getArrayClassType(((PsiArrayType)type).getComponentType(), PsiUtil.getLanguageLevel(context)); } return type; }
private PsiAnnotation copyAnnotation(PsiModifierListOwner owner, PsiAnnotation annotation) { final String notNull = checkContainer(annotation, false); if (notNull != null) { return JavaPsiFacade.getElementFactory(owner.getProject()).createAnnotationFromText("@" + notNull, owner); } return null; }
public static boolean isReceiverType(@Nullable PsiType receiverType, PsiClass containingClass, PsiSubstitutor psiSubstitutor) { if (receiverType == null) { return false; } return TypeConversionUtil.isAssignable(JavaPsiFacade.getElementFactory(containingClass.getProject()).createType(containingClass, psiSubstitutor), replaceArrayType(receiverType, containingClass)); }
public void importClazz(PsiJavaFile file, String clazzName) { if (!JavaUtils.hasImportClazz(file, clazzName)) { Optional<PsiClass> clazz = JavaUtils.findClazz(project, clazzName); PsiImportList importList = file.getImportList(); if (clazz.isPresent() && null != importList) { PsiElementFactory elementFactory = javaPsiFacade.getElementFactory(); PsiImportStatement statement = elementFactory.createImportStatement(clazz.get()); importList.add(statement); editorService.format(file, statement); } } } }
@NotNull private static PsiClassType obtainSafeSuperType(@NotNull PsiTypeParameter typeParameter) { final PsiClassType superType = typeParameter.getSuperTypes()[0]; final PsiClassType.ClassResolveResult result = superType.resolveGenerics(); final PsiClass superClass = result.getElement(); if (superClass != null) { final PsiSubstitutor substitutor = result.getSubstitutor().put(typeParameter, null); return JavaPsiFacade.getInstance(typeParameter.getProject()).getElementFactory().createType(superClass, substitutor); } return superType; }
@Nullable public PsiClassType getBoxedType(final PsiManager manager, final GlobalSearchScope resolveScope) { final String boxedQName = getBoxedTypeName(); //[ven]previous call returns null for NULL, VOID if (boxedQName == null) return null; final PsiClass aClass = JavaPsiFacade.getInstance(manager.getProject()).findClass(boxedQName, resolveScope); if (aClass == null) return null; return JavaPsiFacade.getInstance(manager.getProject()).getElementFactory().createType(aClass); }
public static void expand(@NotNull PsiJavaCodeReferenceElement refExpr, PsiImportStaticStatement staticImport) { final PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(refExpr.getProject()); final PsiReferenceExpression referenceExpression = elementFactory.createReferenceExpression(staticImport.resolveTargetClass()); if (refExpr instanceof PsiReferenceExpression) { ((PsiReferenceExpression)refExpr).setQualifierExpression(referenceExpression); } else { refExpr.replace(elementFactory.createReferenceFromText(referenceExpression.getText() + "." + refExpr.getText(), refExpr)); } }
public void addAnnotation(@NotNull PsiModifierListOwner parameter, @NotNull Annotation annotation) { PsiModifierList modifierList = parameter.getModifierList(); if (JavaUtils.isAnnotationPresent(parameter, annotation) || null == modifierList) { return; } JavaService.getInstance(parameter.getProject()).importClazz((PsiJavaFile) parameter.getContainingFile(), annotation.getQualifiedName()); PsiElementFactory elementFactory = JavaPsiFacade.getInstance(project).getElementFactory(); PsiAnnotation psiAnnotation = elementFactory.createAnnotationFromText(annotation.toString(), parameter); modifierList.add(psiAnnotation); JavaCodeStyleManager.getInstance(project).shortenClassReferences(psiAnnotation.getParent()); }
/** * Tests that the Quick fix for a parameter with no annotation returns the same parameter * unchanged. */ public void testQuickFix_noAnnotation() { Project myProject = myFixture.getProject(); PsiParameter parameter = JavaPsiFacade.getInstance(myProject) .getElementFactory() .createParameter("param", PsiType.BOOLEAN); runQuickFixTest(parameter, "boolean param"); }
public void testQuickFix_noConstructor() { ProblemDescriptorImpl problemDescriptorMock = mock(ProblemDescriptorImpl.class); MockitoAnnotations.initMocks(this); PsiClass psiClass = JavaPsiFacade.getInstance(myFixture.getProject()) .getElementFactory() .createClass(getName()); ConstructorInspection constructorInspection = new ConstructorInspection(); ConstructorInspection.MyQuickFix myQuickFix = constructorInspection.new MyQuickFix(psiClass); myQuickFix.applyFix(myFixture.getProject(), problemDescriptorMock); Assert.assertEquals(1, psiClass.getConstructors().length); Assert.assertTrue(EndpointUtilities.isPublicNullaryConstructor(psiClass.getConstructors()[0])); }
/** Test that the Quick fix for a parameter with @Named returns the same parameter unchanged. */ public void testQuickFix_withNamedAnnotation() { Project myProject = myFixture.getProject(); PsiParameter parameter = JavaPsiFacade.getInstance(myProject) .getElementFactory() .createParameter("param", PsiType.BOOLEAN); parameter.getModifierList().addAnnotation(GctConstants.APP_ENGINE_ANNOTATION_NAMED); runQuickFixTest(parameter, "@" + GctConstants.APP_ENGINE_ANNOTATION_NAMED + " boolean param"); }
/** * Tests that the Quick fix for a parameter with the javax @Nullable returns the parameter * without @Nullable. */ public void testQuickFix_withJavaxNullableAnnotation() { Project myProject = myFixture.getProject(); PsiParameter parameter = JavaPsiFacade.getInstance(myProject) .getElementFactory() .createParameter("param", PsiType.BOOLEAN); parameter.getModifierList().addAnnotation("javax.annotation.Nullable"); runQuickFixTest(parameter, " boolean param"); }
/** * Tests that the Quick fix for a parameter with @DefaultValue returns the parameter * without @DefaultValue. */ public void testQuickFix_withDefaultAnnotation() { Project myProject = myFixture.getProject(); PsiParameter parameter = JavaPsiFacade.getInstance(myProject) .getElementFactory() .createParameter("param", PsiType.BOOLEAN); parameter.getModifierList().addAnnotation(GctConstants.APP_ENGINE_ANNOTATION_DEFAULT_VALUE); runQuickFixTest(parameter, " boolean param"); }
/** * Tests that the Quick fix for a parameter with the App Engine @Nullable returns the parameter * without @Nullable. */ public void testQuickFix_withGaeNullableAnnotation() { Project myProject = myFixture.getProject(); PsiParameter parameter = JavaPsiFacade.getInstance(myProject) .getElementFactory() .createParameter("param", PsiType.BOOLEAN); parameter.getModifierList().addAnnotation(GctConstants.APP_ENGINE_ANNOTATION_NULLABLE); runQuickFixTest(parameter, " boolean param"); }
private void runQuickFixTest(String parameterText, String expectedString) { Project myProject = myFixture.getProject(); PsiParameter parameter = JavaPsiFacade.getInstance(myProject) .getElementFactory() .createParameterFromText(parameterText, null); ApiParameterInspection.MyQuickFix myQuickFix = new ApiParameterInspection().new MyQuickFix(); MockProblemDescriptor problemDescriptor = new MockProblemDescriptor(parameter, "", ProblemHighlightType.ERROR); myQuickFix.applyFix(myFixture.getProject(), problemDescriptor); Assert.assertEquals(expectedString, parameter.getText()); }
private void runQuickFixTest(String annotationString, String expectedString) { final Project myProject = myFixture.getProject(); PsiAnnotation annotation = JavaPsiFacade.getInstance(myProject) .getElementFactory() .createAnnotationFromText(annotationString, null); MockProblemDescriptor problemDescriptor = new MockProblemDescriptor(annotation, "", ProblemHighlightType.ERROR); ApiNamespaceInspection.MyQuickFix myQuickFix = new ApiNamespaceInspection().new MyQuickFix(); myQuickFix.applyFix(myProject, problemDescriptor); assertEquals(expectedString, annotation.getText()); }
public void testQuickFix() { PsiMethod someFunction = JavaPsiFacade.getInstance(myFixture.getProject()) .getElementFactory() .createMethod("someFunction", PsiType.VOID); MockProblemDescriptor problemDescriptor = new MockProblemDescriptor(someFunction, "", ProblemHighlightType.ERROR); FullJavaNameInspection.MyQuickFix myQuickFix = new FullJavaNameInspection().new MyQuickFix(); myQuickFix.applyFix(myFixture.getProject(), problemDescriptor); assertEquals("someFunction_1", someFunction.getName()); myQuickFix.applyFix(myFixture.getProject(), problemDescriptor); assertEquals("someFunction_1_1", someFunction.getName()); }