Java Code Examples for com.intellij.psi.PsiField#getName()

The following examples show how to use com.intellij.psi.PsiField#getName() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EqualsAndHashCodeToStringHandler.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private String buildAttributeNameString(boolean doNotUseGetters, @NotNull PsiField classField, @NotNull PsiClass psiClass) {
  final String fieldName = classField.getName();
  if (doNotUseGetters) {
    return fieldName;
  } else {
    final String getterName = LombokUtils.getGetterName(classField);

    final boolean hasGetter;
    @SuppressWarnings("unchecked") final boolean annotatedWith = PsiAnnotationSearchUtil.isAnnotatedWith(psiClass, Data.class, Value.class, Getter.class);
    if (annotatedWith) {
      final PsiAnnotation getterLombokAnnotation = PsiAnnotationSearchUtil.findAnnotation(psiClass, Getter.class);
      hasGetter = null == getterLombokAnnotation || null != LombokProcessorUtil.getMethodModifier(getterLombokAnnotation);
    } else {
      hasGetter = PsiMethodUtil.hasMethodByName(PsiClassUtil.collectClassMethodsIntern(psiClass), getterName);
    }

    return hasGetter ? getterName + "()" : fieldName;
  }
}
 
Example 2
Source File: LombokUtils.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static String getGetterName(final @NotNull PsiField psiField) {
  final AccessorsInfo accessorsInfo = AccessorsInfo.build(psiField);

  final String psiFieldName = psiField.getName();
  final boolean isBoolean = PsiType.BOOLEAN.equals(psiField.getType());

  return LombokUtils.toGetterName(accessorsInfo, psiFieldName, isBoolean);
}
 
Example 3
Source File: QuarkusConfigPropertiesProvider.java    From intellij-quarkus with Eclipse Public License 2.0 4 votes vote down vote up
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);
			}
		}
	}
}
 
Example 4
Source File: QuarkusConfigRootProvider.java    From intellij-quarkus with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * Process Quarkus ConfigGroup annotation from the given
 * <code>psiElement</code>.
 * 
 * @param extensionName the Quarkus extension name
 * 
 * @param psiElement   the class, field element which have a Quarkus
 *                      ConfigGroup annotations.
 * @param baseKey       the base key
 * @param configPhase   the phase
 * @param javadocCache  the Javadoc cache
 * @param collector     the properties to fill.
 */
private void processConfigGroup(String extensionName, PsiModifierListOwner psiElement, String baseKey,
		ConfigPhase configPhase, Map<VirtualFile, Properties> javadocCache, IPropertiesCollector collector) {
	if (psiElement instanceof PsiClass) {
		PsiElement[] elements = psiElement.getChildren();
		for (PsiElement child : elements) {
			if (child instanceof PsiField) {
				PsiField field = (PsiField) child;
				if (!canProcess(field)) {
					continue;
				}
				final PsiAnnotation configItemAnnotation = AnnotationUtils.getAnnotation(field,
						QuarkusConstants.CONFIG_ITEM_ANNOTATION);
				String name = configItemAnnotation == null ? hyphenate(field.getName())
						: AnnotationUtils.getAnnotationMemberValue(configItemAnnotation, QuarkusConstants.CONFIG_ANNOTATION_NAME);
				if (name == null) {
					name = ConfigItem.HYPHENATED_ELEMENT_NAME;
				}
				String subKey;
				if (name.equals(ConfigItem.PARENT)) {
					subKey = baseKey;
				} else if (name.equals(ConfigItem.ELEMENT_NAME)) {
					subKey = baseKey + "." + field.getName();
				} else if (name.equals(ConfigItem.HYPHENATED_ELEMENT_NAME)) {
					subKey = baseKey + "." + hyphenate(field.getName());
				} else {
					subKey = baseKey + "." + name;
				}
				final String defaultValue = configItemAnnotation == null ? ConfigItem.NO_DEFAULT
						: AnnotationUtils.getAnnotationMemberValue(configItemAnnotation,
								QuarkusConstants.CONFIG_ITEM_ANNOTATION_DEFAULT_VALUE);

				String fieldTypeName = PsiTypeUtils.getResolvedTypeName(field);
				PsiClass fieldClass = PsiTypeUtils.findType(field.getManager(), fieldTypeName);
				final PsiAnnotation configGroupAnnotation = AnnotationUtils.getAnnotation(fieldClass,
						QuarkusConstants.CONFIG_GROUP_ANNOTATION);
				if (configGroupAnnotation != null) {
					processConfigGroup(extensionName, fieldClass, subKey, configPhase, javadocCache, collector);
				} else {
					addItemMetadata(extensionName, field, fieldTypeName, fieldClass, subKey, defaultValue,
							javadocCache, configPhase, collector);
				}
			}
		}
	}
}
 
