List of usage examples for javax.lang.model.type TypeMirror getKind
TypeKind getKind();
From source file:Main.java
public static String defaultReturnLiteral(@NonNull TypeMirror returnType) { final TypeKind kind = returnType.getKind(); switch (kind) { case VOID:/*from w w w . j av a 2 s . co m*/ return null; case BOOLEAN: return "false"; case BYTE: case CHAR: case INT: return "0"; case LONG: return "0L"; case FLOAT: return "0f"; case DOUBLE: return "0d"; default: return "null"; } }
From source file:info.archinnov.achilles.internals.parser.UDFParser.java
public static List<UDFSignature> parseFunctionRegistryAndValidateTypes(AptUtils aptUtils, TypeElement elm) { final List<ExecutableElement> methods = ElementFilter.methodsIn(elm.getEnclosedElements()); final Optional<String> keyspace = AnnotationTree.findKeyspaceForFunctionRegistry(aptUtils, elm); final TypeName sourceClass = TypeName.get(aptUtils.erasure(elm)); //Not allow to declare function in system keyspaces if (keyspace.isPresent()) { final String keyspaceName = keyspace.get(); aptUtils.validateFalse(/* w w w. j a v a2s. c o m*/ FORBIDDEN_KEYSPACES.contains(keyspaceName) || FORBIDDEN_KEYSPACES.contains(keyspaceName.toLowerCase()), "The provided keyspace '%s' on function registry class '%s' is forbidden because it is a system keyspace", keyspaceName, sourceClass); } aptUtils.validateFalse(keyspace.isPresent() && isBlank(keyspace.get()), "The declared keyspace for function registry '%s' should not be blank", elm.getSimpleName().toString()); return methods.stream().map(method -> { final List<TypeName> parametersType = method.getParameters().stream().map(VariableElement::asType) .map(TypeName::get).collect(toList()); final String methodName = method.getSimpleName().toString(); final List<UDFParamSignature> parameterSignatures = method.getParameters().stream() .map(x -> new UDFParamSignature(TypeName.get(x.asType()).box(), x.getSimpleName().toString())) .collect(toList()); //Validate parameter types for (TypeName param : parametersType) { TypeValidator.validateNativeTypesForFunction(aptUtils, method, param, "argument"); } //Validate return type final TypeMirror returnTypeMirror = method.getReturnType(); aptUtils.validateFalse(returnTypeMirror.getKind() == TypeKind.VOID, "The return type for the method '%s' on class '%s' should not be VOID", method.toString(), elm.getSimpleName().toString()); final TypeName returnType = TypeName.get(returnTypeMirror).box(); TypeValidator.validateNativeTypesForFunction(aptUtils, method, returnType, "return type"); // Validate NOT system function comparing only name lowercase aptUtils.validateFalse(SYSTEM_FUNCTIONS_NAME.contains(methodName.toLowerCase()), "The name of the function '%s' in class '%s' is reserved for system functions", method, sourceClass); return new UDFSignature(keyspace, sourceClass, methodName, returnType, parametersType, parameterSignatures); }).collect(toList()); }
From source file:info.archinnov.achilles.internals.parser.FunctionParser.java
public static List<FunctionSignature> parseFunctionRegistryAndValidateTypes(AptUtils aptUtils, TypeElement elm, GlobalParsingContext context) {/*from w w w .j av a2s . c om*/ final List<ExecutableElement> methods = ElementFilter.methodsIn(elm.getEnclosedElements()); final Optional<String> keyspace = AnnotationTree.findKeyspaceForFunctionRegistry(aptUtils, elm); final FunctionParamParser paramParser = new FunctionParamParser(aptUtils); final TypeName parentType = TypeName.get(aptUtils.erasure(elm)); //Not allow to declare function in system keyspaces if (keyspace.isPresent()) { final String keyspaceName = keyspace.get(); aptUtils.validateFalse( FORBIDDEN_KEYSPACES.contains(keyspaceName) || FORBIDDEN_KEYSPACES.contains(keyspaceName.toLowerCase()), "The provided keyspace '%s' on function registry class '%s' is forbidden because it is a system keyspace", keyspaceName, parentType); } aptUtils.validateFalse(keyspace.isPresent() && isBlank(keyspace.get()), "The declared keyspace for function registry '%s' should not be blank", elm.getSimpleName().toString()); return methods.stream().map(method -> { final String methodName = method.getSimpleName().toString(); final List<AnnotationTree> annotationTrees = AnnotationTree.buildFromMethodForParam(aptUtils, method); final List<? extends VariableElement> parameters = method.getParameters(); final List<FunctionParamSignature> parameterSignatures = new ArrayList<>(parameters.size()); for (int i = 0; i < parameters.size(); i++) { final VariableElement parameter = parameters.get(i); context.nestedTypesStrategy.validate(aptUtils, annotationTrees.get(i), method.toString(), parentType); final FunctionParamSignature functionParamSignature = paramParser.parseParam(context, annotationTrees.get(i), parentType, methodName, parameter.getSimpleName().toString()); parameterSignatures.add(functionParamSignature); } //Validate return type final TypeMirror returnType = method.getReturnType(); aptUtils.validateFalse(returnType.getKind() == TypeKind.VOID, "The return type for the method '%s' on class '%s' should not be VOID", method.toString(), elm.getSimpleName().toString()); aptUtils.validateFalse(returnType.getKind().isPrimitive(), "Due to internal JDK API limitations, UDF/UDA return types cannot be primitive. " + "Use their Object counterpart instead for method '%s' " + "in function registry '%s'", method.toString(), elm.getQualifiedName()); final FunctionParamSignature returnTypeSignature = paramParser.parseParam(context, AnnotationTree.buildFromMethodForReturnType(aptUtils, method), parentType, methodName, "returnType"); // Validate NOT system function comparing only name lowercase aptUtils.validateFalse(SYSTEM_FUNCTIONS_NAME.contains(methodName.toLowerCase()), "The name of the function '%s' in class '%s' is reserved for system functions", method, parentType); return new FunctionSignature(keyspace, parentType, methodName, returnTypeSignature, parameterSignatures); }).collect(toList()); }
From source file:io.github.jeddict.jpa.modeler.properties.convert.ConvertPanel.java
static void importAttributeConverter(String classHandle, AtomicBoolean validated, ModelerFile modelerFile) { if (StringUtils.isBlank(classHandle)) { validated.set(true);//from w w w .j a v a 2 s . c o m return; } FileObject pkg = findSourceGroupForFile(modelerFile.getFileObject()).getRootFolder(); try { JavaSource javaSource = JavaSource.create(ClasspathInfo.create(pkg)); javaSource.runUserActionTask(controller -> { try { controller.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED); TypeElement jc = controller.getElements().getTypeElement(classHandle); EntityMappings entityMappings = (EntityMappings) modelerFile.getDefinitionElement(); Optional<Converter> converter = entityMappings.findConverter(classHandle); if (jc != null) { DeclaredType attributeConverterType = null; if (!jc.getInterfaces().isEmpty()) { //fetch interface info for (TypeMirror interfaceType : jc.getInterfaces()) { if (interfaceType.getKind() == TypeKind.DECLARED && AttributeConverter.class .getName().equals(((DeclaredType) interfaceType).asElement().toString())) { attributeConverterType = (DeclaredType) interfaceType; } } } if (attributeConverterType != null && attributeConverterType.getTypeArguments().size() == 2) { TypeMirror attributeType = attributeConverterType.getTypeArguments().get(0); TypeMirror dbFieldType = attributeConverterType.getTypeArguments().get(1); if (!entityMappings.addConverter(classHandle, attributeType.toString(), dbFieldType.toString())) { message("MSG_ATTRIBUTE_CONVERTER_TYPE_CONFLICT", classHandle); } else { if (!converter.isPresent()) { message("MSG_ATTRIBUTE_CONVERTER_TYPE_REGISTERED", classHandle, attributeType.toString(), dbFieldType.toString()); } validated.set(true); } } else { message("MSG_ATTRIBUTE_CONVERTER_NOT_IMPLEMENTED", classHandle); } } else { if (converter.isPresent()) { validated.set(true); } else { message("MSG_ARTIFACT_NOT_FOUND", classHandle, pkg.getPath()); } } } catch (IOException t) { ExceptionUtils.printStackTrace(t); } }, true); } catch (IOException ex) { Exceptions.printStackTrace(ex); } }
From source file:org.netbeans.jpa.modeler.properties.convert.ConvertPanel.java
static void importAttributeConverter(String classHandle, AtomicBoolean validated, ModelerFile modelerFile) { if (StringUtils.isBlank(classHandle)) { validated.set(true);//from ww w . j av a 2 s.c om return; } FileObject pkg = SourceGroupSupport.findSourceGroupForFile(modelerFile.getFileObject()).getRootFolder(); try { JavaSource javaSource = JavaSource.create(ClasspathInfo.create(pkg)); javaSource.runUserActionTask(controller -> { try { controller.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED); TypeElement jc = controller.getElements().getTypeElement(classHandle); EntityMappings entityMappings = (EntityMappings) modelerFile.getDefinitionElement(); Optional<Converter> converter = entityMappings.findConverter(classHandle); if (jc != null) { DeclaredType attributeConverterType = null; if (!jc.getInterfaces().isEmpty()) { //fetch interface info for (TypeMirror interfaceType : jc.getInterfaces()) { if (interfaceType.getKind() == TypeKind.DECLARED && AttributeConverter.class .getName().equals(((DeclaredType) interfaceType).asElement().toString())) { attributeConverterType = (DeclaredType) interfaceType; } } } if (attributeConverterType != null && attributeConverterType.getTypeArguments().size() == 2) { TypeMirror attributeType = attributeConverterType.getTypeArguments().get(0); TypeMirror dbFieldType = attributeConverterType.getTypeArguments().get(1); if (!entityMappings.addConverter(classHandle, attributeType.toString(), dbFieldType.toString())) { message("MSG_ATTRIBUTE_CONVERTER_TYPE_CONFLICT", classHandle); } else { if (!converter.isPresent()) { message("MSG_ATTRIBUTE_CONVERTER_TYPE_REGISTERED", classHandle, attributeType.toString(), dbFieldType.toString()); } validated.set(true); } } else { message("MSG_ATTRIBUTE_CONVERTER_NOT_IMPLEMENTED", classHandle); } } else { if (converter.isPresent()) { validated.set(true); } else { message("MSG_ARTIFACT_NOT_FOUND", classHandle, pkg.getPath()); } } } catch (IOException t) { ExceptionUtils.printStackTrace(t); } }, true); } catch (IOException ex) { Exceptions.printStackTrace(ex); } }
From source file:cop.raml.utils.example.JsonExample.java
/** * Retrieves example for given {@code element} only if given {@code element} is primitive or declared, when example can be auto generated. * First, look at element's javadoc and read {@link Macro#EXAMPLE}. Use it if it exists. * Second, look at element's javadoc and read {@link TagLink} macro, if it exists, then read linked element's javadoc and use {@link * Macro#EXAMPLE} if it exists./*from w ww. j a va 2 s . co m*/ * Third, generate random values using {@link #getPrimitiveExample(TypeKind, Random)} and {@link #getDeclaredExample(String, String)}. Otherwise * returns {@code null}. * * @param element element object * @param random not {@code null} random generator * @return variable example (could be any types or {@code null}) */ private static Object getAutoGeneratedElementExample(@NotNull Element element, @NotNull Random random) throws Exception { String doc; if (Macro.EXAMPLE.exists(doc = ThreadLocalContext.getDocComment(element))) return Macro.EXAMPLE.get(doc); if (Macro.EXAMPLE.exists(doc = ThreadLocalContext.getDocComment(TagLink.create(doc)))) return Macro.EXAMPLE.get(doc); TypeMirror type = ElementUtils.getType(element); Object res = getPrimitiveExample(type.getKind(), random); String varName = element instanceof VariableElement ? element.toString() : null; return res == null ? getDeclaredExample(type.toString(), varName) : res; }
From source file:com.vimeo.stag.processor.utils.TypeUtils.java
/** * Determines where the the type mirrors contains type var params or not * * @param typeMirror the element to check. * @return true if it contains type variables *///from w w w.j av a 2 s . c o m public static boolean containsTypeVarParams(@NotNull TypeMirror typeMirror) { if (typeMirror.getKind() == TypeKind.TYPEVAR) { return true; } if (typeMirror instanceof DeclaredType) { List<? extends TypeMirror> typeMirrors = ((DeclaredType) typeMirror).getTypeArguments(); for (TypeMirror type : typeMirrors) { if (containsTypeVarParams(type)) { return true; } } } return false; }
From source file:com.vimeo.stag.processor.utils.TypeUtils.java
/** * Determines whether or not the TypeMirror is a concrete type. * If the type is a generic type or contains generic type * arguments (i.e. a parameterized type), this method will * return false./*from w ww . jav a 2s . c om*/ * * @param typeMirror the element to check. * @return true if the type is not generic and * contains no generic type arguments, false otherwise. */ public static boolean isConcreteType(@NotNull TypeMirror typeMirror) { if (typeMirror.getKind() == TypeKind.TYPEVAR) { return false; } if (isPrimitive(typeMirror, getUtils())) { return true; } if (typeMirror instanceof DeclaredType) { List<? extends TypeMirror> typeMirrors = ((DeclaredType) typeMirror).getTypeArguments(); for (TypeMirror type : typeMirrors) { if (!isConcreteType(type)) { return false; } } } return true; }
From source file:com.vimeo.stag.processor.utils.TypeUtils.java
private static boolean isPrimitive(@NotNull TypeMirror type, @NotNull Types utils) { try {/*from w ww. jav a 2 s.c om*/ utils.getPrimitiveType(type.getKind()); return true; } catch (IllegalArgumentException ignored) { return false; } }
From source file:com.vimeo.stag.processor.utils.TypeUtils.java
@NotNull private static TypeMirror resolveTypeVars(@NotNull TypeMirror element, @NotNull final List<? extends TypeMirror> inheritedTypes, @NotNull final List<? extends TypeMirror> concreteTypes) { if (isConcreteType(element)) { return element; }/*from w ww . ja va 2s . c o m*/ if (element.getKind() == TypeKind.TYPEVAR) { int index = inheritedTypes.indexOf(element); return concreteTypes.get(index); } Types types = getUtils(); List<? extends TypeMirror> typeMirrors = ((DeclaredType) element).getTypeArguments(); TypeElement typeElement = (TypeElement) types.asElement(element); List<TypeMirror> concreteGenericTypes = new ArrayList<>(typeMirrors.size()); for (TypeMirror type : typeMirrors) { concreteGenericTypes.add(resolveTypeVars(type, inheritedTypes, concreteTypes)); } TypeMirror[] concreteTypeArray = concreteGenericTypes.toArray(new TypeMirror[concreteGenericTypes.size()]); return types.getDeclaredType(typeElement, concreteTypeArray); }