com.intellij.psi.PsiClass#getChildren ( )源码实例Demo

下面列出了com.intellij.psi.PsiClass#getChildren ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

/**
 * 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;
}
 
private static void validateClassType(PsiClass classType, List<Diagnostic> diagnostics, JavaDiagnosticsContext context) {
	for (PsiElement element : classType.getChildren()) {
		if (element instanceof PsiField) {
			PsiField field = (PsiField) element;
			validateField(field, diagnostics, context);
		}
	}
}
 
private void processConfigProperties(PsiModifierListOwner psiElement, PsiAnnotation configPropertiesAnnotation,
									 ConfigPropertiesContext configPropertiesContext, IPropertiesCollector collector) {
	if (!(psiElement instanceof PsiClass)) {
		return;
	}
	PsiClass configPropertiesType = (PsiClass) psiElement;
	// Location (JAR, src)
	VirtualFile packageRoot = PsiTypeUtils.getRootDirectory(psiElement);
	String location = PsiTypeUtils.getLocation(psiElement.getProject(), packageRoot);
	// Quarkus Extension name
	String extensionName = PsiQuarkusUtils.getExtensionName(location);

	String prefix = determinePrefix(configPropertiesType, configPropertiesAnnotation);
	if (configPropertiesType.isInterface()) {
		// See
		// https://github.com/quarkusio/quarkus/blob/0796d712d9a3cf8251d9d8808b705f1a04032ee2/extensions/arc/deployment/src/main/java/io/quarkus/arc/deployment/configproperties/InterfaceConfigPropertiesUtil.java#L89
		List<PsiClass> allInterfaces = new ArrayList(Arrays.asList(findInterfaces(configPropertiesType)));
		allInterfaces.add(0, configPropertiesType);

		for (PsiClass configPropertiesInterface : allInterfaces) {
			// Loop for each methods.
			PsiElement[] elements = configPropertiesInterface.getChildren();
			// Loop for each fields.
			for (PsiElement child : elements) {
				if (child instanceof PsiMethod) {
					PsiMethod method = (PsiMethod) child;
					if (method.getModifierList().hasExplicitModifier(PsiModifier.DEFAULT)) { // don't do anything with default methods
						continue;
					}
					if (method.hasParameters()) {
						LOGGER.info("Method " + method.getName() + " of interface "
										+ method.getContainingClass().getQualifiedName()
										+ " is not a getter method since it defined parameters");
						continue;
					}
					if (PsiType.VOID.equals(method.getReturnType())) {
						LOGGER.info("Method " + method.getName() + " of interface "
										+ method.getContainingClass().getQualifiedName()
										+ " is not a getter method since it returns void");
						continue;
					}
					String name = null;
					String defaultValue = null;
					PsiAnnotation configPropertyAnnotation = AnnotationUtils.getAnnotation(method,
							QuarkusConstants.CONFIG_PROPERTY_ANNOTATION);
					if (configPropertyAnnotation != null) {
						name = getAnnotationMemberValue(configPropertyAnnotation,
								QuarkusConstants.CONFIG_PROPERTY_ANNOTATION_NAME);
						defaultValue = getAnnotationMemberValue(configPropertyAnnotation,
								QuarkusConstants.CONFIG_PROPERTY_ANNOTATION_DEFAULT_VALUE);
					}
					if (name == null) {
						name = getPropertyNameFromMethodName(method);
					}
					if (name == null) {
						continue;
					}

					String propertyName = prefix + "."
							+ convertName(name, method, configPropertiesAnnotation, configPropertiesContext);
					String methodResultTypeName = PsiTypeUtils.getResolvedResultTypeName(method);
					PsiClass returnType = PsiTypeUtils.findType(method.getManager(), methodResultTypeName);

					// Method result type
					String type = PsiTypeUtils.getPropertyType(returnType, methodResultTypeName);

					// TODO: extract Javadoc from Java sources
					String description = null;

					// Method source
					String sourceType = PsiTypeUtils.getSourceType(method);
					String sourceMethod = PsiTypeUtils.getSourceMethod(method);

					// Enumerations
					super.updateHint(collector, returnType);

					if (PsiTypeUtils.isSimpleFieldType(returnType, methodResultTypeName)) {
						ItemMetadata metadata = super.addItemMetadata(collector, propertyName, type, description, sourceType, null,
								sourceMethod, defaultValue, extensionName, PsiTypeUtils.isBinary(method));
						PsiQuarkusUtils.updateConverterKinds(metadata, method, returnType);
					} else {
						populateConfigObject(returnType, propertyName, extensionName, new HashSet(), configPropertiesAnnotation, configPropertiesContext, collector);
					}

				}
			}
		}
	} else {
		// See
		// https://github.com/quarkusio/quarkus/blob/e8606513e1bd14f0b1aaab7f9969899bd27c55a3/extensions/arc/deployment/src/main/java/io/quarkus/arc/deployment/configproperties/ClassConfigPropertiesUtil.java#L117
		// TODO : validation
		populateConfigObject(configPropertiesType, prefix, extensionName, new HashSet<>(), configPropertiesAnnotation, configPropertiesContext, collector);
	}
}
 
private void populateConfigObject(PsiClass configPropertiesType, String prefixStr, String extensionName,
								  Set<PsiClass> typesAlreadyProcessed, PsiAnnotation configPropertiesAnnotation,
								  ConfigPropertiesContext configPropertiesContext, IPropertiesCollector collector) {
	if (typesAlreadyProcessed.contains(configPropertiesType)) {
		return;
	}
	typesAlreadyProcessed.add(configPropertiesType);
	PsiElement[] elements = configPropertiesType.getChildren();
	// Loop for each fields.
	for (PsiElement child : elements) {
		if (child instanceof PsiField) {
			// The following code is an adaptation for JDT of
			// Quarkus arc code:
			// https://github.com/quarkusio/quarkus/blob/e8606513e1bd14f0b1aaab7f9969899bd27c55a3/extensions/arc/deployment/src/main/java/io/quarkus/arc/deployment/configproperties/ClassConfigPropertiesUtil.java#L211
			PsiField field = (PsiField) child;
			boolean useFieldAccess = false;
			String setterName = JavaBeanUtil.getSetterName(field.getName());
			String configClassInfo = configPropertiesType.getQualifiedName();
			PsiMethod setter = findMethod(configPropertiesType, setterName, field.getType());
			if (setter == null) {
				if (!field.getModifierList().hasModifierProperty(PsiModifier.PUBLIC) || field.getModifierList().hasModifierProperty(PsiModifier.FINAL)) {
					LOGGER.info("Configuration properties class " + configClassInfo
									+ " does not have a setter for field " + field
									+ " nor is the field a public non-final field");
					continue;
				}
				useFieldAccess = true;
			}
			if (!useFieldAccess && !setter.getModifierList().hasModifierProperty(PsiModifier.PUBLIC)) {
				LOGGER.info("Setter " + setterName + " of class " + configClassInfo + " must be public");
				continue;
			}

			String name = field.getName();
			// The default value is managed with assign like : 'public String suffix = "!"';
			// Getting "!" value is possible but it requires to re-parse the Java file to
			// build a DOM CompilationUnit to extract assigned value.
			final String defaultValue = null;
			String propertyName = prefixStr + "." + convertName(name, field, configPropertiesAnnotation, configPropertiesContext);

			String fieldTypeName = PsiTypeUtils.getResolvedTypeName(field);
			PsiClass fieldClass = PsiTypeUtils.findType(field.getManager(), fieldTypeName);
			if (PsiTypeUtils.isSimpleFieldType(fieldClass, fieldTypeName)) {

				// Class type
				String type = PsiTypeUtils.getPropertyType(fieldClass, fieldTypeName);

				// Javadoc
				String description = null;

				// field and class source
				String sourceType = PsiTypeUtils.getSourceType(field);
				String sourceField = PsiTypeUtils.getSourceField(field);

				// Enumerations
				super.updateHint(collector, fieldClass);

				ItemMetadata metadata = super.addItemMetadata(collector, propertyName, type, description, sourceType, sourceField, null,
						defaultValue, extensionName, PsiTypeUtils.isBinary(field));
				PsiQuarkusUtils.updateConverterKinds(metadata, field, fieldClass);
			} else {
				populateConfigObject(fieldClass, propertyName, extensionName, typesAlreadyProcessed, configPropertiesAnnotation, configPropertiesContext, collector);
			}
		}
	}
}