Java Code Examples for org.jboss.jandex.ClassInfo#field()

The following examples show how to use org.jboss.jandex.ClassInfo#field() . 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: MethodNameParser.java    From quarkus with Apache License 2.0 6 votes vote down vote up
/**
 * Looks for the field in either the class itself or in a superclass that is annotated with @MappedSuperClass
 */
private FieldInfo getAssociatedEntityClassField(String associatedEntityFieldName, ClassInfo associatedEntityClassInfo) {
    FieldInfo fieldInfo = associatedEntityClassInfo.field(associatedEntityFieldName);
    if (fieldInfo != null) {
        return fieldInfo;
    }
    if (DotNames.OBJECT.equals(associatedEntityClassInfo.superName())) {
        return null;
    }

    ClassInfo superClassInfo = indexView.getClassByName(associatedEntityClassInfo.superName());
    if (superClassInfo.classAnnotation(DotNames.JPA_MAPPED_SUPERCLASS) == null) {
        return null;
    }

    return getAssociatedEntityClassField(associatedEntityFieldName, superClassInfo);
}
 
Example 2
Source File: MethodNameParser.java    From quarkus with Apache License 2.0 5 votes vote down vote up
private boolean entityContainsField(String fieldName) {
    if (entityClass.field(fieldName) != null) {
        return true;
    }

    for (ClassInfo superClass : mappedSuperClassInfos) {
        FieldInfo fieldInfo = superClass.field(fieldName);
        if (fieldInfo != null) {
            return true;
        }
    }
    return false;
}
 
Example 3
Source File: MethodNameParser.java    From quarkus with Apache License 2.0 5 votes vote down vote up
private FieldInfo getField(String fieldName) {
    FieldInfo fieldInfo = entityClass.field(fieldName);
    if (fieldInfo == null) {
        for (ClassInfo superClass : mappedSuperClassInfos) {
            fieldInfo = superClass.field(fieldName);
            if (fieldInfo != null) {
                break;
            }
        }
    }
    return fieldInfo;
}
 
Example 4
Source File: StockMethodsAdder.java    From quarkus with Apache License 2.0 5 votes vote down vote up
private FieldInfo getIdField(AnnotationTarget idAnnotationTarget) {
    if (idAnnotationTarget instanceof FieldInfo) {
        return idAnnotationTarget.asField();
    }

    MethodInfo methodInfo = idAnnotationTarget.asMethod();
    String propertyName = JavaBeanUtil.getPropertyNameFromGetter(methodInfo.name());
    ClassInfo entityClass = methodInfo.declaringClass();
    FieldInfo field = entityClass.field(propertyName);
    if (field == null) {
        throw new IllegalArgumentException("Entity " + entityClass + " does not appear to have a field backing method"
                + methodInfo.name() + " which is annotated with @Id");
    }
    return field;
}
 
Example 5
Source File: StringPropertyAccessorData.java    From quarkus with Apache License 2.0 5 votes vote down vote up
/**
 * Called with data parsed from a Spring expression like #person.name that is places inside a Spring security annotation on
 * a method
 */
static StringPropertyAccessorData from(MethodInfo methodInfo, int matchingParameterIndex, String propertyName,
        IndexView index, String expression) {
    Type matchingParameterType = methodInfo.parameters().get(matchingParameterIndex);
    ClassInfo matchingParameterClassInfo = index.getClassByName(matchingParameterType.name());
    if (matchingParameterClassInfo == null) {
        throw new IllegalArgumentException(
                "Expression: '" + expression + "' in the @PreAuthorize annotation on method '" + methodInfo.name()
                        + "' of class '" + methodInfo.declaringClass() + "' references class "
                        + matchingParameterType.name() + " which could not be in Jandex");
    }
    FieldInfo matchingParameterFieldInfo = matchingParameterClassInfo.field(propertyName);
    if (matchingParameterFieldInfo == null) {
        throw new IllegalArgumentException(
                "Expression: '" + expression + "' in the @PreAuthorize annotation on method '" + methodInfo.name()
                        + "' of class '" + methodInfo.declaringClass() + "' references unknown property '"
                        + propertyName + "' of class " + matchingParameterClassInfo);
    }
    if (!DotNames.STRING.equals(matchingParameterFieldInfo.type().name())) {
        throw new IllegalArgumentException(
                "Expression: '" + expression + "' in the @PreAuthorize annotation on method '" + methodInfo.name()
                        + "' of class '" + methodInfo.declaringClass() + "' references property '"
                        + propertyName + "' which is not a string");
    }

    return new StringPropertyAccessorData(matchingParameterClassInfo, matchingParameterFieldInfo);
}
 
