下面列出了怎么用com.intellij.psi.impl.source.PsiClassImpl的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* 取得实现类
*
* @param psiElement
* @return
*/
private List<PsiElement> getImplListElements(String name, String qualifiedName, PsiElement psiElement, GlobalSearchScope moduleScope) {
Project project = psiElement.getProject();
Collection<PsiReferenceList> psiReferenceListCollection = JavaSuperClassNameOccurenceIndex.getInstance().get(name, project, moduleScope);
List<PsiElement> elements = new ArrayList<>();
for (PsiElement psiReferenceList : psiReferenceListCollection) {
if (psiReferenceList instanceof PsiClassImpl) {
PsiClassImpl psiClassImpl = (PsiClassImpl) psiReferenceList.getContext();
if (psiClassImpl.getAnnotation(IOCBEAN_QUALI_FIED_NAME) != null) {
elements.add(psiClassImpl);
}
}
}
if (methodIocBeans.containsKey(qualifiedName)) {
elements.addAll(methodIocBeans.get(qualifiedName));
}
return elements;
}
@Test
public void TestIsImplicitUsageForClass() throws Exception {
ModuleHelper helper = mock(ModuleHelper.class);
PsiClass c = mock(PsiClassImpl.class);
PsiMethod method = mock(PsiMethod.class);
PsiModifierList list = mock(PsiModifierList.class);
PsiAnnotation annotation = mock(PsiAnnotation.class);
when(annotation.getQualifiedName()).thenReturn(BeforeStep.class.getCanonicalName());
when(list.getAnnotations()).thenReturn(new PsiAnnotation[]{annotation});
when(method.getModifierList()).thenReturn(list);
when(c.getMethods()).thenReturn(new PsiMethod[]{method});
when(helper.isGaugeModule(c)).thenReturn(true);
boolean isUsed = new ImplUsageProvider(null, helper).isImplicitUsage(c);
assertTrue(isUsed);
}
/**
* Get or create the update hint from the given type.
*
* @param collector
* @param type the type.
* @return the hint name.
*/
protected String updateHint(IPropertiesCollector collector, PsiClass type) {
if (type == null) {
return null;
}
if (type.isEnum()) {
// Register Enumeration in "hints" section
//String hint = ClassUtil.getJVMClassName(type);
String hint = type.getQualifiedName();
if (!collector.hasItemHint(hint)) {
ItemHint itemHint = collector.getItemHint(hint);
itemHint.setSourceType(hint);
if (type instanceof PsiClassImpl) {
itemHint.setSource(Boolean.TRUE);
}
PsiElement[] children = type.getChildren();
for (PsiElement c : children) {
if (c instanceof PsiEnumConstant) {
String enumName = ((PsiEnumConstant) c).getName();
// TODO: extract Javadoc
String description = null;
ValueHint value = new ValueHint();
value.setValue(enumName);
itemHint.getValues().add(value);
}
}
}
return hint;
}
return null;
}
@Test
public void TestIsImplicitUsageForClassWithNoMethods() throws Exception {
ModuleHelper helper = mock(ModuleHelper.class);
PsiClass c = mock(PsiClassImpl.class);
when(helper.isGaugeModule(c)).thenReturn(true);
when(c.getMethods()).thenReturn(new PsiMethod[]{});
boolean isUsed = new ImplUsageProvider(null, helper).isImplicitUsage(c);
assertFalse(isUsed);
}
public PsiClassImpl getPsiClass() {
return psiClass;
}
public void setPsiClass(PsiClassImpl psiClass) {
this.psiClass = psiClass;
}
@Override
public void actionPerformed(AnActionEvent event) {
String projectPath = StringUtils.EMPTY;
try {
//todo need check if need module.
UserConfigService.loadUserConfig(ProjectHelper.getProjectPath(event));
projectPath = ProjectHelper.getProjectPath(event);
Project project = event.getProject();
Editor editor = event.getData(LangDataKeys.EDITOR);
PsiFile currentFile = event.getData(LangDataKeys.PSI_FILE);
CaretModel caretModel = editor.getCaretModel();
LogicalPosition oldLogicPos = caretModel.getLogicalPosition();
String text = currentFile.getText();
List<String> lines = Splitter.on("\n").splitToList(text);
PojoLine pojo = getCursorLine(lines, oldLogicPos);
PsiDirectory containingDirectory = currentFile.getContainingDirectory();
// HintManager.getInstance().showInformationHint(editor,"success");
String dir = containingDirectory.getVirtualFile().getCanonicalPath() + GenCodeResponseHelper.getPathSplitter() + currentFile.getName();
AutoCodingRequest request = new AutoCodingRequest();
request.setRequestType("AutoCoding");
request.setCodingType("Setter");
ServerRequestHelper.fillCommonField(request);
if (pojo != null) {
request.setPojoName(pojo.getClassName());
LogicalPosition newStatementPos = new LogicalPosition(pojo.getLineNumber() , pojo.getLineStartPos() + 1);
LogicalPosition insertPos = new LogicalPosition(pojo.getLineNumber() + 1 , 0 );
caretModel.moveToLogicalPosition(newStatementPos);
PsiElement currentFileElement = event.getData(LangDataKeys.PSI_ELEMENT);
if (currentFileElement instanceof PsiClassImpl || currentFileElement instanceof ClsClassImpl) {
// Integer trueOffSet = getOffset(pojo, dir);
// if(trueOffSet != 0){
// offset = trueOffSet;
// }
Document document = PsiDocumentManager.getInstance(event.getProject()).getDocument(currentFile);
caretModel.moveToLogicalPosition(insertPos);
Integer offset = caretModel.getOffset();
String insert = insertSetter(project, pojo, document, currentFileElement, offset);
request.setInsert(insert);
// SettingService.getSetting().setLastInsertPos(offset);
// SettingService.getSetting().setLastInsertLength(setter.length());
}
}
// VirtualFileManager.getInstance().syncRefresh();
// ApplicationManager.getApplication().saveAll();
caretModel.moveToLogicalPosition(oldLogicPos);
SendToServerService.post(project,request);
} catch (Throwable ignored) {
LOGGER.error("actionPerformed :{}", ignored);
}finally {
LoggerWrapper.saveAllLogs(projectPath);
SettingService.updateLastRunTime();
}
}
public static void parseIdeaFieldInfo(@NotNull OnePojoInfo onePojoInfo, GenCodeResponse response){
String pojoName = onePojoInfo.getPojoName();
String pojoFileShortName = pojoName + ".java";
Project project = response.getRequest().getProject();
PsiFile[] psiFile = FilenameIndex
.getFilesByName(project, pojoFileShortName, GlobalSearchScope.projectScope(project));
PsiElement firstChild = psiFile[0].getFirstChild();
LOGGER.info("parseIdeaFieldInfo psiFile[0] path :{}", psiFile[0].getVirtualFile().getPath());
PsiElement child = null;
for (PsiFile each: psiFile){
VirtualFile vf = each.getVirtualFile();
LOGGER.info("parseIdeaFieldInfo :{}, :{}", vf.getPath(), onePojoInfo.getFullPojoPath());
if (removeSplit(vf.getPath()).equals(removeSplit(onePojoInfo.getFullPojoPath()))){
child = firstChild;
}
}
List<PsiElement> elements = Lists.newArrayList();
// i// Find Psi of class and package
do {
if (child instanceof PsiClassImpl) {
elements.add(child);
}
if (child instanceof PsiPackageStatementImpl){
onePojoInfo.setPojoPackage(((PsiPackageStatementImpl) child).getPackageName());
}
child = child.getNextSibling();
}
while (child != null);
PsiClassImpl psiClass = (PsiClassImpl) elements.get(0);
PsiElement context = psiClass.getContext();
if(context == null){
throw new RuntimeException("parse class error");
}
String text = context.getText();
onePojoInfo.setPojoPackage(parsePackage(text));
PsiField[] allFields = psiClass.getAllFields();
List<PojoFieldInfo> fieldList = Lists.newArrayList();
for (PsiField field : allFields) {
if(isStaticField(field)){
continue;
}
SupportFieldClass fieldClass = SupportFieldClass.fromDesc(field.getType().getCanonicalText());
LOGGER.info("parseIdeaFieldInfo canonicalText :{}", field.getType().getCanonicalText());
if(fieldClass == SupportFieldClass.NONE){
continue;
}
PojoFieldInfo fieldInfo = new PojoFieldInfo();
fieldInfo.setFieldComment(parseComment(field));
fieldInfo.setFieldName(field.getName());
fieldInfo.setFieldClass(fieldClass);
fieldInfo.setAnnotations(Lists.newArrayList());
if(!StringUtils.containsIgnoreCase(fieldInfo.getFieldComment(), "IgnoreAutoGenerate")) {
fieldList.add(fieldInfo);
}
}
onePojoInfo.setPojoFieldInfos(fieldList);
}
public boolean isImplicitUsage(PsiElement element) {
if (element == null || !moduleHelper.isGaugeModule(element)) return false;
if (element instanceof PsiClassImpl) return isClassUsed((PsiClassImpl) element);
if (element instanceof PsiParameterImpl) return isParameterUsed((PsiParameterImpl) element);
return isElementUsed(element);
}
private boolean isClassUsed(PsiClassImpl element) {
for (PsiMethod psiMethod : element.getMethods())
if (StepUtil.getGaugeStepAnnotationValues(psiMethod).size() > 0 || HookUtil.isHook(psiMethod)) return true;
return false;
}