/** * Implementation of dynamicProxy.toString() */ private String toStringImpl() { StringBuilder result = new StringBuilder(128); result.append('@'); result.append(type.getName()); result.append('('); boolean firstMember = true; PsiNameValuePair[] attributes = myAnnotation.getParameterList().getAttributes(); for (PsiNameValuePair e : attributes) { if (firstMember) { firstMember = false; } else { result.append(", "); } result.append(e.getName()); result.append('='); PsiAnnotationMemberValue value = e.getValue(); result.append(value == null ? "null" : value.getText()); } result.append(')'); return result.toString(); }
@Override public boolean checkArgumentList(@NotNull AnnotationHolder holder, @NotNull GrAnnotation annotation) { if (!GroovyCommonClassNames.GROOVY_LANG_DELEGATES_TO.equals(annotation.getQualifiedName())) return false; final PsiAnnotationMemberValue valueAttribute = annotation.findAttributeValue("value"); if (valueAttribute == null) { final PsiAnnotationOwner owner = annotation.getOwner(); if (owner instanceof GrModifierList) { final PsiElement parent1 = ((GrModifierList)owner).getParent(); if (parent1 instanceof GrParameter) { final PsiElement parent = parent1.getParent(); if (parent instanceof GrParameterList) { for (GrParameter parameter : ((GrParameterList)parent).getParameters()) { if (parameter.getModifierList().findAnnotation(GroovyCommonClassNames.GROOVY_LANG_DELEGATES_TO_TARGET) != null) { return true; } } } } } } return false; }
private void addOwnerDomainAttribute( @NotNull final Project project, final PsiAnnotation annotation) { new WriteCommandAction(project, annotation.getContainingFile()) { @Override protected void run(final Result result) throws Throwable { // @A(ownerDomain = "your-company.com") PsiAnnotationMemberValue newMemberValue = JavaPsiFacade.getInstance(project) .getElementFactory() .createAnnotationFromText( "@A(" + API_NAMESPACE_DOMAIN_ATTRIBUTE + " = \"" + SUGGESTED_DOMAIN_ATTRIBUTE + "\")", null) .findDeclaredAttributeValue(API_NAMESPACE_DOMAIN_ATTRIBUTE); annotation.setDeclaredAttributeValue(API_NAMESPACE_DOMAIN_ATTRIBUTE, newMemberValue); } }.execute(); }
private void addOwnerNameAttribute( @NotNull final Project project, final PsiAnnotation annotation) { new WriteCommandAction(project, annotation.getContainingFile()) { @Override protected void run(final Result result) throws Throwable { // @A(ownerName = "YourCo") PsiAnnotationMemberValue newMemberValue = JavaPsiFacade.getInstance(project) .getElementFactory() .createAnnotationFromText( "@A(" + API_NAMESPACE_NAME_ATTRIBUTE + " = \"" + SUGGESTED_OWNER_ATTRIBUTE + "\")", null) .findDeclaredAttributeValue(API_NAMESPACE_NAME_ATTRIBUTE); annotation.setDeclaredAttributeValue(API_NAMESPACE_NAME_ATTRIBUTE, newMemberValue); } }.execute(); }
private String getAttributeFromAnnotation( PsiAnnotation annotation, String annotationType, final String attribute) throws InvalidAnnotationException, MissingAttributeException { String annotationQualifiedName = annotation.getQualifiedName(); if (annotationQualifiedName == null) { throw new InvalidAnnotationException(annotation, annotationType); } if (annotationQualifiedName.equals(annotationType)) { PsiAnnotationMemberValue annotationMemberValue = annotation.findAttributeValue(attribute); if (annotationMemberValue == null) { throw new MissingAttributeException(annotation, attribute); } String httpMethodWithQuotes = annotationMemberValue.getText(); return httpMethodWithQuotes.substring(1, httpMethodWithQuotes.length() - 1); } else { throw new InvalidAnnotationException(annotation, annotationType); } }
private void initializePsiMethod(String methodName, String httpMethodValue, String pathValue) { PsiAnnotationMemberValue mockAnnotationMemberValue1 = mock(PsiAnnotationMemberValue.class); when(mockAnnotationMemberValue1.getText()).thenReturn(httpMethodValue); PsiAnnotationMemberValue mockAnnotationMemberValue2 = mock(PsiAnnotationMemberValue.class); when(mockAnnotationMemberValue2.getText()).thenReturn(pathValue); PsiAnnotation mockAnnotation = mock(PsiAnnotation.class); when(mockAnnotation.getQualifiedName()) .thenReturn(GctConstants.APP_ENGINE_ANNOTATION_API_METHOD); when(mockAnnotation.findAttributeValue("httpMethod")).thenReturn(mockAnnotationMemberValue1); when(mockAnnotation.findAttributeValue("path")).thenReturn(mockAnnotationMemberValue2); PsiAnnotation[] mockAnnotationsArray = {mockAnnotation}; PsiModifierList mockModifierList = mock(PsiModifierList.class); when(mockModifierList.getAnnotations()).thenReturn(mockAnnotationsArray); mockPsiMethod = mock(PsiMethod.class); when(mockPsiMethod.getModifierList()).thenReturn(mockModifierList); when(mockPsiMethod.getName()).thenReturn(methodName); when(mockPsiMethod.getContainingClass()).thenReturn(mockPsiClass); PsiParameterList mockParameterList = mock(PsiParameterList.class); when(mockParameterList.getParameters()).thenReturn(new PsiParameter[0]); when(mockPsiMethod.getParameterList()).thenReturn(mockParameterList); }
private void initializePsiClass(String apiResource, String apiClassResource) { PsiAnnotationMemberValue mockAnnotationMemberValue1 = mock(PsiAnnotationMemberValue.class); when(mockAnnotationMemberValue1.getText()).thenReturn(apiResource); PsiAnnotationMemberValue mockAnnotationMemberValue2 = mock(PsiAnnotationMemberValue.class); when(mockAnnotationMemberValue2.getText()).thenReturn(apiClassResource); // Mock @Api(resource = "") PsiAnnotation mockAnnotation1 = mock(PsiAnnotation.class); when(mockAnnotation1.getQualifiedName()).thenReturn(GctConstants.APP_ENGINE_ANNOTATION_API); when(mockAnnotation1.findAttributeValue("resource")).thenReturn(mockAnnotationMemberValue1); // Mock @ApiClass(resource = "") PsiAnnotation mockAnnotation2 = mock(PsiAnnotation.class); when(mockAnnotation2.getQualifiedName()) .thenReturn(GctConstants.APP_ENGINE_ANNOTATION_API_CLASS); when(mockAnnotation2.findAttributeValue("resource")).thenReturn(mockAnnotationMemberValue2); PsiAnnotation[] mockAnnotationsArray = {mockAnnotation1, mockAnnotation2}; PsiModifierList mockModifierList = mock(PsiModifierList.class); when(mockModifierList.getAnnotations()).thenReturn(mockAnnotationsArray); mockPsiClass = mock(PsiClass.class); when(mockPsiClass.getModifierList()).thenReturn(mockModifierList); }
public static String getAttributeValue(PsiAnnotation annotation, String attributeName, DefaultPolicy policy) { final PsiAnnotationMemberValue value = getAnnotationMemberValue(annotation, attributeName); if (value != null) { if (value instanceof PsiClassObjectAccessExpression) { final PsiType type = ((PsiClassObjectAccessExpression) value).getType(); if (type == null) { return null; } return type.getCanonicalText(); } else { final String text = value.getText(); return text.substring(1, text.length() - 1); } } if (policy == DefaultPolicy.OWNER_IDENTIFIER_NAME) { return PsiUtil.getName(getImmediateOwnerElement(annotation)); } else { return null; } }
@Override public boolean shouldShow(UsageTarget target, Usage usage) { PsiElement element = ((UsageInfo2UsageAdapter) usage).getElement(); PsiMethod psimethod = PsiConsultantImpl.findMethod(element); PsiAnnotationMemberValue attribValue = PsiConsultantImpl .findTypeAttributeOfProvidesAnnotation(psimethod); // Is it a @Provides method? return psimethod != null // Ensure it has an @Provides. && PsiConsultantImpl.hasAnnotation(psimethod, CLASS_PROVIDES) // Check for Qualifier annotations. && PsiConsultantImpl.hasQuailifierAnnotations(psimethod, qualifierAnnotations) // Right return type. && PsiConsultantImpl.getReturnClassFromMethod(psimethod, false) .getName() .equals(target.getName()) // Right type parameters. && PsiConsultantImpl.hasTypeParameters(psimethod, typeParameters) // @Provides(type=SET) && attribValue != null && attribValue.textMatches(SET_TYPE); }
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; }
@NotNull private static Nullness extractNullityFromWhenValue(PsiAnnotation nonNull) { PsiAnnotationMemberValue when = nonNull.findAttributeValue("when"); if(when instanceof PsiReferenceExpression) { String refName = ((PsiReferenceExpression) when).getReferenceName(); if("ALWAYS".equals(refName)) { return Nullness.NOT_NULL; } if("MAYBE".equals(refName) || "NEVER".equals(refName)) { return Nullness.NULLABLE; } } return Nullness.UNKNOWN; }
private static PsiElement findTargetFeature( PsiAnnotation psiAnnotation, ManifoldPsiClass facade ) { PsiAnnotationMemberValue value = psiAnnotation.findAttributeValue( SourcePosition.FEATURE ); String featureName = StringUtil.unquoteString( value.getText() ); // value = psiAnnotation.findAttributeValue( SourcePosition.TYPE ); // if( value != null ) // { // String ownersType = StringUtil.unquoteString( value.getText() ); // if( ownersType != null ) // { // PsiElement target = findIndirectTarget( ownersType, featureName, facade.getRawFile().getProject() ); // if( target != null ) // { // return target; // } // } // } int iOffset = Integer.parseInt( psiAnnotation.findAttributeValue( SourcePosition.OFFSET ).getText() ); int iLength = Integer.parseInt( psiAnnotation.findAttributeValue( SourcePosition.LENGTH ).getText() ); List<PsiFile> sourceFiles = facade.getRawFiles(); if( iOffset >= 0 ) { //PsiElement target = sourceFile.findElementAt( iOffset ); //## todo: handle multiple files return new FakeTargetElement( sourceFiles.get( 0 ), iOffset, iLength >= 0 ? iLength : 1, featureName ); } return facade; }
private RemoveAnnotationValueFix( @NotNull PsiAnnotationMemberValue annotationValueToRemove, @NotNull PsiJavaCodeReferenceElement sideEffectClassReference ) { super( message( "side.effects.annotation.declared.correctly.fix.remove.class.reference", sideEffectClassReference.getQualifiedName() ) ); this.annotationValueToRemove = annotationValueToRemove; }
public RemoveInvalidConcernClassReferenceFix( @NotNull PsiAnnotationMemberValue annotationValueToRemove, @NotNull PsiJavaCodeReferenceElement concernClassReference ) { super( message( "concerns.annotation.declared.correctly.fix.remove.concern.class.reference", concernClassReference.getQualifiedName() ) ); this.concernClassAnnotationValue = annotationValueToRemove; }
private ProblemDescriptor createProblemDescriptor( @NotNull InspectionManager manager, @NotNull PsiAnnotationMemberValue mixinAnnotationValue, @NotNull PsiJavaCodeReferenceElement mixinClassReference, @NotNull String message ) { RemoveInvalidMixinClassReferenceFix fix = new RemoveInvalidMixinClassReferenceFix( mixinAnnotationValue, mixinClassReference ); return manager.createProblemDescriptor( mixinAnnotationValue, message, fix, GENERIC_ERROR_OR_WARNING ); }
@NotNull @Override public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) { return new EndpointPsiElementVisitor() { @Override public void visitAnnotation(PsiAnnotation annotation) { if (!EndpointUtilities.isEndpointClass(annotation)) { return; } if (!GctConstants.APP_ENGINE_ANNOTATION_API_METHOD.equals(annotation.getQualifiedName())) { return; } PsiAnnotationMemberValue memberValue = annotation.findAttributeValue(API_NAME_ATTRIBUTE); if (memberValue == null) { return; } String nameValueWithQuotes = memberValue.getText(); String nameValue = EndpointUtilities.removeBeginningAndEndingQuotes(nameValueWithQuotes); if (nameValue.isEmpty()) { return; } if (!API_NAME_PATTERN .matcher(EndpointUtilities.collapseSequenceOfDots(nameValue)) .matches()) { holder.registerProblem( memberValue, "Invalid method name: letters, digits, underscores and dots are acceptable " + "characters. Leading and trailing dots are prohibited.", new MyQuickFix()); } } }; }
/** * Returns true if the class containing <code>psiElement</code> has a transformer specified by * using the @ApiTransformer annotation on a class or by using the transformer attribute of the * * @return True if the class containing <code>psiElement</code> has a transformer and false * otherwise. @Api annotation. Returns false otherwise. */ public boolean hasTransformer(PsiElement psiElement) { PsiClass psiClass = PsiUtils.findClass(psiElement); if (psiClass == null) { return false; } PsiModifierList modifierList = psiClass.getModifierList(); if (modifierList == null) { return false; } // Check if class has @ApiTransformer to specify a transformer PsiAnnotation apiTransformerAnnotation = modifierList.findAnnotation(GctConstants.APP_ENGINE_ANNOTATION_API_TRANSFORMER); if (apiTransformerAnnotation != null) { return true; } // Check if class utilizes the transformer attribute of the @Api annotation // to specify its transformer PsiAnnotation apiAnnotation = modifierList.findAnnotation(GctConstants.APP_ENGINE_ANNOTATION_API); if (apiAnnotation != null) { PsiAnnotationMemberValue transformerMember = apiAnnotation.findAttributeValue(API_TRANSFORMER_ATTRIBUTE); if (transformerMember != null && !transformerMember.getText().equals("{}")) { return true; } } return false; }
/** * Returns the value for @Named if it exists for <code>psiParameter</code> or null if it does not * exist. * * @param psiParameter The parameter whose @Named value is to be returned. * @return The @Named value if it exists for <code>psiParameter</code> or null if it does not * exist. */ @Nullable public PsiAnnotationMemberValue getNamedAnnotationValue(PsiParameter psiParameter) { PsiModifierList modifierList = psiParameter.getModifierList(); if (modifierList == null) { return null; } PsiAnnotation annotation = modifierList.findAnnotation("javax.inject.Named"); if (annotation == null) { annotation = modifierList.findAnnotation(GctConstants.APP_ENGINE_ANNOTATION_NAMED); if (annotation == null) { return null; } } PsiNameValuePair[] nameValuePairs = annotation.getParameterList().getAttributes(); if (nameValuePairs.length != 1) { return null; } if (nameValuePairs[0] == null) { return null; } return nameValuePairs[0].getValue(); }
private void addOwnerDomainAndNameAttributes( @NotNull final Project project, final PsiAnnotation annotation) { new WriteCommandAction(project, annotation.getContainingFile()) { @Override protected void run(final Result result) throws Throwable { // @A(ownerName = "YourCo", ownerDomain = "your-company.com") String annotationString = "@A(" + API_NAMESPACE_NAME_ATTRIBUTE + " = \"" + SUGGESTED_OWNER_ATTRIBUTE + "\", " + API_NAMESPACE_DOMAIN_ATTRIBUTE + " = \"" + "your-company.com" + "\")"; PsiAnnotation newAnnotation = JavaPsiFacade.getInstance(project) .getElementFactory() .createAnnotationFromText(annotationString, null); PsiAnnotationMemberValue newDomainMemberValue = newAnnotation.findDeclaredAttributeValue(API_NAMESPACE_DOMAIN_ATTRIBUTE); PsiAnnotationMemberValue newNameMemberValue = newAnnotation.findDeclaredAttributeValue(API_NAMESPACE_NAME_ATTRIBUTE); annotation.setDeclaredAttributeValue(API_NAMESPACE_NAME_ATTRIBUTE, newNameMemberValue); annotation.setDeclaredAttributeValue( API_NAMESPACE_DOMAIN_ATTRIBUTE, newDomainMemberValue); } }.execute(); }
/** * Returns "/{}" for every parameter with a valid @Named annotation in {@code method} that does * not have @Nullable/@Default. */ private String getPathParameter(PsiMethod method) { StringBuilder path = new StringBuilder(); EndpointPsiElementVisitor elementVisitor = new EndpointPsiElementVisitor(); List<String> annotions = Arrays.asList( GctConstants.APP_ENGINE_ANNOTATION_NULLABLE, "javax.annotation.Nullable", GctConstants.APP_ENGINE_ANNOTATION_DEFAULT_VALUE); for (PsiParameter param : method.getParameterList().getParameters()) { // Check for @Nullable/@Default PsiModifierList modifierList = param.getModifierList(); if (modifierList == null) { continue; } if (AnnotationUtil.isAnnotated(param, annotions)) { continue; } PsiAnnotationMemberValue namedValue = elementVisitor.getNamedAnnotationValue(param); if (namedValue != null) { path.append("/{}"); } } return path.toString(); }
@Override public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) { PsiElement element = descriptor.getPsiElement(); if (!(element instanceof PsiAnnotation)) { return; } PsiAnnotation annotation = (PsiAnnotation) element; if (!GctConstants.APP_ENGINE_ANNOTATION_API_METHOD.equals(annotation.getQualifiedName())) { return; } PsiAnnotationMemberValue apiMethodNameAttribute = annotation.findAttributeValue(API_METHOD_NAME_ATTRIBUTE); if (apiMethodNameAttribute == null) { return; } // Get name String nameValueWithQuotes = apiMethodNameAttribute.getText(); String nameValue = EndpointUtilities.removeBeginningAndEndingQuotes(nameValueWithQuotes); // @A(name = "someName") PsiAnnotationMemberValue newMemberValue = JavaPsiFacade.getInstance(project) .getElementFactory() .createAnnotationFromText( "@A(" + API_METHOD_NAME_ATTRIBUTE + " = \"" + nameValue + "_1\")", null) .findDeclaredAttributeValue(API_METHOD_NAME_ATTRIBUTE); apiMethodNameAttribute.replace(newMemberValue); }
@Nullable public static String inferStringAttribute(@NotNull PsiAnnotation annotation, @NotNull String attributeName) { final PsiAnnotationMemberValue targetValue = annotation.findAttributeValue(attributeName); if (targetValue instanceof PsiLiteral) { final Object value = ((PsiLiteral)targetValue).getValue(); if (value instanceof String) return (String)value; } return null; }
@Nullable public static Integer inferIntegerAttribute(@NotNull PsiAnnotation annotation, @NotNull String attributeName) { final PsiAnnotationMemberValue targetValue = annotation.findAttributeValue(attributeName); if (targetValue instanceof PsiLiteral) { final Object value = ((PsiLiteral)targetValue).getValue(); if (value instanceof Integer) return (Integer)value; } return null; }
@Nullable public static Boolean getDeclaredBooleanAnnotationValue(@NotNull PsiAnnotation psiAnnotation, @NotNull String parameter) { PsiAnnotationMemberValue attributeValue = psiAnnotation.findDeclaredAttributeValue(parameter); Object constValue = null; if (null != attributeValue) { final JavaPsiFacade javaPsiFacade = JavaPsiFacade.getInstance(psiAnnotation.getProject()); constValue = javaPsiFacade.getConstantEvaluationHelper().computeConstantExpression(attributeValue); } return constValue instanceof Boolean ? (Boolean) constValue : null; }
@NotNull public static Collection<String> getOnX(@NotNull PsiAnnotation psiAnnotation, @NotNull String parameterName) { PsiAnnotationMemberValue onXValue = psiAnnotation.findAttributeValue(parameterName); if (!(onXValue instanceof PsiAnnotation)) { return Collections.emptyList(); } Collection<PsiAnnotation> annotations = PsiAnnotationUtil.getAnnotationValues((PsiAnnotation) onXValue, "value", PsiAnnotation.class); Collection<String> annotationStrings = new ArrayList<String>(); for (PsiAnnotation annotation : annotations) { PsiAnnotationParameterList params = annotation.getParameterList(); annotationStrings.add(PsiAnnotationSearchUtil.getSimpleNameOf(annotation) + params.getText()); } return annotationStrings; }
public static PsiAnnotationMemberValue getAnnotationMemberValue(PsiAnnotation annotation, String attributeName) { final PsiNameValuePair[] attributes = annotation.getParameterList().getAttributes(); for (PsiNameValuePair attribute : attributes) { if (attributeName.equals(attribute.getName())) { final PsiAnnotationMemberValue value = attribute.getValue(); if (value != null) { return value; } break; } } return null; }
public static PsiAnnotationMemberValue findTypeAttributeOfProvidesAnnotation( PsiElement element ) { PsiAnnotation annotation = findAnnotation(element, CLASS_PROVIDES); if (annotation != null) { return annotation.findAttributeValue(ATTRIBUTE_TYPE); } return null; }
/** * Return the appropriate return class for a given method element. * * @param psiMethod the method to get the return class from. * @param expandType set this to true if return types annotated with @Provides(type=?) * should be expanded to the appropriate collection type. * @return the appropriate return class for the provided method element. */ public static PsiClass getReturnClassFromMethod(PsiMethod psiMethod, boolean expandType) { if (psiMethod.isConstructor()) { return psiMethod.getContainingClass(); } PsiClassType returnType = ((PsiClassType) psiMethod.getReturnType()); if (returnType != null) { // Check if has @Provides annotation and specified type if (expandType) { PsiAnnotationMemberValue attribValue = findTypeAttributeOfProvidesAnnotation(psiMethod); if (attribValue != null) { if (attribValue.textMatches(SET_TYPE)) { String typeName = "java.util.Set<" + returnType.getCanonicalText() + ">"; returnType = ((PsiClassType) PsiElementFactory.SERVICE.getInstance(psiMethod.getProject()) .createTypeFromText(typeName, psiMethod)); } else if (attribValue.textMatches(MAP_TYPE)) { // TODO(radford): Supporting map will require fetching the key type and also validating // the qualifier for the provided key. // // String typeName = "java.util.Map<String, " + returnType.getCanonicalText() + ">"; // returnType = ((PsiClassType) PsiElementFactory.SERVICE.getInstance(psiMethod.getProject()) // .createTypeFromText(typeName, psiMethod)); } } } return returnType.resolve(); } return null; }
public static List<PsiType> getTypeParameters(PsiElement psiElement) { PsiClassType psiClassType = getPsiClassType(psiElement); if (psiClassType == null) { return new ArrayList<PsiType>(); } // Check if @Provides(type=?) pattern (annotation with specified type). PsiAnnotationMemberValue attribValue = findTypeAttributeOfProvidesAnnotation(psiElement); if (attribValue != null) { if (attribValue.textMatches(SET_TYPE)) { // type = SET. Transform the type parameter to the element type. ArrayList<PsiType> result = new ArrayList<PsiType>(); result.add(psiClassType); return result; } else if (attribValue.textMatches(MAP_TYPE)) { // TODO(radford): Need to figure out key type for maps. // type = SET or type = MAP. Transform the type parameter to the element type. //ArrayList<PsiType> result = new ArrayList<PsiType>(); //result.add(psiKeyType): //result.add(psiClassType); //return result; } } if (PsiConsultantImpl.isLazyOrProvider(getClass(psiClassType))) { psiClassType = extractFirstTypeParameter(psiClassType); } Collection<PsiType> typeParameters = psiClassType.resolveGenerics().getSubstitutor().getSubstitutionMap().values(); return new ArrayList<PsiType>(typeParameters); }
@Nullable public PsiAnnotationMemberValue getPsiElement() { if (myExactValue == null) { assert myAttributeLink != null; return myAttributeLink.findLinkedChild(myParent.getPsiElement()); } return myExactValue; }
public String getStringValue() { final PsiClass value = getValue(); if (value != null) return value.getQualifiedName(); final PsiAnnotationMemberValue psi = getPsiElement(); if (psi == null) return null; final String text = psi.getText(); if (text != null && text.endsWith(".class")) return text.substring(0, text.length() - ".class".length()); return null; }
@Nullable private T getJam(PsiAnnotationMemberValue element) { if (element instanceof PsiAnnotation) { return JamService.getJamService(element.getProject()).getJamElement(myJamKey, element); } return null; }
/** * Implementation of dynamicProxy.toString() */ private String toStringImpl() { StringBuilder result = new StringBuilder(128); result.append('@'); result.append(type.getName()); result.append('('); boolean firstMember = true; PsiNameValuePair[] attributes = myAnnotation.getParameterList().getAttributes(); for(PsiNameValuePair e : attributes) { if(firstMember) { firstMember = false; } else { result.append(", "); } result.append(e.getName()); result.append('='); PsiAnnotationMemberValue value = e.getValue(); result.append(value == null ? "null" : value.getText()); } result.append(')'); return result.toString(); }
/** make sure the value is boolean. */ public static boolean getBooleanValue(PsiAnnotationMemberValue value){ return Boolean.valueOf(value.getText()); }
public RemoveInvalidMixinClassReferenceFix( @NotNull PsiAnnotationMemberValue mixinClassAnnotationValue, @NotNull PsiJavaCodeReferenceElement mixinClassReference ) { super( message( "mixin.implements.mixin.type.fix.remove.class.reference", mixinClassReference.getQualifiedName() ) ); this.mixinClassAnnotationValue = mixinClassAnnotationValue; }
public RemoveInvalidMixinClassReferenceFix( @NotNull PsiAnnotationMemberValue mixinsAnnotation ) { super( message( "mixins.annotation.declared.on.mixin.type.fix.remove.mixins.annotation" ) ); this.mixinsAnnotation = mixinsAnnotation; }
/** Adds "_1" to the query name in an @Named annotation in <code>descriptor</code>. */ @Override public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) { PsiElement element = descriptor.getPsiElement(); if (element == null) { return; } if (!(element instanceof PsiAnnotation)) { return; } PsiAnnotation annotation = (PsiAnnotation) element; if ((!"javax.inject.Named".equals(annotation.getQualifiedName())) && (!GctConstants.APP_ENGINE_ANNOTATION_NAMED.equals(annotation.getQualifiedName()))) { return; } // Get @Named value PsiNameValuePair[] nameValuePairs = annotation.getParameterList().getAttributes(); if (nameValuePairs.length == 0) { return; } PsiAnnotationMemberValue memberValue = nameValuePairs[0].getValue(); if (memberValue == null) { return; } // Create new annotation with value equal to @Named's value plus "_1" String newNamedValue = "@Named(\"" + EndpointUtilities.removeBeginningAndEndingQuotes(memberValue.getText()) + "_1\")"; PsiAnnotation newAnnotation = JavaPsiFacade.getInstance(project) .getElementFactory() .createAnnotationFromText(newNamedValue, null); assert (newAnnotation.getParameterList().getAttributes().length == 1); // Update value of @Named memberValue.replace(newAnnotation.getParameterList().getAttributes()[0].getValue()); }
@NotNull @Override public PsiElementVisitor buildVisitor( @NotNull final com.intellij.codeInspection.ProblemsHolder holder, boolean isOnTheFly) { return new EndpointPsiElementVisitor() { /** * Flags @ApiNamespace that have one or more attributes specified where both the OwnerName and * OwnerDomain attributes are not specified. */ @Override public void visitAnnotation(PsiAnnotation annotation) { if (!EndpointUtilities.isEndpointClass(annotation)) { return; } if (!GctConstants.APP_ENGINE_ANNOTATION_API_NAMESPACE.equals( annotation.getQualifiedName())) { return; } PsiAnnotationMemberValue ownerDomainMember = annotation.findAttributeValue(API_NAMESPACE_DOMAIN_ATTRIBUTE); if (ownerDomainMember == null) { return; } String ownerDomainWithQuotes = ownerDomainMember.getText(); PsiAnnotationMemberValue ownerNameMember = annotation.findAttributeValue(API_NAMESPACE_NAME_ATTRIBUTE); if (ownerNameMember == null) { return; } String ownerNameWithQuotes = ownerNameMember.getText(); String ownerDomain = EndpointUtilities.removeBeginningAndEndingQuotes(ownerDomainWithQuotes); String ownerName = EndpointUtilities.removeBeginningAndEndingQuotes(ownerNameWithQuotes); // Package Path has a default value of "" String packagePath = EndpointUtilities.removeBeginningAndEndingQuotes( annotation.findAttributeValue(API_NAMESPACE_PACKAGE_PATH_ATTRIBUTE).getText()); boolean allUnspecified = ownerDomain.isEmpty() && ownerName.isEmpty() && packagePath.isEmpty(); boolean ownerFullySpecified = !ownerDomain.isEmpty() && !ownerName.isEmpty(); // Either everything must be fully unspecified or owner domain/name must both be specified. if (!allUnspecified && !ownerFullySpecified) { holder.registerProblem( annotation, "Invalid namespace configuration. If a namespace is set," + " make sure to set an Owner Domain and Name. Package Path is optional.", new MyQuickFix()); } } }; }
/** * Provides a default value for OwnerName and OwnerDomain attributes in @ApiNamespace when they * are not provided. */ @Override public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) { PsiElement psiElement = descriptor.getPsiElement(); if (psiElement == null) { return; } if (!(psiElement instanceof PsiAnnotation)) { return; } PsiAnnotation annotation = (PsiAnnotation) psiElement; if (!GctConstants.APP_ENGINE_ANNOTATION_API_NAMESPACE.equals(annotation.getQualifiedName())) { return; } PsiAnnotationMemberValue ownerDomainMember = annotation.findAttributeValue(API_NAMESPACE_DOMAIN_ATTRIBUTE); if (ownerDomainMember == null) { return; } String ownerDomainWithQuotes = ownerDomainMember.getText(); PsiAnnotationMemberValue ownerNameMember = annotation.findAttributeValue(API_NAMESPACE_NAME_ATTRIBUTE); if (ownerNameMember == null) { return; } String ownerNameWithQuotes = ownerNameMember.getText(); String ownerDomain = EndpointUtilities.removeBeginningAndEndingQuotes(ownerDomainWithQuotes); String ownerName = EndpointUtilities.removeBeginningAndEndingQuotes(ownerNameWithQuotes); if (ownerDomain.isEmpty() && ownerName.isEmpty()) { addOwnerDomainAndNameAttributes(project, annotation); } else if (ownerDomain.isEmpty() && !ownerName.isEmpty()) { addOwnerDomainAttribute(project, annotation); } else if (!ownerDomain.isEmpty() && ownerName.isEmpty()) { addOwnerNameAttribute(project, annotation); } }