synchronized ObjectName newObjectName(String name) { try { if (mBeanNames.map.containsKey(name) && !miniClusterMode) { throw new MetricsException(name + " already exists!"); } return new ObjectName(mBeanNames.uniqueName(name)); } catch (Exception e) { throw new MetricsException(e); } }
synchronized String newSourceName(String name, boolean dupOK) { if (sourceNames.map.containsKey(name)) { if (dupOK) { return name; } else if (!miniClusterMode) { throw new MetricsException("Metrics source " + name + " already exists!"); } } return sourceNames.uniqueName(name); }
@Override Optional<TypeSpec.Builder> write(ClassName generatedTypeName, MembersInjectionBinding binding) { // Empty members injection bindings are special and don't need source files. if (binding.injectionSites().isEmpty()) { return Optional.absent(); } // We don't want to write out resolved bindings -- we want to write out the generic version. checkState(!binding.unresolved().isPresent()); ImmutableList<TypeVariableName> typeParameters = bindingTypeElementTypeVariableNames(binding); TypeSpec.Builder injectorTypeBuilder = TypeSpec.classBuilder(generatedTypeName.simpleName()) .addModifiers(PUBLIC, FINAL) .addTypeVariables(typeParameters); TypeName injectedTypeName = TypeName.get(binding.key().type()); TypeName implementedType = membersInjectorOf(injectedTypeName); injectorTypeBuilder.addSuperinterface(implementedType); MethodSpec.Builder injectMembersBuilder = MethodSpec.methodBuilder("injectMembers") .returns(TypeName.VOID) .addModifiers(PUBLIC) .addAnnotation(Override.class) .addParameter(injectedTypeName, "instance") .addCode("if (instance == null) {") .addStatement( "throw new $T($S)", NullPointerException.class, "Cannot inject members into a null reference") .addCode("}"); ImmutableMap<BindingKey, FrameworkField> fields = SourceFiles.generateBindingFieldsForDependencies(dependencyRequestMapper, binding); ImmutableMap.Builder<BindingKey, FieldSpec> dependencyFieldsBuilder = ImmutableMap.builder(); MethodSpec.Builder constructorBuilder = MethodSpec.constructorBuilder().addModifiers(PUBLIC); // We use a static create method so that generated components can avoid having // to refer to the generic types of the factory. // (Otherwise they may have visibility problems referring to the types.) MethodSpec.Builder createMethodBuilder = MethodSpec.methodBuilder("create") .returns(implementedType) .addModifiers(Modifier.PUBLIC, Modifier.STATIC) .addTypeVariables(typeParameters); createMethodBuilder.addCode( "return new $T(", javapoetParameterizedGeneratedTypeNameForBinding(binding)); ImmutableList.Builder<CodeBlock> constructorInvocationParameters = ImmutableList.builder(); boolean usesRawFrameworkTypes = false; UniqueNames fieldNames = new UniqueNames(); for (Entry<BindingKey, FrameworkField> fieldEntry : fields.entrySet()) { BindingKey bindingKey = fieldEntry.getKey(); FrameworkField bindingField = fieldEntry.getValue(); // If the dependency type is not visible to this members injector, then use the raw framework // type for the field. boolean useRawFrameworkType = !VISIBLE_TO_MEMBERS_INJECTOR.visit(bindingKey.key().type(), binding); String fieldName = fieldNames.getUniqueName(bindingField.name()); TypeName fieldType = useRawFrameworkType ? bindingField.javapoetFrameworkType().rawType : bindingField.javapoetFrameworkType(); FieldSpec.Builder fieldBuilder = FieldSpec.builder(fieldType, fieldName, PRIVATE, FINAL); ParameterSpec.Builder parameterBuilder = ParameterSpec.builder(fieldType, fieldName); // If we're using the raw type for the field, then suppress the injectMembers method's // unchecked-type warning and the field's and the constructor and create-method's // parameters' raw-type warnings. if (useRawFrameworkType) { usesRawFrameworkTypes = true; fieldBuilder.addAnnotation(SUPPRESS_WARNINGS_RAWTYPES); parameterBuilder.addAnnotation(SUPPRESS_WARNINGS_RAWTYPES); } constructorBuilder.addParameter(parameterBuilder.build()); createMethodBuilder.addParameter(parameterBuilder.build()); FieldSpec field = fieldBuilder.build(); injectorTypeBuilder.addField(field); constructorBuilder.addStatement("assert $N != null", field); constructorBuilder.addStatement("this.$N = $N", field, field); dependencyFieldsBuilder.put(bindingKey, field); constructorInvocationParameters.add(CodeBlocks.format("$N", field)); } createMethodBuilder.addCode(CodeBlocks.join(constructorInvocationParameters.build(), ", ")); createMethodBuilder.addCode(");"); injectorTypeBuilder.addMethod(constructorBuilder.build()); injectorTypeBuilder.addMethod(createMethodBuilder.build()); Set<String> delegateMethods = new HashSet<>(); ImmutableMap<BindingKey, FieldSpec> dependencyFields = dependencyFieldsBuilder.build(); List<MethodSpec> injectMethodsForSubclasses = new ArrayList<>(); for (InjectionSite injectionSite : binding.injectionSites()) { injectMembersBuilder.addCode( visibleToMembersInjector(binding, injectionSite.element()) ? directInjectMemberCodeBlock(binding, dependencyFields, injectionSite) : delegateInjectMemberCodeBlock(dependencyFields, injectionSite)); if (!injectionSite.element().getModifiers().contains(PUBLIC) && injectionSite.element().getEnclosingElement().equals(binding.bindingElement()) && delegateMethods.add(injectionSiteDelegateMethodName(injectionSite.element()))) { injectMethodsForSubclasses.add( injectorMethodForSubclasses( dependencyFields, typeParameters, injectedTypeName, injectionSite.element(), injectionSite.dependencies())); } } if (usesRawFrameworkTypes) { injectMembersBuilder.addAnnotation(SUPPRESS_WARNINGS_UNCHECKED); } injectorTypeBuilder.addMethod(injectMembersBuilder.build()); for (MethodSpec methodSpec : injectMethodsForSubclasses) { injectorTypeBuilder.addMethod(methodSpec); } return Optional.of(injectorTypeBuilder); }