Example 6
Source File: HasRoleValueUtil.java    From quarkus with Apache License 2.0 5 votes vote down vote up
static HasRoleValueProducer getHasRoleValueProducer(String hasRoleValue, MethodInfo methodInfo, IndexView index,
        Map<String, DotName> springBeansNameToDotName,
        Map<String, ClassInfo> springBeansNameToClassInfo, Set<String> beansReferencedInPreAuthorized) {
    if (hasRoleValue.startsWith("'") && hasRoleValue.endsWith("'")) {
        return new StaticHasRoleValueProducer(hasRoleValue.replace("'", ""));
    } else if (hasRoleValue.startsWith("@")) {
        Matcher beanFieldMatcher = BEAN_FIELD_PATTERN.matcher(hasRoleValue);
        if (!beanFieldMatcher.find()) {
            throw SpringSecurityProcessorUtil.createGenericMalformedException(methodInfo, hasRoleValue);
        }

        String beanName = beanFieldMatcher.group(1);
        ClassInfo beanClassInfo = SpringSecurityProcessorUtil.getClassInfoFromBeanName(beanName, index,
                springBeansNameToDotName, springBeansNameToClassInfo, hasRoleValue, methodInfo);

        String fieldName = beanFieldMatcher.group(2);
        FieldInfo fieldInfo = beanClassInfo.field(fieldName);
        if ((fieldInfo == null) || !Modifier.isPublic(fieldInfo.flags())
                || !DotNames.STRING.equals(fieldInfo.type().name())) {
            throw new IllegalArgumentException("Bean named '" + beanName + "' found in expression '" + hasRoleValue
                    + "' in the @PreAuthorize annotation on method " + methodInfo.name() + " of class "
                    + methodInfo.declaringClass() + " does not have a public field named '" + fieldName
                    + "' of type String");
        }

        beansReferencedInPreAuthorized.add(fieldInfo.declaringClass().name().toString());

        return new FromBeanHasRoleValueProducer(beanName, fieldInfo);
    } else {
        throw SpringSecurityProcessorUtil.createGenericMalformedException(methodInfo, hasRoleValue);
    }
}
 
Example 7
Source File: JaxRsDataObjectScannerTestBase.java    From smallrye-open-api with Apache License 2.0 4 votes vote down vote up
public FieldInfo getFieldFromKlazz(String containerName, String fieldName) {
    ClassInfo container = index.getClassByName(DotName.createSimple(containerName));
    return container.field(fieldName);
}
 
Example 8
Source File: TypesTest.java    From quarkus with Apache License 2.0 4 votes vote down vote up
@Test
public void testGetTypeClosure() throws IOException {
    IndexView index = Basics.index(Foo.class, Baz.class, Producer.class, Object.class, List.class, Collection.class,
            Iterable.class);
    DotName bazName = DotName.createSimple(Baz.class.getName());
    DotName fooName = DotName.createSimple(Foo.class.getName());
    DotName producerName = DotName.createSimple(Producer.class.getName());
    ClassInfo fooClass = index.getClassByName(fooName);
    Map<ClassInfo, Map<TypeVariable, Type>> resolvedTypeVariables = new HashMap<>();
    BeanDeployment dummyDeployment = BeanProcessor.builder().setIndex(index).build().getBeanDeployment();

    // Baz, Foo<String>, Object
    Set<Type> bazTypes = Types.getTypeClosure(index.getClassByName(bazName), null,
            Collections.emptyMap(),
            dummyDeployment,
            resolvedTypeVariables::put);
    assertEquals(3, bazTypes.size());
    assertTrue(bazTypes.contains(Type.create(bazName, Kind.CLASS)));
    assertTrue(bazTypes.contains(ParameterizedType.create(fooName,
            new Type[] { Type.create(DotName.createSimple(String.class.getName()), Kind.CLASS) },
            null)));
    assertEquals(resolvedTypeVariables.size(), 1);
    assertTrue(resolvedTypeVariables.containsKey(fooClass));
    assertEquals(resolvedTypeVariables.get(fooClass).get(fooClass.typeParameters().get(0)),
            Type.create(DotName.createSimple(String.class.getName()), Kind.CLASS));

    resolvedTypeVariables.clear();
    // Foo<T>, Object
    Set<Type> fooTypes = Types.getClassBeanTypeClosure(fooClass,
            dummyDeployment);
    assertEquals(2, fooTypes.size());
    for (Type t : fooTypes) {
        if (t.kind().equals(Kind.PARAMETERIZED_TYPE)) {
            ParameterizedType fooType = t.asParameterizedType();
            assertEquals("T", fooType.arguments().get(0).asTypeVariable().identifier());
            assertEquals(DotNames.OBJECT, fooType.arguments().get(0).asTypeVariable().bounds().get(0).name());
        }
    }
    ClassInfo producerClass = index.getClassByName(producerName);
    String producersName = "produce";
    MethodInfo producerMethod = producerClass.method(producersName);
    // Object is the sole type
    Set<Type> producerMethodTypes = Types.getProducerMethodTypeClosure(producerMethod,
            dummyDeployment);
    assertEquals(1, producerMethodTypes.size());

    // Object is the sole type
    FieldInfo producerField = producerClass.field(producersName);
    Set<Type> producerFieldTypes = Types.getProducerFieldTypeClosure(producerField,
            dummyDeployment);
    assertEquals(1, producerFieldTypes.size());
}