List of usage examples for javax.lang.model.element TypeElement getSimpleName
@Override Name getSimpleName();
From source file:info.archinnov.achilles.internals.codegen.meta.EntityMetaCodeGen.java
private MethodSpec buildDerivedTableName(TypeElement elm, InternalNamingStrategy defaultStrategy) { final Optional<Strategy> strategy = aptUtils.getAnnotationOnClass(elm, Strategy.class); final String tableName = inferNamingStrategy(strategy, defaultStrategy) .apply(elm.getSimpleName().toString()); return MethodSpec.methodBuilder("getDerivedTableOrViewName").addAnnotation(Override.class) .addModifiers(Modifier.PROTECTED).returns(String.class).addStatement("return $S", tableName) .build();/* w w w . j av a 2s . c o m*/ }
From source file:info.archinnov.achilles.internals.parser.FunctionParamParser.java
protected FunctionParamSignature parseUDT(GlobalParsingContext context, AnnotationTree annotationTree, String paramName) {//from ww w .j a va 2 s .c om final boolean isFrozen = AptUtils.containsAnnotation(annotationTree, Frozen.class); final TypeMirror typeMirror = annotationTree.getCurrentType(); final TypeName udtTypeName = TypeName.get(typeMirror); final TypeElement typeElement = aptUtils.asTypeElement(typeMirror); udtParser.validateUDT(udtTypeName, typeElement); final Optional<Strategy> strategy = aptUtils.getAnnotationOnClass(typeElement, Strategy.class); final UDT udt = aptUtils.getAnnotationOnClass(typeElement, UDT.class).get(); final String udtName = isBlank(udt.name()) ? inferNamingStrategy(strategy, context.namingStrategy) .apply(typeElement.getSimpleName().toString()) : udt.name(); if (isFrozen) { return new FunctionParamSignature(paramName, udtTypeName, JAVA_DRIVER_UDT_VALUE_TYPE, "frozen<" + udtName + ">"); } else { return new FunctionParamSignature(paramName, udtTypeName, JAVA_DRIVER_UDT_VALUE_TYPE, udtName); } }
From source file:com.mastfrog.parameters.processor.Processor.java
@Override public boolean process(Set<? extends TypeElement> set, RoundEnvironment re) { SourceVersion sv = processingEnv.getSourceVersion(); if (sv.ordinal() > SourceVersion.RELEASE_7.ordinal()) { optionalType = "java.util.Optional"; } else {//www .j a v a 2s . co m TypeElement el = processingEnv.getElementUtils().getTypeElement(optionalType); if (el == null) { optionalType = "com.mastfrog.util.Optional"; } else { optionalType = "com.google.common.base.Optional"; fromNullable = "fromNullable"; } } Set<? extends Element> all = re.getElementsAnnotatedWith(Params.class); List<GeneratedParamsClass> interfaces = new LinkedList<>(); outer: for (Element e : all) { TypeElement te = (TypeElement) e; if (te.getSimpleName().toString().endsWith("__GenPage")) { continue; } PackageElement pkg = findPackage(e); if (pkg == null) { processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "@Params may not be used in the default package", e); continue; } if (!isPageSubtype(te)) { processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "@Params must be used on a subclass of org.apache.wicket.Page", e); continue; } String className = te.getQualifiedName().toString(); Params params = te.getAnnotation(Params.class); Map<String, List<String>> validators = validatorsForParam(e); GeneratedParamsClass inf = new GeneratedParamsClass(className, te, pkg, params, validators); if (!params.useRequestBody()) { checkConstructor(te, inf); } interfaces.add(inf); Set<String> names = new HashSet<>(); for (Param param : params.value()) { // if (param.required() && !param.defaultValue().isEmpty()) { // processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Don't set required to " // + "true if you are providing a default value - required makes it an error not " // + "to have a value, and if there is a default value, that error is an impossibility " // + "because it will always have a value.", e); // continue outer; // } if (param.value().trim().isEmpty()) { processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Empty parameter name", e); continue outer; } if (!isJavaIdentifier(param.value())) { processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Not a valid Java identifier: " + param.value(), e); continue outer; } if (!names.add(param.value())) { processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Duplicate parameter name '" + param.value() + "'", e); continue outer; } for (char c : ";,./*!@&^/\\<>?'\"[]{}-=+)(".toCharArray()) { if (param.value().contains("" + c)) { processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Param name may not contain the character '" + c + "'", e); } } inf.add(param); } } Filer filer = processingEnv.getFiler(); StringBuilder listBuilder = new StringBuilder(); for (GeneratedParamsClass inf : interfaces) { try { String pth = inf.packageAsPath() + '/' + inf.className; pth = pth.replace('/', '.'); JavaFileObject obj = filer.createSourceFile(pth, inf.el); try (OutputStream out = obj.openOutputStream()) { out.write(inf.toString().getBytes("UTF-8")); } listBuilder.append(inf.className).append('\n'); } catch (Exception ex) { ex.printStackTrace(); processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Error processing annotation: " + ex.getMessage(), inf.el); Logger.getLogger(Processor.class.getName()).log(Level.SEVERE, null, ex); } } if (re.processingOver()) { try { FileObject list = filer.createResource(StandardLocation.CLASS_OUTPUT, "", "META-INF/paramanos/bind.list", all.toArray(new Element[0])); try (OutputStream out = list.openOutputStream()) { out.write(listBuilder.toString().getBytes("UTF-8")); } } catch (FilerException ex) { Logger.getLogger(Processor.class.getName()).log(Level.INFO, null, ex); } catch (IOException ex) { Logger.getLogger(Processor.class.getName()).log(Level.SEVERE, null, ex); } } return true; }
From source file:auto.parse.processor.AutoParseProcessor.java
private String generatedClassName(TypeElement type, String prefix) { String name = type.getSimpleName().toString(); while (type.getEnclosingElement() instanceof TypeElement) { type = (TypeElement) type.getEnclosingElement(); name = type.getSimpleName() + "_" + name; }//from w w w . j a v a 2 s . c om String pkg = TypeSimplifier.packageNameOf(type); String dot = pkg.isEmpty() ? "" : "."; return pkg + dot + prefix + name; }
From source file:org.kie.workbench.common.forms.adf.processors.FormDefinitionsProcessor.java
protected void processFormDefinition(TypeElement formElement) throws Exception { final Messager messager = processingEnv.getMessager(); messager.printMessage(Diagnostic.Kind.NOTE, "Discovered FormDefintion class [" + formElement.getSimpleName() + "]"); boolean checkInheritance = false; FormDefinition defintion = formElement.getAnnotation(FormDefinition.class); checkInheritance = defintion.allowInheritance(); List<Map<String, String>> formElements = new ArrayList<>(); if (checkInheritance) { TypeElement parent = getParent(formElement); formElements.addAll(extractParentFormFields(parent, defintion.policy(), defintion.i18n())); }/* w w w . j a va 2 s. c o m*/ formElements.addAll(extracFormFields(formElement, defintion.policy(), defintion.i18n())); FormGenerationUtils.sort(defintion.startElement(), formElements); messager.printMessage(Diagnostic.Kind.NOTE, "Discovered " + formElements.size() + " elements for form [" + formElement.getQualifiedName().toString() + "]"); String modelClassName = formElement.getQualifiedName().toString(); String builderClassName = fixClassName(formElement.getQualifiedName().toString()) + "FormBuilder"; Map<String, Object> templateContext = new HashMap<>(); templateContext.put("modelClass", modelClassName); templateContext.put("builderClassName", builderClassName); templateContext.put("startElement", defintion.startElement()); templateContext.put("i18n_bundle", StringUtils.isEmpty(defintion.i18n().bundle()) ? formElement.asType().toString() : defintion.i18n().bundle()); Column[] columns = defintion.layout().value(); List<String> layoutColumns = new ArrayList<>(); if (columns.length == 0) { layoutColumns.add(ColSpan.SPAN_12.getName()); } else { for (Column column : columns) { layoutColumns.add(column.value().getName()); } } templateContext.put("layout_columns", layoutColumns); templateContext.put("elements", formElements); StringBuffer builder = writeTemplate("templates/FormDefinitionSettingsBuilder.ftl", templateContext); Map<String, String> form = new HashMap<>(); form.put("package", ((PackageElement) formElement.getEnclosingElement()).getQualifiedName().toString()); form.put("modelClass", modelClassName); form.put("builderClass", builderClassName); form.put("builderCode", builder.toString()); context.getForms().add(form); }
From source file:com.rgeldmacher.leash.LeashAnnotationProcessor.java
private TypeSpec createRetainedFragmentSpec(TypeElement classWithAnnotations, Set<Element> annotatedFields) { MethodSpec ctor = MethodSpec.constructorBuilder().addModifiers(Modifier.PUBLIC) .addCode("setRetainInstance(true);").build(); ArrayList<FieldSpec> fieldSpecs = new ArrayList<>(annotatedFields.size()); for (Element field : annotatedFields) { FieldSpec fieldSpec = FieldSpec.builder(TypeName.get(field.asType()), field.getSimpleName().toString()) .build();/*from w w w . j a v a2 s.c om*/ fieldSpecs.add(fieldSpec); } FieldSpec hasBeenRetained = FieldSpec.builder(TypeName.BOOLEAN, "hasBeenRetained").build(); fieldSpecs.add(hasBeenRetained); return TypeSpec.classBuilder(classWithAnnotations.getSimpleName() + "RetainedDataFragment") .addModifiers(Modifier.PUBLIC, Modifier.STATIC).superclass(getFragmentClass(classWithAnnotations)) .addFields(fieldSpecs).addMethod(ctor).build(); }
From source file:org.kie.workbench.common.forms.adf.processors.FormDefinitionsProcessor.java
private void processFieldDefinition(TypeElement fieldDefinitionElement) throws Exception { final Messager messager = processingEnv.getMessager(); messager.printMessage(Diagnostic.Kind.NOTE, "Discovered FieldDefinition class [" + fieldDefinitionElement.getSimpleName() + "]"); Collection<FieldInfo> fieldInfos = extractFieldInfos(fieldDefinitionElement, null); String modelClassName = fieldDefinitionElement.getQualifiedName().toString(); String fieldModifierName = fixClassName(modelClassName) + "_FieldStatusModifier"; Map<String, String> fieldDefinition = new HashMap<>(); fieldDefinition.put("className", modelClassName); fieldDefinition.put("fieldModifierName", fieldModifierName); Map<String, Object> templateContext = new HashMap<>(); templateContext.put("modelClassName", modelClassName); templateContext.put("fieldModifierName", fieldModifierName); FieldDefinition fieldDefinitionAnnotation = fieldDefinitionElement.getAnnotation(FieldDefinition.class); for (FieldInfo fieldInfo : fieldInfos) { AnnotationMirror annotation = GeneratorUtils.getAnnotation(elementUtils, fieldInfo.fieldElement, FieldValue.class.getName()); if (annotation != null) { if (fieldDefinition.containsKey("value")) { throw new Exception("Problem processing FieldDefinition [" + modelClassName + "]: it should have only one @FieldValue"); }/*from w w w .j a v a 2s.c om*/ if (fieldInfo.getter == null || fieldInfo.setter == null) { throw new Exception("Problem processing FieldDefinition [" + modelClassName + "]: field marked as @FieldValue should have setter & getter"); } fieldDefinition.put("value", fieldInfo.fieldElement.getSimpleName().toString()); } else { annotation = GeneratorUtils.getAnnotation(elementUtils, fieldInfo.fieldElement, FieldReadOnly.class.getName()); if (annotation != null) { if (templateContext.containsKey("readOnly")) { throw new Exception("Problem processing FieldDefinition [" + modelClassName + "]: it should have only one @FieldReadOnly"); } if (!fieldInfo.fieldElement.asType().getKind().equals(TypeKind.BOOLEAN) && !fieldInfo.fieldElement.asType().toString().equals(Boolean.class.getName())) { throw new Exception("Problem processing FieldDefinition [" + modelClassName + "]: field marked as @FieldReadOnly must be boolean or Boolean"); } if (fieldInfo.getter == null) { throw new Exception("Problem processing FieldDefinition [" + modelClassName + "]: field marked as @FieldReadOnly should have getter"); } templateContext.put("readOnly", fieldInfo.getter); } annotation = GeneratorUtils.getAnnotation(elementUtils, fieldInfo.fieldElement, FieldRequired.class.getName()); if (annotation != null) { if (templateContext.containsKey("required")) { throw new Exception("Problem processing FieldDefinition [" + modelClassName + "]: it should have only one @FieldRequired"); } if (!fieldInfo.fieldElement.asType().getKind().equals(TypeKind.BOOLEAN) && !fieldInfo.fieldElement.asType().toString().equals(Boolean.class.getName())) { throw new Exception("Problem processing FieldDefinition [" + modelClassName + "]: field marked as @FieldRequired must be boolean or Boolean"); } if (fieldInfo.getter == null) { throw new Exception("Problem processing FieldDefinition [" + modelClassName + "]: field marked as @FieldRequired should have getter"); } templateContext.put("required", fieldInfo.getter); } if (fieldDefinitionAnnotation.labelMode().equals(LabelMode.OVERRIDE)) { annotation = GeneratorUtils.getAnnotation(elementUtils, fieldInfo.fieldElement, FieldLabel.class.getName()); if (annotation != null) { if (templateContext.containsKey("label")) { throw new Exception("Problem processing FieldDefinition [" + modelClassName + "]: it should have only one @FieldLabel"); } if (!fieldInfo.fieldElement.asType().toString().equals(String.class.getName())) { throw new Exception("Problem processing FieldDefinition [" + modelClassName + "]: field marked as @FieldLabel must be a String"); } if (fieldInfo.getter == null) { throw new Exception("Problem processing FieldDefinition [" + modelClassName + "]: field marked as @FieldLabel should have getter"); } templateContext.put("label", fieldInfo.getter); } } } } StringBuffer source = writeTemplate("templates/FieldDefinitionModifier.ftl", templateContext); fieldDefinition.put("sourceCode", source.toString()); context.getFieldDefinitions().add(fieldDefinition); }
From source file:de.adorsys.beanval2json.converter.BeanvalConverter.java
/** * Searches for all properties which are annotated with given TypeElement and * updates the constraintsMap with the according constraints *///w w w .j a v a 2 s. com public void addConstraints(TypeElement typeElement, RoundEnvironment roundEnv, Map<String, Constraints> constraintsMap) throws ProcessingException { for (Element element : roundEnv.getElementsAnnotatedWith(typeElement)) { String name = getFqn(element); if (ctx.ignoreProperty(name)) { continue; } for (AnnotationMirror annotationMirror : element.getAnnotationMirrors()) { if (!ctx.getTypeUtils().isSameType(typeElement.asType(), annotationMirror.getAnnotationType())) { continue; } Constraint constraint = convertConstraint(annotationMirror); Constraints constraints = getConstraints(constraintsMap, name); try { String methodName = String.format("%s%s", IDENTIFIER_SETTER, typeElement.getSimpleName()); constraints.getClass().getMethod(methodName, constraint.getClass()).invoke(constraints, constraint); } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException ex) { throw new ProcessingException(String.format("Could not add %s-Constraint from %s: %s", typeElement.getSimpleName(), element.getSimpleName(), ex.getMessage())); } } } }
From source file:org.kie.workbench.common.stunner.core.processors.MainProcessor.java
private String getTypeName(final Element e) { TypeElement classElement = (TypeElement) e; PackageElement packageElement = (PackageElement) classElement.getEnclosingElement(); return packageElement.getQualifiedName().toString() + "." + classElement.getSimpleName(); }
From source file:org.kie.workbench.common.stunner.core.processors.MainProcessor.java
private boolean processDockingRules(final Element e) throws Exception { final Messager messager = processingEnv.getMessager(); final boolean isIface = e.getKind() == ElementKind.INTERFACE; final boolean isClass = e.getKind() == ElementKind.CLASS; if (isIface || isClass) { TypeElement classElement = (TypeElement) e; PackageElement packageElement = (PackageElement) classElement.getEnclosingElement(); messager.printMessage(Diagnostic.Kind.NOTE, "Discovered docking rule for class [" + classElement.getSimpleName() + "]"); final String classNameActivity = classElement.getSimpleName() + RULE_DOCKING_SUFFIX_CLASSNAME; generateRuleCode(dockingRuleGenerator, messager, classElement, packageElement, classNameActivity); }/*from ww w. j a v a 2 s . c o m*/ return true; }