Example 5
Source File: JavaParamsObjectBuilder.java    From OkHttpParamsGet with Apache License 2.0 4 votes vote down vote up
@Override
protected String toString(PsiField field) {
    return field.getName();
}
 
Example 6
Source File: KotlinParamsObjectBuilder.java    From OkHttpParamsGet with Apache License 2.0 4 votes vote down vote up
@Override
protected String toString(PsiField field, boolean nullable, String prefix) {
    return prefix != null ? prefix : field.getName();
}
 
Example 7
Source File: FieldFQN.java    From intellij-reference-diagram with Apache License 2.0 4 votes vote down vote up
public static FieldFQN create(PsiField psiField) {
    String qualifiedClassName = psiField.getContainingClass().getQualifiedName();
    return new FieldFQN(qualifiedClassName, psiField.getName());
}
 
Example 8
Source File: PsiUtils.java    From intellij-reference-diagram with Apache License 2.0 4 votes vote down vote up
public static String getPresentableName(PsiElement psiElement) {
    PsiElementDispatcher<String> psiElementDispatcher = new PsiElementDispatcher<String>() {

        @Override
        public String processClass(PsiClass psiClass) {
            return psiClass.getName();
        }

        @Override
        public String processMethod(PsiMethod psiMethod) {
            List<String> parameterArray = MethodFQN.getParameterArray(psiMethod);
            String parameterRepresentation = MethodFQN.createParameterRepresentation(parameterArray);
            return psiMethod.getName() + "(" + parameterRepresentation + ")";
        }

        @Override
        public String processField(PsiField psiField) {
            return psiField.getName();
        }

        @Override
        public String processClassInitializer(PsiClassInitializer psiClassInitializer) {
            return getName(psiClassInitializer);
        }

        @Override
        public String processInnerClass(PsiClass innerClass) {
            return innerClass.getName();
        }

        @Override
        public String processStaticInnerClass(PsiClass staticInnerClass) {
            return staticInnerClass.getName();
        }

        @Override
        public String processEnum(PsiClass anEnum) {
            return anEnum.getName();
        }

        @Override
        public String processPackage(PsiJavaDirectoryImpl aPackage) {
            return aPackage.getName();
        }

        @Override
        public String processFile(PsiJavaFile aFile) {
            return aFile.getName();
        }
    };

    return psiElementDispatcher.dispatch(psiElement);
}
 
Example 9
Source File: SetterFieldProcessor.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@NotNull
public PsiMethod createSetterMethod(@NotNull PsiField psiField, @NotNull PsiClass psiClass, @NotNull String methodModifier) {
  final String fieldName = psiField.getName();
  final PsiType psiFieldType = psiField.getType();
  final PsiAnnotation setterAnnotation = PsiAnnotationSearchUtil.findAnnotation(psiField, Setter.class);

  final String methodName = getSetterName(psiField, PsiType.BOOLEAN.equals(psiFieldType));

  PsiType returnType = getReturnType(psiField);
  LombokLightMethodBuilder methodBuilder = new LombokLightMethodBuilder(psiField.getManager(), methodName)
    .withMethodReturnType(returnType)
    .withContainingClass(psiClass)
    .withParameter(fieldName, psiFieldType)
    .withNavigationElement(psiField);
  if (StringUtil.isNotEmpty(methodModifier)) {
    methodBuilder.withModifier(methodModifier);
  }
  boolean isStatic = psiField.hasModifierProperty(PsiModifier.STATIC);
  if (isStatic) {
    methodBuilder.withModifier(PsiModifier.STATIC);
  }

  PsiParameter methodParameter = methodBuilder.getParameterList().getParameters()[0];
  PsiModifierList methodParameterModifierList = methodParameter.getModifierList();
  if (null != methodParameterModifierList) {
    final Collection<String> annotationsToCopy = PsiAnnotationUtil.collectAnnotationsToCopy(psiField,
      LombokUtils.NON_NULL_PATTERN, LombokUtils.NULLABLE_PATTERN);
    for (String annotationFQN : annotationsToCopy) {
      methodParameterModifierList.addAnnotation(annotationFQN);
    }
    addOnXAnnotations(setterAnnotation, methodParameterModifierList, "onParam");
  }

  final String codeBlockText = createCodeBlockText(psiField, psiClass, returnType, isStatic, methodParameter);
  methodBuilder.withBody(PsiMethodUtil.createCodeBlockFromText(codeBlockText, methodBuilder));

  PsiModifierList methodModifierList = methodBuilder.getModifierList();
  copyAnnotations(psiField, methodModifierList, LombokUtils.DEPRECATED_PATTERN);
  addOnXAnnotations(setterAnnotation, methodModifierList, "onMethod");

  return methodBuilder;
}
 
Example 10
Source File: EqualsAndHashCodeToStringHandler.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
MemberInfo(PsiField psiField) {
  this(psiField, psiField.getName(), false);
}