protected <T, K> T applyTrait( K core, Class<T> trait, Object value, boolean logical ) throws LogicalTypeInconsistencyException {
TraitFactory builder = TraitFactory.getTraitBuilderForKnowledgeBase( this.getKnowledgeRuntime().getKieBase() );
TraitableBean inner = makeTraitable( core, builder, logical );
boolean needsProxy = trait.isAssignableFrom( inner.getClass() );
boolean hasTrait = inner.hasTrait( trait.getName() );
boolean needsUpdate = needsProxy || core != inner;
BitSet boundary = inner.getCurrentTypeCode() != null ? (BitSet) inner.getCurrentTypeCode().clone() : null;
Collection<Thing> mostSpecificTraits = getTraitBoundary( inner, needsProxy, hasTrait, trait );
T thing = asTrait( core, inner, trait, needsProxy, hasTrait, needsUpdate, builder, logical );
configureTrait( thing, value );
thing = doInsertTrait( thing, core, logical, boundary );
refresh( thing, core, inner, trait, mostSpecificTraits, logical );
if ( trait != Thing.class && inner._getFieldTMS() != null ) {
inner._getFieldTMS().resetModificationMask();
}
return thing;
}